{"id":1052296,"date":"2024-12-31T14:23:49","date_gmt":"2024-12-31T06:23:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1052296.html"},"modified":"2024-12-31T14:23:53","modified_gmt":"2024-12-31T06:23:53","slug":"python%e7%9a%84%e6%95%b0%e6%8d%ae%e5%a6%82%e4%bd%95%e8%bd%ac%e5%8c%96%e6%88%90%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1052296.html","title":{"rendered":"python\u7684\u6570\u636e\u5982\u4f55\u8f6c\u5316\u6210\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/3750ad22-7061-4dab-988b-494f9e132c89.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u7684\u6570\u636e\u5982\u4f55\u8f6c\u5316\u6210\u77e9\u9635\" \/><\/p>\n<p><p> <strong>Python\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7NumPy\u6570\u7ec4\u3001\u5217\u8868\u5d4c\u5957\u3001Pandas DataFrame\u3001SciPy\u7a00\u758f\u77e9\u9635\u7b49\u65b9\u5f0f\u8f6c\u5316\u6210\u77e9\u9635\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001Pandas\u5e93\u3001SciPy\u5e93\u7b49\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u6570\u636e\u8f6c\u5316\u3002<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u5e93\uff0c\u7528\u4e8e\u5904\u7406\u5927\u578b\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u5e93\u548c\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\uff0c\u662f\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u5e38\u7528\u7684\u5de5\u5177\u4e4b\u4e00\u3002\u4f7f\u7528NumPy\u5e93\u5c06\u6570\u636e\u8f6c\u5316\u4e3a\u77e9\u9635\u7684\u8fc7\u7a0b\u76f8\u5bf9\u7b80\u5355\u4e14\u9ad8\u6548\uff0c\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NumPy\u5e93\u5c06\u5217\u8868\u8f6c\u5316\u6210\u77e9\u9635<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5NumPy\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528NumPy\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\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><h4>2\u3001\u5bfc\u5165NumPy\u5e93\u5e76\u521b\u5efa\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5bfc\u5165NumPy\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u5217\u8868\u3002\u5217\u8868\u53ef\u4ee5\u662f\u5d4c\u5957\u5217\u8868\u7684\u5f62\u5f0f\uff0c\u8fd9\u6837\u6bcf\u4e2a\u5b50\u5217\u8868\u5c31\u4ee3\u8868\u77e9\u9635\u4e2d\u7684\u4e00\u884c\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u5d4c\u5957\u5217\u8868<\/strong><\/h2>\n<p>data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5c06\u5217\u8868\u8f6c\u5316\u4e3aNumPy\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u7684<code>array<\/code>\u51fd\u6570\u5c06\u5d4c\u5957\u5217\u8868\u8f6c\u5316\u4e3a\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = np.array(data)<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>[[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>\u6b64\u65f6\uff0c\u5217\u8868\u5df2\u7ecf\u6210\u529f\u8f6c\u5316\u4e3aNumPy\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pandas\u5e93\u5c06DataFrame\u8f6c\u5316\u6210\u77e9\u9635<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u9700\u8981\u5148\u5b89\u88c5Pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bfc\u5165Pandas\u5e93\u5e76\u521b\u5efaDataFrame<\/h4>\n<\/p>\n<p><p>\u5bfc\u5165Pandas\u5e93\u540e\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aDataFrame\u3002DataFrame\u662f\u4e00\u79cd\u4e8c\u7ef4\u7684\u3001\u5e26\u6807\u7b7e\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8eExcel\u8868\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6],<\/p>\n<p>    &#39;C&#39;: [7, 8, 9]<\/p>\n<p>})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5c06DataFrame\u8f6c\u5316\u4e3aNumPy\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u7684<code>values<\/code>\u5c5e\u6027\u5c06DataFrame\u8f6c\u5316\u4e3aNumPy\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = df.values<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1 4 7]<\/p>\n<p> [2 5 8]<\/p>\n<p> [3 6 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528SciPy\u5e93\u521b\u5efa\u7a00\u758f\u77e9\u9635<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5SciPy\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5SciPy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scipy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bfc\u5165SciPy\u5e93\u5e76\u521b\u5efa\u7a00\u758f\u77e9\u9635<\/h4>\n<\/p>\n<p><p>SciPy\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u7c7b\u578b\u7684\u7a00\u758f\u77e9\u9635\uff0c\u5e38\u7528\u7684\u6709CSR\uff08Compressed Sparse Row\uff09\u548cCSC\uff08Compressed Sparse Column\uff09\u7b49\u3002\u4ee5\u4e0b\u662f\u521b\u5efaCSR\u7a00\u758f\u77e9\u9635\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.sparse import csr_matrix<\/p>\n<h2><strong>\u521b\u5efa\u7a00\u758f\u77e9\u9635<\/strong><\/h2>\n<p>data = [1, 2, 3, 4]<\/p>\n<p>row_indices = [0, 0, 1, 2]<\/p>\n<p>col_indices = [0, 2, 2, 0]<\/p>\n<p>sparse_matrix = csr_matrix((data, (row_indices, col_indices)), shape=(3, 3))<\/p>\n<p>print(sparse_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>  (0, 0)    1<\/p>\n<p>  (0, 2)    2<\/p>\n<p>  (1, 2)    3<\/p>\n<p>  (2, 0)    4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3\u4e0e\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>NumPy\u5e93\u9002\u7528\u4e8e\u5904\u7406\u5e38\u89c4\u77e9\u9635\u64cd\u4f5c\uff0c<\/strong>\u5982\u52a0\u51cf\u4e58\u9664\u3001\u77e9\u9635\u4e58\u6cd5\u3001\u6c42\u9006\u7b49\u3002\u5176\u64cd\u4f5c\u7b80\u5355\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p><strong>Pandas\u5e93\u66f4\u9002\u7528\u4e8e\u5904\u7406\u5e26\u6709\u6807\u7b7e\u7684\u6570\u636e\uff0c<\/strong>\u5982\u8868\u683c\u6570\u636e\u548c\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002Pandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\uff0c\u5982\u6570\u636e\u8fc7\u6ee4\u3001\u5206\u7ec4\u3001\u805a\u5408\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>SciPy\u5e93\u5219\u9002\u7528\u4e8e\u5904\u7406\u7a00\u758f\u77e9\u9635\uff0c<\/strong>\u5373\u5927\u90e8\u5206\u5143\u7d20\u4e3a\u96f6\u7684\u77e9\u9635\u3002\u5728\u673a\u5668\u5b66\u4e60\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u7a00\u758f\u77e9\u9635\u5e38\u7528\u4e8e\u8868\u793a\u7a00\u758f\u6570\u636e\uff0c\u5982\u6587\u672c\u6570\u636e\u7684\u8bcd\u9891\u77e9\u9635\u548c\u56fe\u6570\u636e\u7684\u90bb\u63a5\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0c\u5c06Python\u4e2d\u7684\u6570\u636e\u8f6c\u5316\u4e3a\u77e9\u9635\uff0c\u8fdb\u800c\u8fdb\u884c\u5404\u79cd\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u64cd\u4f5c\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u6570\u636e\u5904\u7406\u7684\u6548\u7387\uff0c\u8fd8\u6269\u5c55\u4e86Python\u5728\u6570\u636e\u79d1\u5b66\u548c\u673a\u5668\u5b66\u4e60\u9886\u57df\u7684\u5e94\u7528\u8303\u56f4\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u4e2d\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u5c06\u5d4c\u5957\u5217\u8868\u8f6c\u6362\u4e3a\u77e9\u9635\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>numpy.array()<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np  \nlist_data = [[1, 2, 3], [4, 5, 6]]  \nmatrix = np.array(list_data)  \n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>matrix<\/code>\u5c31\u6210\u4e3a\u4e86\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u60a8\u53ef\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u8fdb\u884c\u66f4\u591a\u7684\u77e9\u9635\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u77e9\u9635\uff1f<\/strong><br \/>\u9664\u4e86NumPy\uff0cPandas\u5e93\u4e5f\u63d0\u4f9b\u4e86\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u77e9\u9635\u7684\u529f\u80fd\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7<code>DataFrame<\/code>\u5bf9\u8c61\u7684<code>values<\/code>\u5c5e\u6027\u5c06\u6570\u636e\u8f6c\u5316\u4e3aNumPy\u6570\u7ec4\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd  \ndata = pd.DataFrame([[1, 2, 3], [4, 5, 6]])  \nmatrix = data.values  \n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2aNumPy\u6570\u7ec4\uff0c\u53ef\u4ee5\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u975e\u6570\u503c\u578b\u6570\u636e\u7684\u77e9\u9635\u8f6c\u6362\uff1f<\/strong><br \/>\u5728\u5904\u7406\u975e\u6570\u503c\u578b\u6570\u636e\u65f6\uff0c\u8f6c\u6362\u8fc7\u7a0b\u53ef\u80fd\u4f1a\u6d89\u53ca\u5230\u7f16\u7801\u6216\u586b\u5145\u7f3a\u5931\u503c\u7b49\u6b65\u9aa4\u3002\u4f7f\u7528Pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u8fd9\u7c7b\u6570\u636e\u3002\u901a\u8fc7<code>DataFrame<\/code>\uff0c\u60a8\u53ef\u4ee5\u5c06\u5206\u7c7b\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u503c\u578b\u6570\u636e\uff0c\u4f7f\u7528<code>get_dummies()<\/code>\u51fd\u6570\u5e2e\u52a9\u5c06\u5206\u7c7b\u53d8\u91cf\u8f6c\u6362\u4e3a\u865a\u62df\u53d8\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd  \ndata = pd.DataFrame({&#39;Category&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;A&#39;, &#39;C&#39;]})  \nmatrix = pd.get_dummies(data)  \n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u4e86\u975e\u6570\u503c\u578b\u6570\u636e\u53ef\u4ee5\u987a\u5229\u8f6c\u5316\u4e3a\u77e9\u9635\uff0c\u9002\u5408\u540e\u7eed\u7684\u5206\u6790\u548c\u5efa\u6a21\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7NumPy\u6570\u7ec4\u3001\u5217\u8868\u5d4c\u5957\u3001Pandas DataFrame\u3001SciPy\u7a00\u758f\u77e9\u9635\u7b49\u65b9\u5f0f [&hellip;]","protected":false},"author":3,"featured_media":1052312,"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\/1052296"}],"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=1052296"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1052296\/revisions"}],"predecessor-version":[{"id":1052313,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1052296\/revisions\/1052313"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1052312"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1052296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1052296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1052296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}