{"id":1004139,"date":"2024-12-27T10:23:34","date_gmt":"2024-12-27T02:23:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1004139.html"},"modified":"2024-12-27T10:23:38","modified_gmt":"2024-12-27T02:23:38","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e7%9f%a9%e9%98%b5%e8%bf%90%e7%ae%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1004139.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u77e9\u9635\u8fd0\u7b97"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081214\/de0d8679-ddfb-4280-afdb-17f2d78c9a04.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u77e9\u9635\u8fd0\u7b97\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9a\u4e49\u77e9\u9635\u8fd0\u7b97\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u548cPandas\u5e93\u7b49\u65b9\u6cd5\u3002\u5176\u4e2d\uff0cNumPy\u5e93\u662f\u6700\u5e38\u7528\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u77e9\u9635\u548c\u6570\u7ec4\u5904\u7406\u529f\u80fd\u3001\u6613\u4e8e\u5b9e\u73b0\u591a\u79cd\u77e9\u9635\u8fd0\u7b97\u3001\u652f\u6301\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002<\/strong>\u5728NumPy\u4e2d\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u77e9\u9635\u7684\u52a0\u51cf\u3001\u4e58\u9664\u3001\u8f6c\u7f6e\u3001\u6c42\u9006\u7b49\u64cd\u4f5c\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u548c\u64cd\u4f5c\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u5b9a\u4e49\u77e9\u9635<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u57fa\u672c\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6765\u5b9a\u4e49\u77e9\u9635\u3002\u5d4c\u5957\u5217\u8868\u662f\u6307\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4e5f\u662f\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u7684\u77e9\u9635\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49\u77e9\u9635<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u6765\u5b9a\u4e49\u4e00\u4e2a\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a2&#215;2\u7684\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [<\/p>\n<p>    [1, 2],<\/p>\n<p>    [3, 4]<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u77e9\u9635\u52a0\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u77e9\u9635\u52a0\u6cd5\u662f\u6307\u4e24\u4e2a\u77e9\u9635\u7684\u5bf9\u5e94\u5143\u7d20\u76f8\u52a0\u3002\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u904d\u5386\u77e9\u9635\u5143\u7d20\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def matrix_addition(A, B):<\/p>\n<p>    result = [[0 for _ in range(len(A[0]))] for _ in range(len(A))]<\/p>\n<p>    for i in range(len(A)):<\/p>\n<p>        for j in range(len(A[0])):<\/p>\n<p>            result[i][j] = A[i][j] + B[i][j]<\/p>\n<p>    return result<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u77e9\u9635\u4e58\u6cd5\u9700\u8981\u904d\u5386\u884c\u548c\u5217\uff0c\u5e76\u8ba1\u7b97\u70b9\u79ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def matrix_multiplication(A, B):<\/p>\n<p>    result = [[0 for _ in range(len(B[0]))] for _ in range(len(A))]<\/p>\n<p>    for i in range(len(A)):<\/p>\n<p>        for j in range(len(B[0])):<\/p>\n<p>            for k in range(len(B)):<\/p>\n<p>                result[i][j] += A[i][k] * B[k][j]<\/p>\n<p>    return result<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93\u5b9a\u4e49\u77e9\u9635<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u6838\u5fc3\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u7528\u4e8e\u64cd\u4f5c\u6570\u7ec4\u7684\u591a\u79cd\u51fd\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5NumPy<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528NumPy\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u8fd9\u4e2a\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9a\u4e49\u77e9\u9635<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u5b9a\u4e49\u77e9\u9635\u975e\u5e38\u7b80\u5355\uff0c\u4f7f\u7528<code>numpy.array<\/code>\u5373\u53ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([<\/p>\n<p>    [1, 2],<\/p>\n<p>    [3, 4]<\/p>\n<p>])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u77e9\u9635\u52a0\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u52a0\u53f7\u8fdb\u884c\u77e9\u9635\u52a0\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">A = np.array([<\/p>\n<p>    [1, 2],<\/p>\n<p>    [3, 4]<\/p>\n<p>])<\/p>\n<p>B = np.array([<\/p>\n<p>    [5, 6],<\/p>\n<p>    [7, 8]<\/p>\n<p>])<\/p>\n<p>result = A + B<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u77e9\u9635\u4e58\u6cd5\u53ef\u4ee5\u4f7f\u7528<code>numpy.dot<\/code>\u6216<code>@<\/code>\u8fd0\u7b97\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = np.dot(A, B)<\/p>\n<h2><strong>\u6216\u8005<\/strong><\/h2>\n<p>result = A @ B<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u77e9\u9635\u8f6c\u7f6e<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.transpose<\/code>\u6216<code>.T<\/code>\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transpose = np.transpose(A)<\/p>\n<h2><strong>\u6216\u8005<\/strong><\/h2>\n<p>transpose = A.T<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u77e9\u9635\u6c42\u9006<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.linalg.inv<\/code>\u8fdb\u884c\u77e9\u9635\u6c42\u9006\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">inverse = np.linalg.inv(A)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528Pandas\u5e93\u5b9a\u4e49\u77e9\u9635<\/p>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u5206\u6790\u7684\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86<code>DataFrame<\/code>\u5bf9\u8c61\uff0c\u9002\u5408\u8fdb\u884c\u6570\u636e\u8868\u683c\u5f62\u5f0f\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Pandas<\/strong><\/p>\n<\/p>\n<p><p>Pandas\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9a\u4e49\u77e9\u9635<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u5b9a\u4e49\u77e9\u9635\uff0c\u53ef\u4ee5\u5229\u7528<code>DataFrame<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame([<\/p>\n<p>    [1, 2],<\/p>\n<p>    [3, 4]<\/p>\n<p>], columns=[&#39;A&#39;, &#39;B&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u77e9\u9635\u64cd\u4f5c<\/strong><\/p>\n<\/p>\n<p><p>\u867d\u7136Pandas\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u7684\u77e9\u9635\u8fd0\u7b97\uff0c\u4f8b\u5982\u52a0\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df1 = pd.DataFrame([<\/p>\n<p>    [1, 2],<\/p>\n<p>    [3, 4]<\/p>\n<p>], columns=[&#39;A&#39;, &#39;B&#39;])<\/p>\n<p>df2 = pd.DataFrame([<\/p>\n<p>    [5, 6],<\/p>\n<p>    [7, 8]<\/p>\n<p>], columns=[&#39;A&#39;, &#39;B&#39;])<\/p>\n<p>result = df1 + df2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u77e9\u9635\u8fd0\u7b97\uff0c\u5efa\u8bae\u4f7f\u7528NumPy\u7ed3\u5408Pandas\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5b9a\u4e49\u548c\u64cd\u4f5c\u77e9\u9635\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u5c0f\u89c4\u6a21\u77e9\u9635\u8fd0\u7b97\uff0c\u5d4c\u5957\u5217\u8868\u53ef\u80fd\u5df2\u7ecf\u8db3\u591f\uff1b\u800c\u5bf9\u4e8e\u590d\u6742\u548c\u5927\u89c4\u6a21\u7684\u77e9\u9635\u8fd0\u7b97\uff0cNumPy\u65e0\u7591\u662f\u6700\u7406\u60f3\u7684\u9009\u62e9\uff0c\u56e0\u5176\u9ad8\u6548\u7684\u6027\u80fd\u548c\u4e30\u5bcc\u7684\u529f\u80fd\u3002\u6b64\u5916\uff0c\u7ed3\u5408Pandas\u8fdb\u884c\u6570\u636e\u5206\u6790\u65f6\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7<code>DataFrame<\/code>\u8fdb\u884c\u7b80\u5355\u7684\u77e9\u9635\u8fd0\u7b97\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u7406\u89e3\u5176\u57fa\u672c\u539f\u7406\u548c\u4f7f\u7528\u573a\u666f\u5bf9\u4e8e\u6709\u6548\u5730\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u521b\u5efa\u77e9\u9635\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528<code>numpy.array()<\/code>\u51fd\u6570\u6765\u5b9a\u4e49\u77e9\u9635\u3002\u4f8b\u5982\uff0c<code>matrix = np.array([[1, 2], [3, 4]])<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a2&#215;2\u7684\u77e9\u9635\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u77e9\u9635\u8fd0\u7b97\uff1f<\/strong><br \/>\u5e38\u89c1\u7684\u77e9\u9635\u8fd0\u7b97\u5305\u62ec\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u8f6c\u7f6e\u3002\u4f7f\u7528NumPy\uff0c\u77e9\u9635\u52a0\u6cd5\u53ef\u4ee5\u901a\u8fc7<code>np.add(matrix1, matrix2)<\/code>\u5b9e\u73b0\uff0c\u4e58\u6cd5\u53ef\u4ee5\u4f7f\u7528<code>np.dot(matrix1, matrix2)<\/code>\u6216<code>matrix1 @ matrix2<\/code>\u3002\u8f6c\u7f6e\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7<code>matrix.T<\/code>\u6765\u5b8c\u6210\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u77e9\u9635\u7684\u7ef4\u5ea6\u4e0d\u5339\u914d\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u65f6\uff0c\u7ef4\u5ea6\u4e0d\u5339\u914d\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4e24\u4e2a\u77e9\u9635\u8fdb\u884c\u52a0\u6cd5\u65f6\uff0c\u5b83\u4eec\u7684\u5f62\u72b6\u5fc5\u987b\u76f8\u540c\u3002\u53ef\u4ee5\u901a\u8fc7<code>matrix1.shape<\/code>\u6765\u68c0\u67e5\u77e9\u9635\u7684\u7ef4\u5ea6\u3002\u5982\u679c\u9700\u8981\u8fdb\u884c\u66f4\u590d\u6742\u7684\u8fd0\u7b97\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5e7f\u64ad\u673a\u5236\uff0cNumPy\u4f1a\u81ea\u52a8\u6269\u5c55\u7ef4\u5ea6\u4ee5\u8fdb\u884c\u8fd0\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9a\u4e49\u77e9\u9635\u8fd0\u7b97\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u548cPandas\u5e93\u7b49\u65b9\u6cd5\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1004146,"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\/1004139"}],"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=1004139"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004139\/revisions"}],"predecessor-version":[{"id":1004147,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004139\/revisions\/1004147"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1004146"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1004139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1004139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1004139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}