{"id":1090947,"date":"2025-01-08T14:07:03","date_gmt":"2025-01-08T06:07:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1090947.html"},"modified":"2025-01-08T14:07:07","modified_gmt":"2025-01-08T06:07:07","slug":"python%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e7%9f%a9%e9%98%b5%e7%9a%84%e8%a1%8c%e5%92%8c%e5%88%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1090947.html","title":{"rendered":"python\u5982\u4f55\u67e5\u770b\u77e9\u9635\u7684\u884c\u548c\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203359\/6b28d235-cf9f-4527-b781-214d862f1a0b.webp\" alt=\"python\u5982\u4f55\u67e5\u770b\u77e9\u9635\u7684\u884c\u548c\u5217\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u67e5\u770b\u77e9\u9635\u7684\u884c\u548c\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528NumPy\u5e93\u4e2d\u7684<code>shape<\/code>\u5c5e\u6027\u6765\u5b9e\u73b0\u3001NumPy\u5e93\u4f7f\u5f97\u5904\u7406\u77e9\u9635\u548c\u6570\u7ec4\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3001\u53ef\u4ee5\u521b\u5efa\u77e9\u9635\u5e76\u8f7b\u677e\u83b7\u53d6\u5176\u884c\u6570\u548c\u5217\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u7528\u4e8e\u6267\u884c\u79d1\u5b66\u8ba1\u7b97\u548c\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u3002\u901a\u8fc7\u4f7f\u7528NumPy\u5e93\u4e2d\u7684<code>shape<\/code>\u5c5e\u6027\uff0c\u53ef\u4ee5\u5feb\u901f\u67e5\u770b\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528NumPy\u6765\u67e5\u770b\u77e9\u9635\u7684\u884c\u548c\u5217\u7684\u8be6\u7ec6\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165NumPy\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5NumPy\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\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><p>\u7136\u540e\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165NumPy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u4e00\u4e2a\u77e9\u9635<\/h3>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u521b\u5efa\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u521b\u5efa\u77e9\u9635<\/p>\n<p>matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u4f7f\u7528zeros\u51fd\u6570\u521b\u5efa\u4e00\u4e2a3x3\u7684\u96f6\u77e9\u9635<\/strong><\/h2>\n<p>zero_matrix = np.zeros((3, 3))<\/p>\n<h2><strong>\u4f7f\u7528ones\u51fd\u6570\u521b\u5efa\u4e00\u4e2a3x3\u7684\u5355\u4f4d\u77e9\u9635<\/strong><\/h2>\n<p>ones_matrix = np.ones((3, 3))<\/p>\n<h2><strong>\u4f7f\u7528eye\u51fd\u6570\u521b\u5efa\u4e00\u4e2a3x3\u7684\u5355\u4f4d\u77e9\u9635<\/strong><\/h2>\n<p>identity_matrix = np.eye(3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u67e5\u770b\u77e9\u9635\u7684\u884c\u548c\u5217<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>shape<\/code>\u5c5e\u6027\u53ef\u4ee5\u8f7b\u677e\u67e5\u770b\u77e9\u9635\u7684\u884c\u548c\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix_shape = matrix.shape<\/p>\n<p>print(f&quot;\u77e9\u9635\u7684\u5f62\u72b6: {matrix_shape}&quot;)<\/p>\n<p>print(f&quot;\u77e9\u9635\u7684\u884c\u6570: {matrix_shape[0]}&quot;)<\/p>\n<p>print(f&quot;\u77e9\u9635\u7684\u5217\u6570: {matrix_shape[1]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>shape<\/code>\u5c5e\u6027\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u8868\u793a\u884c\u6570\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u8868\u793a\u5217\u6570\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5feb\u901f\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u66f4\u591aNumPy\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>NumPy\u4e0d\u4ec5\u53ef\u4ee5\u67e5\u770b\u77e9\u9635\u7684\u884c\u548c\u5217\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u8bb8\u591a\u5176\u4ed6\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u77e9\u9635\u7684\u8f6c\u7f6e<\/h4>\n<\/p>\n<p><p>\u77e9\u9635\u7684\u8f6c\u7f6e\u662f\u5c06\u884c\u548c\u5217\u4e92\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transpose_matrix = matrix.T<\/p>\n<p>print(f&quot;\u8f6c\u7f6e\u540e\u7684\u77e9\u9635:\\n{transpose_matrix}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u77e9\u9635\u7684\u52a0\u6cd5<\/h4>\n<\/p>\n<p><p>\u4e24\u4e2a\u77e9\u9635\u76f8\u52a0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix1 = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<p>matrix2 = np.array([[7, 8, 9], [10, 11, 12]])<\/p>\n<p>sum_matrix = matrix1 + matrix2<\/p>\n<p>print(f&quot;\u77e9\u9635\u76f8\u52a0\u7ed3\u679c:\\n{sum_matrix}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u77e9\u9635\u7684\u4e58\u6cd5<\/h4>\n<\/p>\n<p><p>\u77e9\u9635\u4e58\u6cd5\u53ef\u4ee5\u4f7f\u7528<code>dot<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix2 = np.array([[5, 6], [7, 8]])<\/p>\n<p>product_matrix = np.dot(matrix1, matrix2)<\/p>\n<p>print(f&quot;\u77e9\u9635\u4e58\u6cd5\u7ed3\u679c:\\n{product_matrix}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u77e9\u9635\u7684\u9006<\/h4>\n<\/p>\n<p><p>\u8ba1\u7b97\u77e9\u9635\u7684\u9006\u53ef\u4ee5\u4f7f\u7528<code>linalg.inv<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = np.array([[1, 2], [3, 4]])<\/p>\n<p>inverse_matrix = np.linalg.inv(matrix)<\/p>\n<p>print(f&quot;\u77e9\u9635\u7684\u9006:\\n{inverse_matrix}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001\u77e9\u9635\u7684\u884c\u5217\u5f0f<\/h4>\n<\/p>\n<p><p>\u8ba1\u7b97\u77e9\u9635\u7684\u884c\u5217\u5f0f\u53ef\u4ee5\u4f7f\u7528<code>linalg.det<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = np.array([[1, 2], [3, 4]])<\/p>\n<p>determinant = np.linalg.det(matrix)<\/p>\n<p>print(f&quot;\u77e9\u9635\u7684\u884c\u5217\u5f0f: {determinant}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528NumPy\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u67e5\u770b\u77e9\u9635\u7684\u884c\u548c\u5217\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u77e9\u9635\u64cd\u4f5c\u3002<strong>\u5b89\u88c5\u548c\u5bfc\u5165NumPy\u5e93\u3001\u521b\u5efa\u77e9\u9635\u3001\u4f7f\u7528<code>shape<\/code>\u5c5e\u6027\u67e5\u770b\u77e9\u9635\u7684\u884c\u548c\u5217\u662f\u57fa\u672c\u64cd\u4f5c<\/strong>\uff0c\u6b64\u5916\u8fd8\u53ef\u4ee5\u8fdb\u884c\u77e9\u9635\u7684\u8f6c\u7f6e\u3001\u52a0\u6cd5\u3001\u4e58\u6cd5\u3001\u9006\u77e9\u9635\u548c\u884c\u5217\u5f0f\u7b49\u64cd\u4f5c\u3002NumPy\u662f\u5904\u7406\u77e9\u9635\u548c\u6570\u7ec4\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u5bf9\u4e8e\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u4f7f\u7528Python\u67e5\u770b\u77e9\u9635\u7684\u884c\u548c\u5217\uff0c\u4ee5\u53ca\u5982\u4f55\u8fdb\u884c\u5176\u4ed6\u5e38\u89c1\u7684\u77e9\u9635\u64cd\u4f5c\u3002\u5982\u679c\u60a8\u6709\u4efb\u4f55\u95ee\u9898\u6216\u9700\u8981\u8fdb\u4e00\u6b65\u7684\u5e2e\u52a9\uff0c\u8bf7\u968f\u65f6\u7559\u8a00\u4ea4\u6d41\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\u5e76\u67e5\u770b\u5176\u884c\u548c\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5NumPy\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<code>pip install numpy<\/code>\u3002\u521b\u5efa\u77e9\u9635\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shape<\/code>\u5c5e\u6027\u6765\u67e5\u770b\u884c\u548c\u5217\u7684\u6570\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nmatrix = np.array([[1, 2, 3], [4, 5, 6]])\nprint(matrix.shape)  # \u8f93\u51fa (2, 3)\uff0c\u8868\u793a\u67092\u884c\u548c3\u5217\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4f7f\u7528Pandas\u5e93\u67e5\u770b\u77e9\u9635\u7684\u884c\u548c\u5217\uff1f<\/strong><br \/>Pandas\u5e93\u540c\u6837\u662f\u5904\u7406\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\u3002\u901a\u8fc7\u5c06\u6570\u636e\u8f6c\u6362\u4e3aDataFrame\uff0c\u53ef\u4ee5\u8f7b\u677e\u67e5\u770b\u884c\u548c\u5217\u6570\u91cf\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Pandas\u5e93\uff1a<code>pip install pandas<\/code>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = [[1, 2, 3], [4, 5, 6]]\ndf = pd.DataFrame(data)\nprint(df.shape)  # \u8f93\u51fa (2, 3)\uff0c\u8868\u793a\u67092\u884c\u548c3\u5217\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u83b7\u53d6\u77e9\u9635\u7684\u884c\u548c\u5217\u540d\u79f0\uff1f<\/strong><br \/>\u5f53\u4f7f\u7528Pandas\u7684DataFrame\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u884c\u548c\u5217\u7684\u540d\u79f0\u3002\u8fd9\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = [[1, 2, 3], [4, 5, 6]]\ndf = pd.DataFrame(data, columns=[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;], index=[&#39;Row1&#39;, &#39;Row2&#39;])\nprint(df.index)  # \u8f93\u51fa\u884c\u540d\u79f0\nprint(df.columns)  # \u8f93\u51fa\u5217\u540d\u79f0\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u4e0d\u4ec5\u53ef\u4ee5\u67e5\u770b\u77e9\u9635\u7684\u884c\u548c\u5217\u6570\u91cf\uff0c\u8fd8\u80fd\u83b7\u5f97\u66f4\u4e30\u5bcc\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u67e5\u770b\u77e9\u9635\u7684\u884c\u548c\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528NumPy\u5e93\u4e2d\u7684shape\u5c5e\u6027\u6765\u5b9e\u73b0\u3001NumPy\u5e93\u4f7f\u5f97\u5904\u7406\u77e9\u9635\u548c [&hellip;]","protected":false},"author":3,"featured_media":1090957,"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\/1090947"}],"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=1090947"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1090947\/revisions"}],"predecessor-version":[{"id":1090958,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1090947\/revisions\/1090958"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1090957"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1090947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1090947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1090947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}