{"id":953396,"date":"2024-12-27T01:43:12","date_gmt":"2024-12-26T17:43:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/953396.html"},"modified":"2024-12-27T01:43:14","modified_gmt":"2024-12-26T17:43:14","slug":"python%e4%b8%ad%e7%9f%a9%e9%98%b5%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/953396.html","title":{"rendered":"python\u4e2d\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\/25090911\/9669a78b-57c0-4fdd-9bbf-f06fe11982e5.webp\" alt=\"python\u4e2d\u77e9\u9635\u5982\u4f55\u8868\u793a\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u8868\u793a\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec<strong>\u5217\u8868\u7684\u5217\u8868\u3001NumPy\u5e93\u3001Pandas\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u548c\u6700\u6709\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528NumPy\u5e93\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u5904\u7406\u529f\u80fd\u3002NumPy\u5e93\u5728\u5904\u7406\u6570\u503c\u8ba1\u7b97\u65f6\u5177\u6709\u51fa\u8272\u7684\u6027\u80fd\u548c\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u4f7f\u5f97\u77e9\u9635\u64cd\u4f5c\u66f4\u52a0\u4fbf\u6377\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5217\u8868\u7684\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5217\u8868\u7684\u5217\u8868\u662f\u4e00\u79cd\u7b80\u5355\u76f4\u89c2\u7684\u65b9\u6cd5\u6765\u8868\u793a\u77e9\u9635\u3002\u6bcf\u4e2a\u5b50\u5217\u8868\u4ee3\u8868\u77e9\u9635\u7684\u4e00\u884c\uff0c\u5b50\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u77e9\u9635\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u548c\u8bbf\u95ee\u77e9\u9635<\/strong><\/li>\n<\/ol>\n<p><p>\u8981\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff0c\u53ef\u4ee5\u7b80\u5355\u5730\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a2&#215;3\u7684\u77e9\u9635\u53ef\u4ee5\u8868\u793a\u4e3a\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>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bbf\u95ee\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u53cc\u91cd\u7d22\u5f15\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = matrix[0][1]  # \u8bbf\u95ee\u7b2c\u4e00\u884c\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u8f93\u51fa\u4e3a2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u64cd\u4f5c\u77e9\u9635<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528\u5217\u8868\u7684\u5217\u8868\u8868\u793a\u77e9\u9635\uff0c\u53ef\u4ee5\u8fdb\u884c\u57fa\u672c\u7684\u77e9\u9635\u64cd\u4f5c\uff0c\u5982\u904d\u5386\u3001\u4fee\u6539\u3001\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u3002\u904d\u5386\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u7684for\u5faa\u73af\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in matrix:<\/p>\n<p>    for element in row:<\/p>\n<p>        print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5728\u8fdb\u884c\u590d\u6742\u7684\u77e9\u9635\u8fd0\u7b97\u65f6\u663e\u5f97\u4e0d\u591f\u7b80\u6d01\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001NumPy\u5e93<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u4e00\u7cfb\u5217\u7528\u4e8e\u64cd\u4f5c\u6570\u7ec4\u7684\u51fd\u6570\u3002\u5b83\u662f\u6570\u503c\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u77e9\u9635\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u5bfc\u5165NumPy<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\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>\u7136\u540e\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165NumPy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u521b\u5efaNumPy\u77e9\u9635<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u4e2d\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7<code>numpy.array()<\/code>\u51fd\u6570\u521b\u5efa\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6]<\/p>\n<p>])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u77e9\u9635\u8fd0\u7b97<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u8fd0\u7b97\u529f\u80fd\uff0c\u5982\u77e9\u9635\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u3001\u8f6c\u7f6e\u7b49\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u77e9\u9635\u52a0\u6cd5<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">matrix1 = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6]<\/p>\n<p>])<\/p>\n<p>matrix2 = np.array([<\/p>\n<p>    [7, 8, 9],<\/p>\n<p>    [10, 11, 12]<\/p>\n<p>])<\/p>\n<p>result = matrix1 + matrix2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">result = np.dot(matrix1, matrix2.T)  # \u4f7f\u7528.T\u6765\u8f6c\u7f6ematrix2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u77e9\u9635\u8f6c\u7f6e<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">transpose_matrix = matrix.T<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NumPy\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u9ad8\u6548\u6027\u548c\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u4f7f\u5f97\u77e9\u9635\u64cd\u4f5c\u66f4\u4e3a\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001Pandas\u5e93<\/p>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u6570\u636e\u5206\u6790\u5e93\uff0c\u867d\u7136\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5904\u7406\u548c\u5206\u6790\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u8868\u793a\u548c\u64cd\u4f5c\u77e9\u9635\u3002Pandas\u4e2d\u7684DataFrame\u53ef\u4ee5\u770b\u4f5c\u662f\u5e26\u6709\u884c\u5217\u6807\u7b7e\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u5bfc\u5165Pandas<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165Pandas\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u521b\u5efaDataFrame\u77e9\u9635<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>pandas.DataFrame()<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = pd.DataFrame({<\/p>\n<p>    &#39;Column1&#39;: [1, 4],<\/p>\n<p>    &#39;Column2&#39;: [2, 5],<\/p>\n<p>    &#39;Column3&#39;: [3, 6]<\/p>\n<p>})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u77e9\u9635\u64cd\u4f5c<\/strong><\/li>\n<\/ol>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\uff0c\u5982\u5207\u7247\u3001\u6761\u4ef6\u7b5b\u9009\u7b49\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5207\u7247\u64cd\u4f5c<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">sub_matrix = matrix.iloc[0:1, 0:2]  # \u63d0\u53d6\u7b2c1\u884c\u548c\u7b2c1\u30012\u5217<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u6761\u4ef6\u7b5b\u9009<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">filtered_matrix = matrix[matrix[&#39;Column1&#39;] &gt; 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pandas\u7684\u4f18\u70b9\u5728\u4e8e\u5176\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u548c\u53cb\u597d\u7684\u6570\u636e\u8868\u793a\u65b9\u5f0f\uff0c\u9002\u5408\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8868\u793a\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5176\u4e2d<strong>NumPy<\/strong>\u662f\u6700\u5e38\u7528\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u548c\u77e9\u9635\u8fd0\u7b97\u3002<strong>\u5217\u8868\u7684\u5217\u8868<\/strong>\u867d\u7136\u7b80\u5355\u76f4\u89c2\uff0c\u4f46\u5728\u590d\u6742\u8fd0\u7b97\u4e2d\u4e0d\u591f\u9ad8\u6548\u3002<strong>Pandas<\/strong>\u5219\u9002\u5408\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u4efb\u52a1\uff0c\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u64cd\u4f5c\u548c\u4e30\u5bcc\u7684\u529f\u80fd\u3002\u6839\u636e\u5177\u4f53\u5e94\u7528\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u77e9\u9635\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u7684\u5217\u8868\u6765\u8868\u793a\uff0c\u4f8b\u5982\uff1a<code>matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/code>\u3002\u53e6\u5916\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\uff0c\u901a\u8fc7<code>numpy.array()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u5982\uff1a<code>import numpy as np; matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/code>\u3002NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u6765\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\uff0c\u5982\u52a0\u6cd5\u3001\u4e58\u6cd5\u3001\u8f6c\u7f6e\u7b49\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u77e9\u9635\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u5e93\u548c\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\uff1f<\/strong><br \/>\u9664\u4e86NumPy\uff0cSciPy\u5e93\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u77e9\u9635\u8fd0\u7b97\u529f\u80fd\uff0c\u7279\u522b\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\u548c\u5de5\u7a0b\u5e94\u7528\u3002\u6b64\u5916\uff0cPandas\u5e93\u80fd\u591f\u5904\u7406\u5e26\u6807\u7b7e\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9002\u5408\u4e8e\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002\u5bf9\u4e8e\u66f4\u9ad8\u5c42\u6b21\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4efb\u52a1\uff0cTensorFlow\u548cPyTorch\u7b49\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u4e5f\u5229\u7528\u4e86\u77e9\u9635\u7684\u6982\u5ff5\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u8ba1\u7b97\u56fe\u548c\u81ea\u52a8\u6c42\u5bfc\u529f\u80fd\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5bf9\u77e9\u9635\u8fdb\u884c\u57fa\u672c\u8fd0\u7b97\uff1f<\/strong><br \/>\u57fa\u672c\u7684\u77e9\u9635\u8fd0\u7b97\u53ef\u4ee5\u901a\u8fc7NumPy\u8f7b\u677e\u5b9e\u73b0\u3002\u5e38\u89c1\u7684\u64cd\u4f5c\u5305\u62ec\u77e9\u9635\u52a0\u6cd5\uff08<code>matrix1 + matrix2<\/code>\uff09\u3001\u77e9\u9635\u4e58\u6cd5\uff08<code>np.dot(matrix1, matrix2)<\/code>\u6216<code>matrix1 @ matrix2<\/code>\uff09\u3001\u8f6c\u7f6e\uff08<code>matrix.T<\/code>\uff09\uff0c\u4ee5\u53ca\u6c42\u9006\uff08<code>np.linalg.inv(matrix)<\/code>\uff09\u3002\u8fd9\u4e9b\u64cd\u4f5c\u4e0d\u4ec5\u7b80\u5355\u6613\u7528\uff0c\u800c\u4e14\u6548\u7387\u9ad8\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u8868\u793a\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u5217\u8868\u7684\u5217\u8868\u3001NumPy\u5e93\u3001Pandas\u5e93\u3002\u5176\u4e2d\uff0c\u6700 [&hellip;]","protected":false},"author":3,"featured_media":953403,"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\/953396"}],"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=953396"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953396\/revisions"}],"predecessor-version":[{"id":953404,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953396\/revisions\/953404"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/953403"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=953396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=953396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=953396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}