{"id":927092,"date":"2024-12-26T16:01:11","date_gmt":"2024-12-26T08:01:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/927092.html"},"modified":"2024-12-26T16:01:13","modified_gmt":"2024-12-26T08:01:13","slug":"python%e5%a6%82%e4%bd%95reshape","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/927092.html","title":{"rendered":"python\u5982\u4f55reshape"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063410\/e6b0b467-5f76-4488-95ec-6cdb4d1e689f.webp\" alt=\"python\u5982\u4f55reshape\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u6570\u7ec4\u8fdb\u884creshape\u64cd\u4f5c\u3002\u8981\u8fdb\u884creshape\u64cd\u4f5c\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165NumPy\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>reshape<\/code>\u51fd\u6570\u6765\u6539\u53d8\u6570\u7ec4\u7684\u5f62\u72b6\u3002NumPy\u7684<code>reshape<\/code>\u51fd\u6570\u53ef\u4ee5\u8ba9\u4f60\u91cd\u65b0\u5b9a\u4e49\u6570\u7ec4\u7684\u5f62\u72b6\uff0c\u800c\u4e0d\u6539\u53d8\u6570\u636e\u7684\u987a\u5e8f\u3002<\/strong>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c<code>reshape<\/code>\u51fd\u6570\u7528\u4e8e\u8c03\u6574\u591a\u7ef4\u6570\u7ec4\u7684\u5f62\u72b6\uff0c\u4f7f\u5176\u66f4\u7b26\u5408\u6570\u636e\u5904\u7406\u7684\u9700\u8981\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528NumPy\u8fdb\u884creshape\u64cd\u4f5c\uff0c\u91cd\u70b9\u5305\u62ecreshape\u7684\u57fa\u672c\u7528\u6cd5\u3001\u5e38\u89c1\u9519\u8bef\u53ca\u5176\u89e3\u51b3\u65b9\u6848\u3001\u4ee5\u53ca\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001NUMPY\u5e93\u4e0eRESHAPE\u51fd\u6570<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u4e3a\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u63d0\u4f9b\u4e86\u652f\u6301\u3002<code>reshape<\/code>\u51fd\u6570\u662fNumPy\u5e93\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u529f\u80fd\uff0c\u5141\u8bb8\u7528\u6237\u5728\u4e0d\u6539\u53d8\u6570\u636e\u5185\u5bb9\u7684\u524d\u63d0\u4e0b\u6539\u53d8\u6570\u7ec4\u7684\u5f62\u72b6\u3002<code>reshape<\/code>\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>a = np.array([1, 2, 3, 4, 5, 6])<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u6570\u7ec4\u91cd\u5851\u4e3a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>b = a.reshape(2, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6570\u7ec4<code>a<\/code>\u88ab\u91cd\u5851\u4e3a\u4e00\u4e2a2\u884c3\u5217\u7684\u4e8c\u7ef4\u6570\u7ec4<code>b<\/code>\u3002<code>reshape<\/code>\u51fd\u6570\u7684\u53c2\u6570\u51b3\u5b9a\u4e86\u76ee\u6807\u6570\u7ec4\u7684\u5f62\u72b6\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001RESHAPE\u51fd\u6570\u7684\u5e38\u89c1\u7528\u6cd5<\/p>\n<\/p>\n<p><p>1\u3001\u8c03\u6574\u6570\u7ec4\u7684\u7ef4\u5ea6<\/p>\n<\/p>\n<p><p><code>reshape<\/code>\u51fd\u6570\u6700\u5e38\u89c1\u7684\u7528\u6cd5\u662f\u6539\u53d8\u6570\u7ec4\u7684\u7ef4\u5ea6\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u6216\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e09\u7ef4\u6570\u7ec4\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0creshape\u64cd\u4f5c\u5fc5\u987b\u4fdd\u8bc1\u5143\u7d20\u603b\u6570\u4e0d\u53d8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/p>\n<p>a = np.array([1, 2, 3, 4, 5, 6, 7, 8])<\/p>\n<h2><strong>\u91cd\u5851\u4e3a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>b = a.reshape(2, 4)<\/p>\n<h2><strong>\u91cd\u5851\u4e3a\u4e09\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>c = a.reshape(2, 2, 2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528-1\u81ea\u52a8\u63a8\u65ad\u7ef4\u5ea6<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>reshape<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528-1\u4f5c\u4e3a\u53c2\u6570\u4e4b\u4e00\uff0c\u8ba9NumPy\u81ea\u52a8\u8ba1\u7b97\u8be5\u7ef4\u5ea6\u7684\u5927\u5c0f\u3002\u4f7f\u7528-1\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u7ef4\u5ea6\u8c03\u6574\uff0c\u800c\u4e0d\u9700\u8981\u624b\u52a8\u8ba1\u7b97\u67d0\u4e2a\u7ef4\u5ea6\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/p>\n<p>a = np.array([1, 2, 3, 4, 5, 6, 7, 8])<\/p>\n<h2><strong>\u4f7f\u7528-1\u81ea\u52a8\u63a8\u65ad\u5217\u6570<\/strong><\/h2>\n<p>b = a.reshape(2, -1)<\/p>\n<h2><strong>\u4f7f\u7528-1\u81ea\u52a8\u63a8\u65ad\u884c\u6570<\/strong><\/h2>\n<p>c = a.reshape(-1, 4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5e38\u89c1\u9519\u8bef\u53ca\u5176\u89e3\u51b3\u65b9\u6848<\/p>\n<\/p>\n<p><p>1\u3001\u4e0d\u80fd\u6539\u53d8\u5143\u7d20\u603b\u6570<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>reshape<\/code>\u51fd\u6570\u65f6\uff0c\u6700\u5e38\u89c1\u7684\u9519\u8bef\u662f\u8bd5\u56fe\u6539\u53d8\u6570\u7ec4\u7684\u5143\u7d20\u603b\u6570\u3002<code>reshape<\/code>\u64cd\u4f5c\u8981\u6c42\u8f93\u5165\u548c\u8f93\u51fa\u7684\u5143\u7d20\u603b\u6570\u76f8\u540c\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/p>\n<p>a = np.array([1, 2, 3, 4, 5, 6])<\/p>\n<h2><strong>\u5c1d\u8bd5\u91cd\u5851\u4e3a3x3\u7684\u6570\u7ec4\uff08\u9519\u8bef\uff09<\/strong><\/h2>\n<p>try:<\/p>\n<p>    b = a.reshape(3, 3)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(&quot;\u9519\u8bef:&quot;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u89e3\u51b3\u65b9\u6848\u662f\u786e\u4fdd\u65b0\u7684\u5f62\u72b6\u4e0e\u539f\u59cb\u6570\u7ec4\u7684\u5143\u7d20\u603b\u6570\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u5f62\u72b6\u53c2\u6570\u4e0d\u6b63\u786e<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>reshape<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u56e0\u4e3a\u4e0d\u6b63\u786e\u7684\u5f62\u72b6\u53c2\u6570\u800c\u5bfc\u81f4\u9519\u8bef\u3002\u8fd9\u901a\u5e38\u53d1\u751f\u5728\u624b\u52a8\u8ba1\u7b97\u7ef4\u5ea6\u65f6\u51fa\u9519\u3002\u4f7f\u7528-1\u53ef\u4ee5\u907f\u514d\u6b64\u7c7b\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/p>\n<p>a = np.array([1, 2, 3, 4, 5, 6])<\/p>\n<h2><strong>\u5c1d\u8bd5\u4f7f\u7528\u4e0d\u6b63\u786e\u7684\u5f62\u72b6\u53c2\u6570\uff08\u9519\u8bef\uff09<\/strong><\/h2>\n<p>try:<\/p>\n<p>    b = a.reshape(2, 2, 2)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(&quot;\u9519\u8bef:&quot;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528-1\uff0cNumPy\u4f1a\u81ea\u52a8\u8ba1\u7b97\u6b63\u786e\u7684\u5f62\u72b6\u53c2\u6570\uff0c\u907f\u514d\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u9ad8\u7ea7\u7528\u6cd5<\/p>\n<\/p>\n<p><p>1\u3001\u7ed3\u5408\u5207\u7247\u4e0ereshape<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u5bf9\u6570\u7ec4\u8fdb\u884c\u5207\u7247\uff0c\u5e76\u5728\u6b64\u57fa\u7840\u4e0a\u8fdb\u884creshape\u64cd\u4f5c\u3002\u7ed3\u5408\u5207\u7247\u4e0ereshape\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])<\/p>\n<h2><strong>\u5bf9\u6570\u7ec4\u8fdb\u884c\u5207\u7247<\/strong><\/h2>\n<p>sliced = a[:, :2]<\/p>\n<h2><strong>\u5bf9\u5207\u7247\u540e\u7684\u6570\u7ec4\u8fdb\u884c\u91cd\u5851<\/strong><\/h2>\n<p>reshaped = sliced.reshape(4, 1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u7684\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0creshape\u64cd\u4f5c\u7ecf\u5e38\u7528\u4e8e\u8c03\u6574\u6570\u636e\u7684\u5f62\u72b6\u4ee5\u9002\u5e94\u6a21\u578b\u7684\u8f93\u5165\u8981\u6c42\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u56fe\u50cf\u6570\u636e\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u4e8c\u7ef4\u56fe\u50cf\u5c55\u5e73\u4e3a\u4e00\u7ef4\u5411\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u6a21\u62df\u7684\u56fe\u50cf\u6570\u7ec4\uff083x3\uff09<\/p>\n<p>image = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])<\/p>\n<h2><strong>\u5c06\u4e8c\u7ef4\u56fe\u50cf\u5c55\u5e73\u4e3a\u4e00\u7ef4\u5411\u91cf<\/strong><\/h2>\n<p>flattened = image.reshape(-1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5904\u7406\u65f6\uff0c<code>reshape<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528<code>reshape<\/code>\uff0c\u53ef\u4ee5\u5728\u4e0d\u6539\u53d8\u6570\u636e\u5185\u5bb9\u7684\u524d\u63d0\u4e0b\u7075\u6d3b\u8c03\u6574\u6570\u7ec4\u7684\u5f62\u72b6\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u5e94\u7528\u9700\u6c42\u3002\u5728\u4f7f\u7528<code>reshape<\/code>\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u8f93\u5165\u548c\u8f93\u51fa\u7684\u5143\u7d20\u603b\u6570\u4e00\u81f4\uff0c\u5e76\u719f\u7ec3\u638c\u63e1-1\u7684\u4f7f\u7528\u6280\u5de7\uff0c\u4ee5\u907f\u514d\u624b\u52a8\u8ba1\u7b97\u7ef4\u5ea6\u5e26\u6765\u7684\u9519\u8bef\u3002\u901a\u8fc7\u7ed3\u5408\u5176\u4ed6NumPy\u529f\u80fd\uff0c\u5982\u5207\u7247\u3001\u7d22\u5f15\u7b49\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u4e3a\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728Python\u4e2d\u4f7f\u7528<code>reshape<\/code>\u51fd\u6570\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u6570\u7ec4\u91cd\u5851\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6570\u7ec4\u91cd\u5851\u901a\u5e38\u4f7f\u7528NumPy\u5e93\u6765\u5b9e\u73b0\u3002\u901a\u8fc7NumPy\u7684<code>reshape<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u6539\u53d8\u6570\u7ec4\u7684\u5f62\u72b6\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>numpy.reshape(array, new_shape)<\/code>\u6765\u5c06\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u3002\u786e\u4fdd\u65b0\u5f62\u72b6\u7684\u603b\u5143\u7d20\u6570\u91cf\u4e0e\u539f\u6570\u7ec4\u76f8\u540c\u3002<\/p>\n<p><strong>\u91cd\u5851\u6570\u7ec4\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6570\u7ec4\u91cd\u5851\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u91cd\u5851\u540e\u7684\u5f62\u72b6\u4e0e\u539f\u59cb\u6570\u7ec4\u7684\u5143\u7d20\u6570\u91cf\u76f8\u5339\u914d\u3002\u5982\u679c\u539f\u6570\u7ec4\u670910\u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u91cd\u5851\u540e\u7684\u5f62\u72b6\u603b\u5143\u7d20\u6570\u91cf\u4e5f\u5fc5\u987b\u4e3a10\u3002\u6b64\u5916\uff0c\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>-1<\/code>\u4f5c\u4e3a\u7ef4\u5ea6\u53c2\u6570\uff0cNumPy\u4f1a\u81ea\u52a8\u8ba1\u7b97\u8be5\u7ef4\u5ea6\u7684\u5927\u5c0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u65e0\u6cd5\u91cd\u5851\u7684\u6570\u7ec4\uff1f<\/strong><br \/>\u5982\u679c\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u6570\u7ec4\u91cd\u5851\u4e3a\u4e00\u4e2a\u4e0d\u517c\u5bb9\u7684\u5f62\u72b6\uff0cNumPy\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u68c0\u67e5\u539f\u6570\u7ec4\u7684\u5f62\u72b6\u548c\u5143\u7d20\u6570\u91cf\uff0c\u786e\u4fdd\u5b83\u4eec\u4e0e\u76ee\u6807\u5f62\u72b6\u76f8\u7b26\u3002\u5982\u679c\u4ecd\u7136\u65e0\u6cd5\u91cd\u5851\uff0c\u53ef\u80fd\u9700\u8981\u8003\u8651\u5176\u4ed6\u6570\u636e\u5904\u7406\u7684\u65b9\u6cd5\uff0c\u6bd4\u5982\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\uff0c\u6216\u8005\u4f7f\u7528<code>resize<\/code>\u65b9\u6cd5\u6765\u8c03\u6574\u6570\u7ec4\u7684\u5f62\u72b6\u548c\u5927\u5c0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u6570\u7ec4\u8fdb\u884creshape\u64cd\u4f5c\u3002\u8981\u8fdb\u884creshape\u64cd\u4f5c\uff0c\u9996\u5148\u9700\u8981\u5bfc [&hellip;]","protected":false},"author":3,"featured_media":927097,"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\/927092"}],"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=927092"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/927092\/revisions"}],"predecessor-version":[{"id":927098,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/927092\/revisions\/927098"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/927097"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=927092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=927092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=927092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}