{"id":1063398,"date":"2024-12-31T16:00:20","date_gmt":"2024-12-31T08:00:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1063398.html"},"modified":"2024-12-31T16:00:22","modified_gmt":"2024-12-31T08:00:22","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86mat%e7%9f%a9%e9%98%b5%e8%bd%ac%e4%b8%banpy","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1063398.html","title":{"rendered":"python\u5982\u4f55\u5c06mat\u77e9\u9635\u8f6c\u4e3anpy"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/3918b82a-39ed-4e97-afc0-c206bd627a4d.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5c06mat\u77e9\u9635\u8f6c\u4e3anpy\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06MAT\u77e9\u9635\u8f6c\u6362\u4e3aNPY\u6587\u4ef6<\/strong>\uff0c\u5305\u62ec\u4f7f\u7528SciPy\u5e93\u52a0\u8f7dMAT\u6587\u4ef6\u5e76\u4f7f\u7528NumPy\u5e93\u4fdd\u5b58\u4e3aNPY\u6587\u4ef6\u3001\u4f7f\u7528Matlab\u5f15\u64ce\u76f4\u63a5\u5728Python\u4e2d\u8f6c\u6362\u3001\u4ee5\u53ca\u4f7f\u7528HDF5\u683c\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u901a\u8fc7SciPy\u548cNumPy\u5e93\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p><strong>\u6838\u5fc3\u89c2\u70b9:<\/strong> \u4f7f\u7528SciPy\u5e93\u52a0\u8f7dMAT\u6587\u4ef6\u3001\u4f7f\u7528NumPy\u5e93\u4fdd\u5b58\u4e3aNPY\u6587\u4ef6\u3001\u786e\u4fdd\u6570\u636e\u7c7b\u578b\u548c\u7ef4\u5ea6\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5c06MAT\u77e9\u9635\u8f6c\u6362\u4e3aNPY\u6587\u4ef6\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u5148\u4f7f\u7528SciPy\u5e93\u7684<code>scipy.io.loadmat<\/code>\u51fd\u6570\u6765\u52a0\u8f7dMAT\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528NumPy\u5e93\u7684<code>numpy.save<\/code>\u51fd\u6570\u5c06\u6570\u636e\u4fdd\u5b58\u4e3aNPY\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u6240\u9700\u7684Python\u5e93\uff1aSciPy\u548cNumPy\u3002<\/li>\n<li>\u4f7f\u7528SciPy\u5e93\u52a0\u8f7dMAT\u6587\u4ef6\u3002<\/li>\n<li>\u63d0\u53d6MAT\u6587\u4ef6\u4e2d\u7684\u77e9\u9635\u6570\u636e\u3002<\/li>\n<li>\u4f7f\u7528NumPy\u5e93\u5c06\u63d0\u53d6\u7684\u6570\u636e\u4fdd\u5b58\u4e3aNPY\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u6240\u9700\u7684Python\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86SciPy\u548cNumPy\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scipy numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u52a0\u8f7dMAT\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528SciPy\u5e93\u4e2d\u7684<code>scipy.io.loadmat<\/code>\u51fd\u6570\u52a0\u8f7dMAT\u6587\u4ef6\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u8bfb\u53d6MAT\u6587\u4ef6\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542bMAT\u6587\u4ef6\u5185\u5bb9\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import scipy.io<\/p>\n<h2><strong>\u52a0\u8f7dMAT\u6587\u4ef6<\/strong><\/h2>\n<p>mat_data = scipy.io.loadmat(&#39;example.mat&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u63d0\u53d6\u77e9\u9635\u6570\u636e<\/h3>\n<\/p>\n<p><p>MAT\u6587\u4ef6\u52a0\u8f7d\u540e\uff0c\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5305\u542bMAT\u6587\u4ef6\u5185\u5bb9\u7684\u5b57\u5178\u3002\u4f60\u9700\u8981\u4ece\u5b57\u5178\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u77e9\u9635\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbeMAT\u6587\u4ef6\u4e2d\u5305\u542b\u4e00\u4e2a\u540d\u4e3a&#39;matrix_name&#39;\u7684\u77e9\u9635<\/p>\n<p>matrix_data = mat_data[&#39;matrix_name&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4fdd\u5b58\u4e3aNPY\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u5e93\u4e2d\u7684<code>numpy.save<\/code>\u51fd\u6570\u5c06\u63d0\u53d6\u7684\u77e9\u9635\u6570\u636e\u4fdd\u5b58\u4e3aNPY\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aNPY\u6587\u4ef6<\/strong><\/h2>\n<p>np.save(&#39;output.npy&#39;, matrix_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u7528\u4e8e\u5c06MAT\u77e9\u9635\u8f6c\u6362\u4e3aNPY\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import scipy.io<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u52a0\u8f7dMAT\u6587\u4ef6<\/strong><\/h2>\n<p>mat_data = scipy.io.loadmat(&#39;example.mat&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u77e9\u9635\u6570\u636e<\/strong><\/h2>\n<p>matrix_data = mat_data[&#39;matrix_name&#39;]<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aNPY\u6587\u4ef6<\/strong><\/h2>\n<p>np.save(&#39;output.npy&#39;, matrix_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u786e\u4fdd\u6570\u636e\u7c7b\u578b\u548c\u7ef4\u5ea6\u4e00\u81f4<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6570\u636e\u8f6c\u6362\u65f6\uff0c\u786e\u4fddMAT\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u548c\u7ef4\u5ea6\u4e0eNPY\u6587\u4ef6\u4fdd\u6301\u4e00\u81f4\u975e\u5e38\u91cd\u8981\u3002\u5982\u679c\u9700\u8981\uff0c\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u9002\u5f53\u7684\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u8f6c\u6362\u540e\u7684\u6570\u636e\u7b26\u5408\u9884\u671f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0cMAT\u6587\u4ef6\u4e2d\u53ef\u80fd\u5305\u542b\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5d4c\u5957\u7684\u6570\u7ec4\u3001\u5143\u7ec4\u6216\u81ea\u5b9a\u4e49\u5bf9\u8c61\u3002\u5904\u7406\u8fd9\u4e9b\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u989d\u5916\u7684\u6b65\u9aa4\u6765\u786e\u4fdd\u6570\u636e\u6b63\u786e\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u5904\u7406\u5d4c\u5957\u7684\u6570\u7ec4<\/p>\n<p>nested_array = mat_data[&#39;nested_array&#39;]<\/p>\n<h2><strong>\u5c06\u5d4c\u5957\u7684\u6570\u7ec4\u5c55\u5e73\u4e3a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>flattened_array = nested_array.reshape(-1, nested_array.shape[-1])<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aNPY\u6587\u4ef6<\/strong><\/h2>\n<p>np.save(&#39;flattened_output.npy&#39;, flattened_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528HDF5\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528HDF5\u683c\u5f0f\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u548c\u8f6c\u6362\u3002HDF5\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u548c\u7ba1\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u6587\u4ef6\u683c\u5f0f\uff0c\u9002\u7528\u4e8e\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import h5py<\/p>\n<h2><strong>\u52a0\u8f7dMAT\u6587\u4ef6<\/strong><\/h2>\n<p>mat_data = scipy.io.loadmat(&#39;example.mat&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u77e9\u9635\u6570\u636e<\/strong><\/h2>\n<p>matrix_data = mat_data[&#39;matrix_name&#39;]<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aHDF5\u6587\u4ef6<\/strong><\/h2>\n<p>with h5py.File(&#39;output.h5&#39;, &#39;w&#39;) as h5f:<\/p>\n<p>    h5f.create_dataset(&#39;dataset_name&#39;, data=matrix_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528HDF5\u683c\u5f0f\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5e76\u63d0\u4f9b\u66f4\u7075\u6d3b\u7684\u6570\u636e\u5b58\u50a8\u548c\u8bbf\u95ee\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u793a\u4f8b\u4ee3\u7801\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528SciPy\u548cNumPy\u5e93\u5c06MAT\u77e9\u9635\u8f6c\u6362\u4e3aNPY\u6587\u4ef6\uff0c\u5e76\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import scipy.io<\/p>\n<p>import numpy as np<\/p>\n<p>import h5py<\/p>\n<h2><strong>\u52a0\u8f7dMAT\u6587\u4ef6<\/strong><\/h2>\n<p>mat_data = scipy.io.loadmat(&#39;example.mat&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u77e9\u9635\u6570\u636e<\/strong><\/h2>\n<p>matrix_data = mat_data[&#39;matrix_name&#39;]<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aNPY\u6587\u4ef6<\/strong><\/h2>\n<p>np.save(&#39;output.npy&#39;, matrix_data)<\/p>\n<h2><strong>\u5904\u7406\u5d4c\u5957\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>nested_array = mat_data[&#39;nested_array&#39;]<\/p>\n<p>flattened_array = nested_array.reshape(-1, nested_array.shape[-1])<\/p>\n<p>np.save(&#39;flattened_output.npy&#39;, flattened_array)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aHDF5\u6587\u4ef6<\/strong><\/h2>\n<p>with h5py.File(&#39;output.h5&#39;, &#39;w&#39;) as h5f:<\/p>\n<p>    h5f.create_dataset(&#39;dataset_name&#39;, data=matrix_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06MAT\u77e9\u9635\u8f6c\u6362\u4e3aNPY\u6587\u4ef6\uff0c\u5e76\u6839\u636e\u9700\u8981\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002\u786e\u4fdd\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u68c0\u67e5\u6570\u636e\u7c7b\u578b\u548c\u7ef4\u5ea6\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06MATLAB\u7684\u77e9\u9635\u6570\u636e\u5bfc\u5165Python\u8fdb\u884c\u5904\u7406\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528SciPy\u5e93\u7684<code>scipy.io.loadmat<\/code>\u51fd\u6570\u6765\u8bfb\u53d6MATLAB\u7684MAT\u6587\u4ef6\u3002\u8bfb\u53d6\u540e\uff0c\u60a8\u53ef\u4ee5\u83b7\u53d6\u77e9\u9635\u6570\u636e\u5e76\u8fdb\u884c\u5904\u7406\uff0c\u4e4b\u540e\u518d\u4f7f\u7528NumPy\u5c06\u5176\u4fdd\u5b58\u4e3a.npy\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u4fdd\u5b58\u4e3a.npy\u683c\u5f0f\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\u4f7f\u7528NumPy\u7684<code>numpy.save<\/code>\u51fd\u6570\u5c06\u6570\u7ec4\u4fdd\u5b58\u4e3a.npy\u683c\u5f0f\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u60a8\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u540d\u5177\u6709\u63cf\u8ff0\u6027\uff0c\u4ee5\u4fbf\u5c06\u6765\u8f7b\u677e\u8bc6\u522b\u3002\u540c\u65f6\uff0c\u4fdd\u6301\u4fdd\u5b58\u7684\u6570\u7ec4\u7ef4\u5ea6\u548c\u6570\u636e\u7c7b\u578b\u7684\u4e00\u81f4\u6027\u4e5f\u5f88\u91cd\u8981\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u52a0\u8f7d.npy\u6587\u4ef6\u4ee5\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u5206\u6790\uff1f<\/strong><br \/>\u4f7f\u7528NumPy\u7684<code>numpy.load<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u52a0\u8f7d.npy\u6587\u4ef6\u3002\u52a0\u8f7d\u540e\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u64cd\u4f5c\u6570\u7ec4\u6570\u636e\uff0c\u4f8b\u5982\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u53ef\u89c6\u5316\u6216<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u7684\u8bad\u7ec3\u7b49\u3002\u786e\u4fdd\u5728\u52a0\u8f7d\u65f6\u6ce8\u610f\u6587\u4ef6\u8def\u5f84\uff0c\u4ee5\u907f\u514d\u6587\u4ef6\u672a\u627e\u5230\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06MAT\u77e9\u9635\u8f6c\u6362\u4e3aNPY\u6587\u4ef6\uff0c\u5305\u62ec\u4f7f\u7528SciPy\u5e93\u52a0\u8f7dMAT\u6587\u4ef6\u5e76\u4f7f\u7528NumP [&hellip;]","protected":false},"author":3,"featured_media":1063404,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1063398"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1063398"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1063398\/revisions"}],"predecessor-version":[{"id":1063408,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1063398\/revisions\/1063408"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1063404"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1063398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1063398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1063398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}