{"id":950266,"date":"2024-12-27T00:30:51","date_gmt":"2024-12-26T16:30:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/950266.html"},"modified":"2024-12-27T00:30:53","modified_gmt":"2024-12-26T16:30:53","slug":"python-%e7%9f%a9%e9%98%b5%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/950266.html","title":{"rendered":"python  \u77e9\u9635\u5982\u4f55\u8868\u793a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084920\/12d11c65-374d-4df7-8b59-9a6fab2c5c97.webp\" alt=\"python  \u77e9\u9635\u5982\u4f55\u8868\u793a\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8868\u793a\uff0c\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u548cPandas\u5e93\u3002\u5d4c\u5957\u5217\u8868\u662f\u6700\u57fa\u672c\u7684\u65b9\u6cd5\uff0c\u4f46NumPy\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u64cd\u4f5c\u548c\u66f4\u591a\u7684\u529f\u80fd\u3002\u4f7f\u7528NumPy\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u3001\u5207\u7247\u548c\u53d8\u6362\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u77e9\u9635\u8868\u793a\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u548c\u57fa\u7840\u7684\u662f\u5d4c\u5957\u5217\u8868\u3002\u7136\u800c\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u77e9\u9635\u8868\u793a\u548c\u64cd\u4f5c\u65b9\u6cd5\uff0c\u88ab\u5e7f\u6cdb\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u4e2d\u3002\u6b64\u5916\uff0cPandas\u5e93\u4e2d\u7684DataFrame\u4e5f\u53ef\u4ee5\u89c6\u4f5c\u4e00\u79cd\u77e9\u9635\u8868\u793a\u65b9\u5f0f\uff0c\u9002\u5408\u4e8e\u6570\u636e\u5206\u6790\u548c\u64cd\u4f5c\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u8868\u793a\u65b9\u6cd5\uff0c\u5e76\u8ba8\u8bba\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5d4c\u5957\u5217\u8868\u8868\u793a\u77e9\u9635<\/p>\n<\/p>\n<p><p>\u5d4c\u5957\u5217\u8868\u662fPython\u4e2d\u5185\u7f6e\u7684\u5217\u8868\u5bf9\u8c61\uff0c\u901a\u8fc7\u5d4c\u5957\u7684\u65b9\u5f0f\u53ef\u4ee5\u7528\u6765\u8868\u793a\u77e9\u9635\u3002\u4e00\u4e2a\u77e9\u9635\u53ef\u4ee5\u770b\u4f5c\u662f\u4e00\u4e2a\u7531\u5217\u8868\u7ec4\u6210\u7684\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5185\u90e8\u5217\u8868\u4ee3\u8868\u77e9\u9635\u7684\u4e00\u884c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u548c\u8bbf\u95ee\u5d4c\u5957\u5217\u8868<\/li>\n<\/ol>\n<p><p>\u521b\u5efa\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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>\u8bbf\u95ee\u77e9\u9635\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528\u53cc\u91cd\u7d22\u5f15\uff0c\u4f8b\u5982\u8bbf\u95ee\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = matrix[0][1]  # \u7ed3\u679c\u4e3a2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5d4c\u5957\u5217\u8868\u7684\u4f18\u7f3a\u70b9<\/li>\n<\/ol>\n<p><p>\u5d4c\u5957\u5217\u8868\u7684\u4f18\u70b9\u5728\u4e8e\u5176\u7b80\u5355\u6027\u548c\u7075\u6d3b\u6027\uff0c\u9002\u5408\u4e8e\u5c0f\u578b\u77e9\u9635\u6216\u7b80\u5355\u64cd\u4f5c\u3002\u7136\u800c\uff0c\u7531\u4e8ePython\u5217\u8868\u7684\u52a8\u6001\u6027\uff0c\u5d4c\u5957\u5217\u8868\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\u6548\u7387\u8f83\u4f4e\uff0c\u5c24\u5176\u662f\u5728\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93\u8868\u793a\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\u8c61ndarray\u3002\u4f7f\u7528NumPy\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u3001\u5207\u7247\u548c\u53d8\u6362\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efaNumPy\u6570\u7ec4<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7NumPy\u7684array\u51fd\u6570\u5c06\u5d4c\u5957\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\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, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>NumPy\u77e9\u9635\u64cd\u4f5c<\/li>\n<\/ol>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u64cd\u4f5c\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528.dot\u65b9\u6cd5\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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>result = np.dot(matrix, matrix2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NumPy\u8fd8\u652f\u6301\u5404\u79cd\u77e9\u9635\u53d8\u6362\uff0c\u5982\u8f6c\u7f6e\u3001\u9006\u77e9\u9635\u8ba1\u7b97\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transpose = matrix.T<\/p>\n<p>inverse = np.linalg.inv(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>NumPy\u7684\u4f18\u7f3a\u70b9<\/li>\n<\/ol>\n<p><p>NumPy\u7684\u4e3b\u8981\u4f18\u70b9\u5728\u4e8e\u5176\u9ad8\u6548\u6027\u548c\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5c24\u5176\u9002\u5408\u4e8e\u5927\u578b\u77e9\u9635\u548c\u590d\u6742\u77e9\u9635\u8fd0\u7b97\u3002\u7136\u800c\uff0cNumPy\u6570\u7ec4\u7684\u7c7b\u578b\u56fa\u5b9a\uff0c\u7f3a\u5c11Python\u5217\u8868\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Pandas\u5e93\u8868\u793a\u77e9\u9635<\/p>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u5206\u6790\u7684\u5e93\uff0c\u5176\u4e2d\u7684DataFrame\u5bf9\u8c61\u53ef\u4ee5\u88ab\u89c6\u4f5c\u77e9\u9635\u3002DataFrame\u4e0d\u4ec5\u5305\u542b\u6570\u636e\uff0c\u8fd8\u53ef\u4ee5\u5305\u542b\u884c\u5217\u7d22\u5f15\uff0c\u4fbf\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efaPandas DataFrame<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7Pandas\u7684DataFrame\u51fd\u6570\u5c06\u5d4c\u5957\u5217\u8868\u6216NumPy\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\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><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>DataFrame\u7684\u77e9\u9635\u64cd\u4f5c<\/li>\n<\/ol>\n<p><p>\u867d\u7136DataFrame\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0c\u4f46\u4e5f\u652f\u6301\u57fa\u672c\u7684\u77e9\u9635\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u8bbf\u95ee\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7iloc\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = matrix.iloc[0, 1]  # \u7ed3\u679c\u4e3a2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>DataFrame\u8fd8\u652f\u6301\u4e0eNumPy\u6570\u7ec4\u7c7b\u4f3c\u7684\u8fd0\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = matrix.dot(matrix.T)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>Pandas\u7684\u4f18\u7f3a\u70b9<\/li>\n<\/ol>\n<p><p>Pandas\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u80fd\u529b\u548c\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5c24\u5176\u9002\u5408\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002\u7f3a\u70b9\u662f\u5bf9\u5927\u578b\u77e9\u9635\u7684\u8fd0\u7b97\u6548\u7387\u4e0d\u5982NumPy\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u77e9\u9635\u8868\u793a\u65b9\u5f0f<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u77e9\u9635\u8868\u793a\u65b9\u5f0f\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5d4c\u5957\u5217\u8868\u9002\u5408\u4e8e\u5c0f\u578b\u77e9\u9635\u548c\u7b80\u5355\u64cd\u4f5c\uff0c\u5177\u6709\u7075\u6d3b\u6027\uff1b<\/li>\n<li>NumPy\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6548\u77e9\u9635\u8fd0\u7b97\u7684\u573a\u666f\uff0c\u5c24\u5176\u662f\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\uff1b<\/li>\n<li>Pandas\u9002\u5408\u4e8e\u9700\u8981\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u7684\u573a\u666f\uff0c\u80fd\u591f\u65b9\u4fbf\u5730\u5904\u7406\u5e26\u7d22\u5f15\u7684\u6570\u636e\u3002<\/li>\n<\/ol>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u4f1a\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u751a\u81f3\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u5e93\u4ee5\u53d1\u6325\u5404\u81ea\u7684\u4f18\u52bf\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\uff0c\u4e86\u89e3\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u90fd\u662f\u5b9e\u73b0\u9ad8\u6548\u7f16\u7a0b\u7684\u5173\u952e\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\u77e9\u9635\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6216NumPy\u5e93\u6765\u521b\u5efa\u3002\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u591a\u4e2a\u5217\u8868\u653e\u5728\u4e00\u4e2a\u5927\u7684\u5217\u8868\u4e2d\u6765\u8868\u793a\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a2&#215;3\u7684\u77e9\u9635\u53ef\u4ee5\u5199\u4f5c\uff1a<code>matrix = [[1, 2, 3], [4, 5, 6]]<\/code>\u3002\u5982\u679c\u4f7f\u7528NumPy\uff0c\u53ef\u4ee5\u7528<code>numpy.array()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u77e9\u9635\uff0c\u793a\u4f8b\u4ee3\u7801\u4e3a\uff1a<code>import numpy as np; matrix = np.array([[1, 2, 3], [4, 5, 6]])<\/code>\u3002NumPy\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u3002<\/p>\n<p><strong>Python\u4e2d\u7684\u77e9\u9635\u8fd0\u7b97\u5982\u4f55\u8fdb\u884c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u8f7b\u677e\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u3002\u5e38\u89c1\u7684\u64cd\u4f5c\u5305\u62ec\u77e9\u9635\u52a0\u6cd5\u3001\u51cf\u6cd5\u548c\u4e58\u6cd5\u3002\u4f7f\u7528NumPy\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd0\u7b97\u7b26\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\uff0c\u4f8b\u5982\uff1a<code>result = np.add(matrix1, matrix2)<\/code>\u5b9e\u73b0\u77e9\u9635\u52a0\u6cd5\uff0c<code>result = np.dot(matrix1, matrix2)<\/code>\u5b9e\u73b0\u77e9\u9635\u4e58\u6cd5\u3002\u6b64\u5916\uff0cNumPy\u8fd8\u652f\u6301\u5bf9\u77e9\u9635\u8fdb\u884c\u8f6c\u7f6e\u3001\u6c42\u9006\u7b49\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528\u77e9\u9635\u7684\u9ad8\u7ea7\u64cd\u4f5c\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01Python\u4e2d\u7684NumPy\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u652f\u6301\u9ad8\u7ea7\u77e9\u9635\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5229\u7528<code>numpy.linalg<\/code>\u6a21\u5757\u8fdb\u884c\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf\u7684\u8ba1\u7b97\u3001\u6c42\u89e3\u7ebf\u6027\u65b9\u7a0b\u7ec4\u7b49\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>numpy.linalg.inv()<\/code>\u6765\u8ba1\u7b97\u77e9\u9635\u7684\u9006\uff0c\u4f7f\u7528<code>numpy.linalg.det()<\/code>\u6765\u8ba1\u7b97\u77e9\u9635\u7684\u884c\u5217\u5f0f\u3002\u8fd9\u4e9b\u529f\u80fd\u4f7f\u5f97Python\u6210\u4e3a\u5904\u7406\u7ebf\u6027\u4ee3\u6570\u95ee\u9898\u7684\u5f3a\u5927\u5de5\u5177\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8868\u793a\uff0c\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u548cPandas\u5e93\u3002\u5d4c\u5957\u5217\u8868\u662f\u6700\u57fa\u672c\u7684 [&hellip;]","protected":false},"author":3,"featured_media":950268,"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\/950266"}],"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=950266"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/950266\/revisions"}],"predecessor-version":[{"id":950271,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/950266\/revisions\/950271"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/950268"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=950266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=950266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=950266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}