{"id":1136630,"date":"2025-01-08T21:38:12","date_gmt":"2025-01-08T13:38:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1136630.html"},"modified":"2025-01-08T21:38:15","modified_gmt":"2025-01-08T13:38:15","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e4%b8%80%e5%88%97%e6%95%b0%e6%8d%ae%e8%bd%ac%e6%8d%a2%e4%b8%ba%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1136630.html","title":{"rendered":"python\u5982\u4f55\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100703\/cf75cb45-7493-4d83-9257-bf86625bfe14.webp\" alt=\"python\u5982\u4f55\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u77e9\u9635\" \/><\/p>\n<p><p> <strong>Python\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528Pandas\u5e93\u3001\u4ee5\u53ca\u624b\u52a8\u5b9e\u73b0\u77e9\u9635\u8f6c\u6362\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528NumPy\u5e93\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u77e9\u9635\u7684\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u5e93\u662fPython\u4e2d\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u3002\u4e0b\u9762\u4ee5NumPy\u4e3a\u4f8b\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001NumPy\u5e93\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><p>NumPy\uff08Numerical Python\uff09\u662fPython\u7684\u4e00\u4e2a\u6269\u5c55\u5e93\uff0c\u652f\u6301\u5927\u6570\u636e\u96c6\u7684\u9ad8\u6548\u64cd\u4f5c\u3002\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6027\u80fd\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\uff0c\u4ee5\u53ca\u7528\u4e8e\u64cd\u4f5c\u8fd9\u4e9b\u6570\u7ec4\u7684\u591a\u79cd\u51fd\u6570\u3002NumPy\u662f\u8bb8\u591a\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4efb\u52a1\u7684\u57fa\u7840\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1\u3001NumPy\u5b89\u88c5\u4e0e\u5bfc\u5165<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528NumPy\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165NumPy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001NumPy\u6570\u7ec4\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>NumPy\u7684\u6838\u5fc3\u662f\u5176\u63d0\u4f9b\u7684ndarray\u5bf9\u8c61\uff08N-dimensional array\uff09\uff0c\u5b83\u662f\u4e00\u4e2a\u591a\u7ef4\u6570\u7ec4\uff0c\u652f\u6301\u9ad8\u6548\u7684\u5411\u91cf\u548c\u77e9\u9635\u8fd0\u7b97\u3002ndarray\u5bf9\u8c61\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u521b\u5efa\uff0c\u5982\u4ece\u5217\u8868\u3001\u5143\u7ec4\u3001\u6216\u5176\u4ed6\u6570\u7ec4\u4e2d\u521b\u5efa\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u77e9\u9635\u7684\u8fc7\u7a0b\uff0c\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u521b\u5efa\u4e00\u5217\u6570\u636e\u3001\u5c06\u5176\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3001\u8c03\u6574\u6570\u7ec4\u5f62\u72b6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u4e00\u5217\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u5217\u6570\u636e\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u4e00\u5217\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528NumPy\u7684array\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_array = np.array(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65f6\uff0cdata_array\u662f\u4e00\u4e2a\u4e00\u7ef4\u7684NumPy\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u8c03\u6574\u6570\u7ec4\u5f62\u72b6<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u77e9\u9635\uff0c\u9700\u8981\u8c03\u6574\u6570\u7ec4\u7684\u5f62\u72b6\u3002\u53ef\u4ee5\u4f7f\u7528NumPy\u7684reshape\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u5047\u8bbe\u6211\u4eec\u5e0c\u671b\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a3\u884c3\u5217\u7684\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = data_array.reshape(3, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65f6\uff0cmatrix\u662f\u4e00\u4e2a3\u884c3\u5217\u7684\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array([[1, 2, 3],<\/p>\n<p>       [4, 5, 6],<\/p>\n<p>       [7, 8, 9]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5b9e\u4f8b\u6f14\u793a<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u76f4\u89c2\u5730\u5c55\u793a\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u77e9\u9635\u7684\u8fc7\u7a0b\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u5b9e\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u5217\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<h2><strong>\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>data_array = np.array(data)<\/p>\n<h2><strong>\u8c03\u6574\u6570\u7ec4\u5f62\u72b6\uff0c\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a3\u884c3\u5217\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = data_array.reshape(3, 3)<\/p>\n<h2><strong>\u6253\u5370\u77e9\u9635<\/strong><\/h2>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u5c06\u8f93\u51fa\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">[[1 2 3]<\/p>\n<p> [4 5 6]<\/p>\n<p> [7 8 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u957f\u5ea6\u4e0e\u77e9\u9635\u5f62\u72b6<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528reshape\u51fd\u6570\u8c03\u6574\u6570\u7ec4\u5f62\u72b6\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u6570\u636e\u7684\u957f\u5ea6\u4e0e\u76ee\u6807\u77e9\u9635\u7684\u5f62\u72b6\u5339\u914d\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6570\u636e\u7684\u957f\u5ea6\u662f9\uff0c\u76ee\u6807\u77e9\u9635\u7684\u5f62\u72b6\u53ef\u4ee5\u662f(3, 3)\u6216(9, 1)\u7b49\u3002\u5982\u679c\u6570\u636e\u7684\u957f\u5ea6\u4e0e\u76ee\u6807\u77e9\u9635\u7684\u5f62\u72b6\u4e0d\u5339\u914d\uff0c\u5c06\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9519\u8bef\u793a\u4f8b\uff1a\u6570\u636e\u957f\u5ea6\u4e0e\u77e9\u9635\u5f62\u72b6\u4e0d\u5339\u914d<\/p>\n<p>data = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>data_array = np.array(data)<\/p>\n<h2><strong>\u5c1d\u8bd5\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a4\u884c3\u5217\u7684\u77e9\u9635\uff0c\u5c06\u5f15\u53d1\u9519\u8bef<\/strong><\/h2>\n<p>matrix = data_array.reshape(4, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u5f15\u53d1\u4ee5\u4e0b\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ValueError: cannot reshape array of size 9 into shape (4,3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u7f3a\u5931\u503c<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u6570\u636e\u5904\u7406\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f3a\u5931\u503c\u7684\u60c5\u51b5\u3002\u53ef\u4ee5\u4f7f\u7528NumPy\u7684nan\u5904\u7406\u51fd\u6570\u6765\u5904\u7406\u7f3a\u5931\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528np.nan\u8868\u793a\u7f3a\u5931\u503c\uff0c\u5e76\u5728\u8f6c\u6362\u4e3a\u77e9\u9635\u65f6\u5bf9\u5176\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u5305\u542b\u7f3a\u5931\u503c\u7684\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, np.nan, 4, 5, 6, 7, 8, 9]<\/p>\n<h2><strong>\u5c06\u6570\u636e\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>data_array = np.array(data)<\/p>\n<h2><strong>\u586b\u5145\u7f3a\u5931\u503c\uff08\u4f8b\u5982\uff0c\u75280\u586b\u5145\uff09<\/strong><\/h2>\n<p>data_array = np.nan_to_num(data_array, nan=0)<\/p>\n<h2><strong>\u8c03\u6574\u6570\u7ec4\u5f62\u72b6\uff0c\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a3\u884c3\u5217\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = data_array.reshape(3, 3)<\/p>\n<h2><strong>\u6253\u5370\u77e9\u9635<\/strong><\/h2>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u5c06\u8f93\u51fa\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">[[1. 2. 0.]<\/p>\n<p> [4. 5. 6.]<\/p>\n<p> [7. 8. 9.]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8fdb\u9636\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1\u3001\u968f\u673a\u751f\u6210\u6570\u636e\u5e76\u8f6c\u6362\u4e3a\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u968f\u673a\u751f\u6210\u6570\u636e\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u77e9\u9635\u3002\u53ef\u4ee5\u4f7f\u7528NumPy\u7684random\u6a21\u5757\u6765\u751f\u6210\u968f\u673a\u6570\u636e\uff0c\u5e76\u6309\u7167\u4e0a\u8ff0\u6b65\u9aa4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u968f\u673a\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.randint(1, 100, size=9)<\/p>\n<h2><strong>\u8c03\u6574\u6570\u7ec4\u5f62\u72b6\uff0c\u5c06\u968f\u673a\u6570\u636e\u8f6c\u6362\u4e3a3\u884c3\u5217\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = data.reshape(3, 3)<\/p>\n<h2><strong>\u6253\u5370\u77e9\u9635<\/strong><\/h2>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u5c06\u8f93\u51fa\u4e00\u4e2a\u968f\u673a\u751f\u6210\u76843\u884c3\u5217\u7684\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">[[53 12 77]<\/p>\n<p> [68 45 89]<\/p>\n<p> [34 23 91]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u5e76\u8f6c\u6362\u4e3a\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u901a\u5e38\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528NumPy\u7684loadtxt\u6216genfromtxt\u51fd\u6570\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u5e76\u8f6c\u6362\u4e3a\u77e9\u9635\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3adata.txt\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-plaintext\">1 2 3<\/p>\n<p>4 5 6<\/p>\n<p>7 8 9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u8bfb\u53d6\u4e3a\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = np.loadtxt(&#39;data.txt&#39;)<\/p>\n<h2><strong>\u6253\u5370\u77e9\u9635<\/strong><\/h2>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u5c06\u8f93\u51fa\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">[[1. 2. 3.]<\/p>\n<p> [4. 5. 6.]<\/p>\n<p> [7. 8. 9.]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u77e9\u9635\u662f\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\u7684\u5e38\u89c1\u64cd\u4f5c\u3002\u901a\u8fc7\u4f7f\u7528NumPy\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u521b\u5efa\u6570\u636e\u3001\u5c06\u6570\u636e\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3001\u8c03\u6574\u6570\u7ec4\u5f62\u72b6\u3002\u9700\u8981\u6ce8\u610f\u6570\u636e\u957f\u5ea6\u4e0e\u77e9\u9635\u5f62\u72b6\u7684\u5339\u914d\uff0c\u4ee5\u53ca\u5904\u7406\u7f3a\u5931\u503c\u7b49\u95ee\u9898\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u57fa\u672c\u64cd\u4f5c\uff0c\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0cNumPy\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u51fd\u6570\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u7b80\u5316\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4efb\u52a1\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528NumPy\u4e0e\u5176\u4ed6\u6570\u636e\u5904\u7406\u5e93\uff08\u5982Pandas\uff09\u6765\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u77e9\u9635\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5NumPy\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528<code>numpy.reshape()<\/code>\u65b9\u6cd5\u5c06\u4e00\u7ef4\u6570\u7ec4\u91cd\u5851\u4e3a\u6240\u9700\u7684\u884c\u548c\u5217\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5305\u542b10\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>array.reshape(2, 5)<\/code>\u6765\u5c06\u5176\u8f6c\u6362\u4e3a2\u884c5\u5217\u7684\u77e9\u9635\u3002\u8bb0\u5f97\u5728\u91cd\u5851\u65f6\uff0c\u5143\u7d20\u603b\u6570\u5fc5\u987b\u5339\u914d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u77e9\u9635\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u6709\u4e00\u4e2aPython\u5217\u8868\uff0c\u60f3\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u7684<code>array()<\/code>\u51fd\u6570\u3002\u5c06\u5217\u8868\u4f20\u9012\u7ed9<code>numpy.array()<\/code>\uff0c\u5c31\u80fd\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4\u3002\u5982\u679c\u9700\u8981\u7279\u5b9a\u7684\u5f62\u72b6\uff0c\u53ef\u4ee5\u7ed3\u5408<code>reshape()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u5217\u8868\u6570\u636e\u5e76\u5c06\u5176\u8f6c\u5316\u4e3a\u77e9\u9635\u683c\u5f0f\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u8ba1\u7b97\u548c\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u4f7f\u7528Pandas\u5982\u4f55\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u77e9\u9635\u683c\u5f0f\uff1f<\/strong><br \/>\u4f7f\u7528Pandas\u5e93\u540c\u6837\u53ef\u4ee5\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u77e9\u9635\u683c\u5f0f\u3002\u9996\u5148\uff0c\u5c06\u6570\u636e\u653e\u5165\u4e00\u4e2aPandas DataFrame\u4e2d\uff0c\u7136\u540e\u53ef\u4ee5\u4f7f\u7528<code>values<\/code>\u5c5e\u6027\u63d0\u53d6\u6570\u636e\u5e76\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u901a\u8fc7<code>reshape()<\/code>\u65b9\u6cd5\u8c03\u6574\u5f62\u72b6\u3002\u5982\u679c\u4f60\u5e0c\u671b\u4fdd\u7559\u6570\u636e\u7ed3\u6784\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>DataFrame.to_numpy()<\/code>\u5c06\u5176\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u9879\u76ee\u4e2d\u975e\u5e38\u5b9e\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528Pandas\u5e93\u3001\u4ee5\u53ca\u624b\u52a8\u5b9e\u73b0\u77e9\u9635 [&hellip;]","protected":false},"author":3,"featured_media":1136636,"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\/1136630"}],"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=1136630"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136630\/revisions"}],"predecessor-version":[{"id":1136637,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136630\/revisions\/1136637"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1136636"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1136630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1136630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1136630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}