{"id":1099143,"date":"2025-01-08T15:27:45","date_gmt":"2025-01-08T07:27:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1099143.html"},"modified":"2025-01-08T15:27:52","modified_gmt":"2025-01-08T07:27:52","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e4%b8%80%e4%b8%aa%e7%9f%a9%e9%98%b5-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1099143.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8868\u793a\u4e00\u4e2a\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063021\/62807902-0463-448e-9111-8171f0c7b6d4.webp\" alt=\"python\u4e2d\u5982\u4f55\u8868\u793a\u4e00\u4e2a\u77e9\u9635\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8868\u793a\u4e00\u4e2a\u77e9\u9635\u6709\u591a\u79cd\u65b9\u6cd5\uff0c<strong>\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528Pandas\u5e93\u3001\u4f7f\u7528SciPy\u5e93<\/strong>\u3002\u5176\u4e2d\u6700\u5e38\u7528\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5\u662f\u4f7f\u7528NumPy\u5e93\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u64cd\u4f5c\u51fd\u6570\u548c\u9ad8\u6548\u7684\u8ba1\u7b97\u6027\u80fd\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5d4c\u5957\u5217\u8868\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u8868\u793a\u77e9\u9635\u7684\u65b9\u6cd5\u3002\u4e00\u4e2a\u77e9\u9635\u53ef\u4ee5\u770b\u4f5c\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5217\u8868\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5b50\u5217\u8868\u4ee3\u8868\u77e9\u9635\u7684\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5d4c\u5957\u5217\u8868\u8868\u793a\u4e00\u4e2a3x3\u7684\u77e9\u9635<\/p>\n<p>matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u4f46\u4e0d\u9002\u5408\u8fdb\u884c\u590d\u6742\u7684\u77e9\u9635\u64cd\u4f5c\u3002\u5982\u679c\u9700\u8981\u8fdb\u884c\u66f4\u591a\u7684\u77e9\u9635\u8fd0\u7b97\uff0c\u5efa\u8bae\u4f7f\u7528NumPy\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u7684\u5f3a\u5927\u5e93\u3002NumPy\u7684array\u5bf9\u8c61\u53ef\u4ee5\u9ad8\u6548\u5730\u8868\u793a\u548c\u64cd\u4f5c\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5NumPy<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5c1a\u672a\u5b89\u88c5NumPy\uff0c\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>\u521b\u5efa\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>array<\/code>\u51fd\u6570\u521b\u5efa\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u6570\u7ec4\u8868\u793a\u4e00\u4e2a3x3\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e38\u7528\u77e9\u9635\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u64cd\u4f5c\u51fd\u6570\uff0c\u4f8b\u5982\u77e9\u9635\u8f6c\u7f6e\u3001\u77e9\u9635\u4e58\u6cd5\u3001\u6c42\u9006\u77e9\u9635\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u77e9\u9635\u8f6c\u7f6e<\/p>\n<p>transpose_matrix = np.transpose(matrix)<\/p>\n<p>print(&quot;Transpose:\\n&quot;, transpose_matrix)<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>matrix2 = np.array([<\/p>\n<p>    [9, 8, 7],<\/p>\n<p>    [6, 5, 4],<\/p>\n<p>    [3, 2, 1]<\/p>\n<p>])<\/p>\n<p>product_matrix = np.dot(matrix, matrix2)<\/p>\n<p>print(&quot;Product:\\n&quot;, product_matrix)<\/p>\n<h2><strong>\u6c42\u9006\u77e9\u9635<\/strong><\/h2>\n<p>inverse_matrix = np.linalg.inv(matrix)<\/p>\n<p>print(&quot;Inverse:\\n&quot;, inverse_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u7684DataFrame\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u7528\u6765\u8868\u793a\u77e9\u9635\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u5e26\u6709\u6807\u7b7e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Pandas<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5c1a\u672a\u5b89\u88c5Pandas\uff0c\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 pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Pandas\u7684<code>DataFrame<\/code>\u51fd\u6570\u521b\u5efa\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4f7f\u7528Pandas DataFrame\u8868\u793a\u4e00\u4e2a3x3\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = pd.DataFrame([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>], columns=[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;])<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e38\u7528\u77e9\u9635\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>Pandas\u4e5f\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u636e\u64cd\u4f5c\u51fd\u6570\uff0c\u4f8b\u5982\u9009\u62e9\u7279\u5b9a\u5217\u3001\u884c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9009\u62e9\u4e00\u5217<\/p>\n<p>column_A = matrix[&#39;A&#39;]<\/p>\n<p>print(&quot;Column A:\\n&quot;, column_A)<\/p>\n<h2><strong>\u9009\u62e9\u4e00\u884c<\/strong><\/h2>\n<p>row_1 = matrix.iloc[0]<\/p>\n<p>print(&quot;Row 1:\\n&quot;, row_1)<\/p>\n<h2><strong>\u77e9\u9635\u8f6c\u7f6e<\/strong><\/h2>\n<p>transpose_matrix = matrix.T<\/p>\n<p>print(&quot;Transpose:\\n&quot;, transpose_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001SciPy\u5e93<\/h3>\n<\/p>\n<p><p>SciPy\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u6570\u5b66\u3001\u79d1\u5b66\u548c\u5de5\u7a0b\u51fd\u6570\u3002SciPy\u7684<code>scipy.sparse<\/code>\u6a21\u5757\u53ef\u4ee5\u5904\u7406\u7a00\u758f\u77e9\u9635\uff0c\u8fd9\u5728\u5904\u7406\u5927\u89c4\u6a21\u7a00\u758f\u77e9\u9635\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5SciPy<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5c1a\u672a\u5b89\u88c5SciPy\uff0c\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 scipy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u7a00\u758f\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528SciPy\u7684<code>sparse<\/code>\u6a21\u5757\u521b\u5efa\u7a00\u758f\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy import sparse<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7a00\u758f\u77e9\u9635<\/strong><\/h2>\n<p>matrix = sparse.csr_matrix([<\/p>\n<p>    [1, 0, 0],<\/p>\n<p>    [0, 2, 0],<\/p>\n<p>    [0, 0, 3]<\/p>\n<p>])<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e38\u7528\u77e9\u9635\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>SciPy\u63d0\u4f9b\u4e86\u8bb8\u591a\u7a00\u758f\u77e9\u9635\u7684\u64cd\u4f5c\u51fd\u6570\uff0c\u4f8b\u5982\u6c42\u89e3\u7ebf\u6027\u65b9\u7a0b\u7ec4\u3001\u7279\u5f81\u503c\u5206\u89e3\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7a00\u758f\u77e9\u9635\u8f6c\u4e3a\u5bc6\u96c6\u77e9\u9635<\/p>\n<p>dense_matrix = matrix.todense()<\/p>\n<p>print(&quot;Dense matrix:\\n&quot;, dense_matrix)<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>matrix2 = sparse.csr_matrix([<\/p>\n<p>    [0, 0, 1],<\/p>\n<p>    [0, 2, 0],<\/p>\n<p>    [3, 0, 0]<\/p>\n<p>])<\/p>\n<p>product_matrix = matrix.dot(matrix2)<\/p>\n<p>print(&quot;Product:\\n&quot;, product_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8868\u793a\u4e00\u4e2a\u77e9\u9635\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6700\u5e38\u7528\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5\u662f\u4f7f\u7528NumPy\u5e93\u3002NumPy\u4e0d\u4ec5\u53ef\u4ee5\u9ad8\u6548\u5730\u8868\u793a\u548c\u64cd\u4f5c\u77e9\u9635\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u64cd\u4f5c\u51fd\u6570\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u5e26\u6709\u6807\u7b7e\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u7684DataFrame\u5bf9\u8c61\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u5927\u89c4\u6a21\u7a00\u758f\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528SciPy\u5e93\u7684<code>sparse<\/code>\u6a21\u5757\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u77e9\u9635\u64cd\u4f5c\u66f4\u52a0\u4fbf\u6377\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u8868\u793a\u548c\u64cd\u4f5c\u77e9\u9635\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u79cd\u5e93\u53ef\u4ee5\u6709\u6548\u5730\u8868\u793a\u548c\u64cd\u4f5c\u77e9\u9635\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fNumPy\u548cPandas\u3002NumPy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u5bf9\u8c61ndarray\uff0c\u9002\u5408\u7528\u4e8e\u6570\u5b66\u8fd0\u7b97\u548c\u77e9\u9635\u64cd\u4f5c\u3002Pandas\u5219\u662f\u7528\u4e8e\u6570\u636e\u5206\u6790\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u5c24\u5176\u9002\u5408\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u901a\u8fc7\u8fd9\u4e24\u4e2a\u5e93\uff0c\u7528\u6237\u80fd\u591f\u8f7b\u677e\u521b\u5efa\u3001\u4fee\u6539\u548c\u8fd0\u7b97\u77e9\u9635\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528NumPy\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff1f<\/strong><br \/>\u4f7f\u7528NumPy\u521b\u5efa\u77e9\u9635\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u901a\u8fc7<code>numpy.array()<\/code>\u51fd\u6570\u5c06\u5d4c\u5957\u5217\u8868\u8f6c\u6362\u4e3a\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a2&#215;3\u7684\u77e9\u9635\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nmatrix = np.array([[1, 2, 3], [4, 5, 6]])\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5b50\u6570\u7ec4\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5f62\u6210\u4e00\u4e2a\u77e9\u9635\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8fdb\u884c\u77e9\u9635\u7684\u57fa\u672c\u8fd0\u7b97\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528<code>numpy.dot()<\/code>\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5\uff0c\u4f7f\u7528<code>numpy.add()<\/code>\u6216\u76f4\u63a5\u7684<code>+<\/code>\u7b26\u53f7\u8fdb\u884c\u77e9\u9635\u52a0\u6cd5\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>numpy.transpose()<\/code>\u53ef\u4ee5\u8f7b\u677e\u8f6c\u7f6e\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nA = np.array([[1, 2], [3, 4]])\nB = np.array([[5, 6], [7, 8]])\nC = np.dot(A, B)  # \u77e9\u9635\u4e58\u6cd5\nD = np.add(A, B)  # \u77e9\u9635\u52a0\u6cd5\nE = np.transpose(A)  # \u77e9\u9635\u8f6c\u7f6e\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u57fa\u672c\u64cd\u4f5c\uff0c\u7528\u6237\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u590d\u6742\u7684\u6570\u5b66\u8ba1\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8868\u793a\u4e00\u4e2a\u77e9\u9635\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528Pandas\u5e93\u3001\u4f7f\u7528SciPy\u5e93\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1099161,"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\/1099143"}],"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=1099143"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1099143\/revisions"}],"predecessor-version":[{"id":1099165,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1099143\/revisions\/1099165"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1099161"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1099143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1099143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1099143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}