{"id":1098153,"date":"2025-01-08T15:18:34","date_gmt":"2025-01-08T07:18:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098153.html"},"modified":"2025-01-08T15:18:37","modified_gmt":"2025-01-08T07:18:37","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86mat%e7%9f%a9%e9%98%b5%e8%bd%ac%e4%b8%banpy-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1098153.html","title":{"rendered":"python\u5982\u4f55\u5c06mat\u77e9\u9635\u8f6c\u4e3anpy"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212612\/587916a7-496b-4422-8d97-a9d6e9e1a826.webp\" alt=\"python\u5982\u4f55\u5c06mat\u77e9\u9635\u8f6c\u4e3anpy\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u6b65\u5c06mat\u77e9\u9635\u8f6c\u6362\u4e3anpy\u683c\u5f0f\uff1a\u9996\u5148\u4f7f\u7528SciPy\u5e93\u4e2d\u7684loadmat\u51fd\u6570\u8bfb\u53d6mat\u6587\u4ef6\uff0c\u63a5\u7740\u4f7f\u7528NumPy\u5e93\u4e2d\u7684save\u51fd\u6570\u5c06\u6570\u636e\u4fdd\u5b58\u4e3anpy\u6587\u4ef6\u3001\u4f7f\u7528SciPy\u5e93\u8bfb\u53d6mat\u6587\u4ef6\u3001\u4f7f\u7528NumPy\u5e93\u4fdd\u5b58\u4e3anpy\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u6240\u9700\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u7684Python\u73af\u5883\u4e2d\u5b89\u88c5\u4e86SciPy\u548cNumPy\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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\u8bfb\u53d6mat\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u8981\u8bfb\u53d6mat\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528SciPy\u5e93\u4e2d\u7684loadmat\u51fd\u6570\u3002\u6b64\u51fd\u6570\u53ef\u4ee5\u5c06mat\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u52a0\u8f7d\u4e3a\u4e00\u4e2a\u5b57\u5178\u5bf9\u8c61\uff0c\u5176\u4e2d\u952e\u662f\u53d8\u91cf\u540d\uff0c\u503c\u662f\u53d8\u91cf\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import scipy.io<\/p>\n<h2><strong>\u8bfb\u53d6mat\u6587\u4ef6<\/strong><\/h2>\n<p>mat_data = scipy.io.loadmat(&#39;your_mat_file.mat&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u63d0\u53d6\u9700\u8981\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4ece\u52a0\u8f7d\u7684\u5b57\u5178\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u5047\u8bbe\u60a8\u7684mat\u6587\u4ef6\u4e2d\u5305\u542b\u4e00\u4e2a\u540d\u4e3a&#39;my_matrix&#39;\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d0\u53d6\u77e9\u9635\u6570\u636e<\/p>\n<p>matrix_data = mat_data[&#39;my_matrix&#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\u7684save\u51fd\u6570\u5c06\u63d0\u53d6\u7684\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;your_npy_file.npy&#39;, matrix_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c06mat\u6587\u4ef6\u4e2d\u7684\u77e9\u9635\u6570\u636e\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>\u8bfb\u53d6mat\u6587\u4ef6<\/strong><\/h2>\n<p>mat_data = scipy.io.loadmat(&#39;your_mat_file.mat&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u77e9\u9635\u6570\u636e<\/strong><\/h2>\n<p>matrix_data = mat_data[&#39;my_matrix&#39;]<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3anpy\u6587\u4ef6<\/strong><\/h2>\n<p>np.save(&#39;your_npy_file.npy&#39;, matrix_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u9a8c\u8bc1\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u6570\u636e\u8f6c\u6362\u7684\u6b63\u786e\u6027\uff0c\u53ef\u4ee5\u52a0\u8f7d\u4fdd\u5b58\u7684npy\u6587\u4ef6\u5e76\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a0\u8f7dnpy\u6587\u4ef6<\/p>\n<p>loaded_data = np.load(&#39;your_npy_file.npy&#39;)<\/p>\n<h2><strong>\u9a8c\u8bc1\u6570\u636e\u662f\u5426\u4e00\u81f4<\/strong><\/h2>\n<p>print(np.array_equal(matrix_data, loaded_data))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/strong>\uff1a\u786e\u4fdd\u5b89\u88c5\u4e86SciPy\u548cNumPy\u3002<\/li>\n<li><strong>\u8bfb\u53d6mat\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528SciPy\u7684loadmat\u51fd\u6570\u3002<\/li>\n<li><strong>\u63d0\u53d6\u6570\u636e<\/strong>\uff1a\u4ece\u5b57\u5178\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u77e9\u9635\u6570\u636e\u3002<\/li>\n<li><strong>\u4fdd\u5b58\u4e3anpy\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528NumPy\u7684save\u51fd\u6570\u3002<\/li>\n<li><strong>\u9a8c\u8bc1\u6570\u636e<\/strong>\uff1a\u52a0\u8f7dnpy\u6587\u4ef6\u5e76\u68c0\u67e5\u6570\u636e\u662f\u5426\u4e00\u81f4\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06mat\u77e9\u9635\u8f6c\u6362\u4e3anpy\u683c\u5f0f\u3002\u5e0c\u671b\u8fd9\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06MAT\u6587\u4ef6\u4e2d\u7684\u77e9\u9635\u6570\u636e\u63d0\u53d6\u5e76\u4fdd\u5b58\u4e3a.npy\u683c\u5f0f\uff1f<\/strong><br \/>\u8981\u5c06MAT\u6587\u4ef6\u4e2d\u7684\u77e9\u9635\u6570\u636e\u63d0\u53d6\u5e76\u4fdd\u5b58\u4e3a.npy\u683c\u5f0f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>scipy.io<\/code>\u6a21\u5757\u7684<code>loadmat<\/code>\u51fd\u6570\u6765\u8bfb\u53d6MAT\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>numpy<\/code>\u7684<code>save<\/code>\u51fd\u6570\u6765\u4fdd\u5b58\u6570\u636e\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nfrom scipy.io import loadmat\n\n# \u8bfb\u53d6MAT\u6587\u4ef6\ndata = loadmat(&#39;your_file.mat&#39;)\n\n# \u63d0\u53d6\u6240\u9700\u7684\u77e9\u9635\nmatrix_data = data[&#39;your_matrix_variable&#39;]\n\n# \u4fdd\u5b58\u4e3a.npy\u6587\u4ef6\nnp.save(&#39;output_file.npy&#39;, matrix_data)\n<\/code><\/pre>\n<p><strong>\u5728\u5c06MAT\u77e9\u9635\u8f6c\u6362\u4e3a.npy\u683c\u5f0f\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848\uff1f<\/strong><br \/>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002\u4f8b\u5982\uff0cMAT\u6587\u4ef6\u4e2d\u53ef\u80fd\u5305\u542b\u591a\u4e2a\u53d8\u91cf\uff0c\u786e\u4fdd\u60a8\u63d0\u53d6\u6b63\u786e\u7684\u53d8\u91cf\u540d\u662f\u5173\u952e\u3002\u5982\u679c\u627e\u4e0d\u5230\u53d8\u91cf\uff0c\u901a\u5e38\u662f\u7531\u4e8e\u53d8\u91cf\u540d\u4e0d\u5339\u914d\u3002\u53e6\u4e00\u5e38\u89c1\u95ee\u9898\u662f\u5185\u5b58\u4e0d\u8db3\uff0c\u5c24\u5176\u662f\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\u3002\u53ef\u4ee5\u8003\u8651\u5206\u6279\u52a0\u8f7d\u548c\u4fdd\u5b58\u6570\u636e\uff0c\u6216\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p><strong>\u4f7f\u7528numpy\u4fdd\u5b58\u6570\u636e\u65f6\uff0c\u6709\u54ea\u4e9b\u6700\u4f73\u5b9e\u8df5\uff1f<\/strong><br \/>\u5728\u4f7f\u7528numpy\u4fdd\u5b58\u6570\u636e\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u538b\u7f29\u683c\u5f0f\u4ee5\u8282\u7701\u5b58\u50a8\u7a7a\u95f4\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728<code>np.save<\/code>\u51fd\u6570\u4e2d\u6307\u5b9a<code>.npz<\/code>\u683c\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4f8b\u5982\uff1a<code>np.savez_compressed(&#39;output_file.npz&#39;, matrix_data)<\/code>\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u5728\u4fdd\u5b58\u6570\u636e\u65f6\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4ee5\u4f18\u5316\u6027\u80fd\u548c\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u591a\u4e2aMAT\u6587\u4ef6\u4e2d\u7684\u77e9\u9635\u6570\u636e\u5408\u5e76\u5e76\u4fdd\u5b58\u4e3a\u5355\u4e2a.npy\u6587\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u9010\u4e2a\u8bfb\u53d6\u6bcf\u4e2aMAT\u6587\u4ef6\u4e2d\u7684\u77e9\u9635\u6570\u636e\uff0c\u5e76\u5c06\u5b83\u4eec\u5408\u5e76\u4e3a\u4e00\u4e2a\u5355\u4e00\u7684\u6570\u7ec4\u6765\u5b9e\u73b0\u3002\u4f7f\u7528<code>numpy<\/code>\u7684<code>concatenate<\/code>\u6216<code>stack<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5b8c\u6210\u6b64\u64cd\u4f5c\u3002\u786e\u4fdd\u6240\u6709\u77e9\u9635\u5728\u7ef4\u5ea6\u4e0a\u517c\u5bb9\uff0c\u4ee5\u4fbf\u8fdb\u884c\u5408\u5e76\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nfrom scipy.io import loadmat\n\n# \u5047\u8bbe\u6211\u4eec\u6709\u591a\u4e2aMAT\u6587\u4ef6\nmat_files = [&#39;file1.mat&#39;, &#39;file2.mat&#39;, &#39;file3.mat&#39;]\nall_matrices = []\n\nfor file in mat_files:\n    data = loadmat(file)\n    matrix_data = data[&#39;your_matrix_variable&#39;]\n    all_matrices.append(matrix_data)\n\n# \u5408\u5e76\u6240\u6709\u77e9\u9635\ncombined_matrix = np.concatenate(all_matrices, axis=0)\n\n# \u4fdd\u5b58\u4e3a.npy\u6587\u4ef6\nnp.save(&#39;combined_output.npy&#39;, combined_matrix)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u6b65\u5c06mat\u77e9\u9635\u8f6c\u6362\u4e3anpy\u683c\u5f0f\uff1a\u9996\u5148\u4f7f\u7528SciPy\u5e93\u4e2d\u7684loadmat\u51fd\u6570\u8bfb\u53d6mat [&hellip;]","protected":false},"author":3,"featured_media":1098159,"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\/1098153"}],"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=1098153"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098153\/revisions"}],"predecessor-version":[{"id":1098160,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098153\/revisions\/1098160"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098159"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}