{"id":1005847,"date":"2024-12-27T10:39:15","date_gmt":"2024-12-27T02:39:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1005847.html"},"modified":"2024-12-27T10:39:18","modified_gmt":"2024-12-27T02:39:18","slug":"python%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e7%9f%a9%e9%98%b5%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1005847.html","title":{"rendered":"python\u5982\u4f55\u67e5\u770b\u77e9\u9635\u4fe1\u606f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082219\/a09595ab-ddfd-49b5-a130-61eb2b1edb16.webp\" alt=\"python\u5982\u4f55\u67e5\u770b\u77e9\u9635\u4fe1\u606f\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u67e5\u770b\u77e9\u9635\u4fe1\u606f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u67e5\u770b\u77e9\u9635\u7684\u5f62\u72b6\u3001\u7ef4\u5ea6\u3001\u6570\u636e\u7c7b\u578b\u3001\u5143\u7d20\u503c\u7b49\u4fe1\u606f\u3002\u5177\u4f53\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528<code>numpy.shape()<\/code>\u83b7\u53d6\u77e9\u9635\u5f62\u72b6\u3001\u4f7f\u7528<code>numpy.ndim()<\/code>\u83b7\u53d6\u7ef4\u5ea6\u3001\u4f7f\u7528<code>numpy.dtype()<\/code>\u67e5\u770b\u6570\u636e\u7c7b\u578b\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u67e5\u770b\u77e9\u9635\u7684\u5404\u79cd\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001NUMPY\u5e93\u7684\u5b89\u88c5\u4e0e\u7b80\u4ecb<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5904\u7406\u77e9\u9635\uff0c\u6700\u5e38\u7528\u7684\u5c31\u662fNumPy\u5e93\u3002NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u548c\u65b9\u6cd5\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\u3002\u5728\u4f7f\u7528NumPy\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u8be5\u5e93\u3002\u5982\u679c\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\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>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>import numpy as np<\/code>\u5bfc\u5165NumPy\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u67e5\u770b\u77e9\u9635\u7684\u5f62\u72b6\u4e0e\u7ef4\u5ea6<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u83b7\u53d6\u77e9\u9635\u7684\u5f62\u72b6<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86<code>shape<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u77e9\u9635\u7684\u5f62\u72b6\u3002\u77e9\u9635\u7684\u5f62\u72b6\u4ee5\u4e00\u4e2a\u5143\u7ec4\u7684\u5f62\u5f0f\u8fd4\u56de\uff0c\u8868\u793a\u6bcf\u4e2a\u7ef4\u5ea6\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<p>shape = matrix.shape<\/p>\n<p>print(&quot;\u77e9\u9635\u7684\u5f62\u72b6:&quot;, shape)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u8f93\u51fa<code>(2, 3)<\/code>\uff0c\u8868\u793a\u8be5\u77e9\u9635\u67092\u884c3\u5217\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u77e9\u9635\u7684\u7ef4\u5ea6<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>ndim<\/code>\u5c5e\u6027\u53ef\u4ee5\u5f97\u5230\u77e9\u9635\u7684\u7ef4\u5ea6\u3002\u5bf9\u4e8e\u4e8c\u7ef4\u77e9\u9635\uff0c\u7ef4\u5ea6\u662f2\uff1b\u5bf9\u4e8e\u4e09\u7ef4\u77e9\u9635\uff0c\u7ef4\u5ea6\u662f3\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dimensions = matrix.ndim<\/p>\n<p>print(&quot;\u77e9\u9635\u7684\u7ef4\u5ea6:&quot;, dimensions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u5c06\u8f93\u51fa<code>2<\/code>\uff0c\u8868\u660e\u8be5\u77e9\u9635\u662f\u4e8c\u7ef4\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u67e5\u770b\u77e9\u9635\u7684\u6570\u636e\u7c7b\u578b<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u83b7\u53d6\u77e9\u9635\u5143\u7d20\u7684\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u77e9\u9635\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u5177\u6709\u76f8\u540c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u901a\u8fc7<code>dtype<\/code>\u5c5e\u6027\u67e5\u770b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_type = matrix.dtype<\/p>\n<p>print(&quot;\u77e9\u9635\u7684\u6570\u636e\u7c7b\u578b:&quot;, data_type)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\u77e9\u9635\u5143\u7d20\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982<code>int64<\/code>\u6216<code>float64<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8f6c\u6362\u77e9\u9635\u7684\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5c06\u77e9\u9635\u7684\u6570\u636e\u7c7b\u578b\u66f4\u6539\u4e3a\u53e6\u4e00\u79cd\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>astype()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">float_matrix = matrix.astype(float)<\/p>\n<p>print(&quot;\u8f6c\u6362\u540e\u7684\u77e9\u9635\u6570\u636e\u7c7b\u578b:&quot;, float_matrix.dtype)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u628a\u77e9\u9635\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u7c7b\u578b\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u67e5\u770b\u77e9\u9635\u7684\u5143\u7d20\u503c<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bbf\u95ee\u77e9\u9635\u7684\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u77e9\u9635\u7684\u7279\u5b9a\u5143\u7d20\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u683c\u5f0f\u4e3a<code>matrix[row, column]<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = matrix[1, 2]<\/p>\n<p>print(&quot;\u77e9\u9635\u4e2d\u7684\u5143\u7d20:&quot;, element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\u77e9\u9635\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u904d\u5386\u77e9\u9635\u7684\u6240\u6709\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u53ef\u4ee5\u904d\u5386\u77e9\u9635\u7684\u6240\u6709\u5143\u7d20\uff0c\u9010\u4e2a\u8bbf\u95ee\u3002<\/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, end=&#39; &#39;)<\/p>\n<p>    print()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u9010\u884c\u6253\u5370\u51fa\u77e9\u9635\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u77e9\u9635\u7684\u5176\u4ed6\u4fe1\u606f<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u77e9\u9635\u7684\u5927\u5c0f<\/strong><\/p>\n<\/p>\n<p><p>\u77e9\u9635\u7684\u5927\u5c0f\u662f\u6307\u77e9\u9635\u4e2d\u5143\u7d20\u7684\u603b\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7<code>size<\/code>\u5c5e\u6027\u83b7\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">size = matrix.size<\/p>\n<p>print(&quot;\u77e9\u9635\u7684\u5927\u5c0f:&quot;, size)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\u77e9\u9635\u4e2d\u5143\u7d20\u7684\u603b\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u77e9\u9635\u7684\u5b58\u50a8\u5b57\u8282\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>nbytes<\/code>\u5c5e\u6027\u53ef\u4ee5\u83b7\u53d6\u77e9\u9635\u5728\u5185\u5b58\u4e2d\u5360\u7528\u7684\u5b57\u8282\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">bytes_used = matrix.nbytes<\/p>\n<p>print(&quot;\u77e9\u9635\u5360\u7528\u7684\u5b57\u8282\u6570:&quot;, bytes_used)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\u77e9\u9635\u5728\u5185\u5b58\u4e2d\u7684\u5b57\u8282\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u67e5\u770b\u77e9\u9635\u7684\u7edf\u8ba1\u4fe1\u606f<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u77e9\u9635\u5143\u7d20\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86<code>max()<\/code>\u548c<code>min()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u77e9\u9635\u5143\u7d20\u4e2d\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">max_value = matrix.max()<\/p>\n<p>min_value = matrix.min()<\/p>\n<p>print(&quot;\u6700\u5927\u503c:&quot;, max_value, &quot;\u6700\u5c0f\u503c:&quot;, min_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u77e9\u9635\u5143\u7d20\u7684\u5e73\u5747\u503c<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>mean()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8ba1\u7b97\u77e9\u9635\u4e2d\u6240\u6709\u5143\u7d20\u7684\u5e73\u5747\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mean_value = matrix.mean()<\/p>\n<p>print(&quot;\u5e73\u5747\u503c:&quot;, mean_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u67e5\u770b\u548c\u64cd\u4f5c\u77e9\u9635\u7684\u5404\u79cd\u4fe1\u606f\uff0c\u5305\u62ec\u5f62\u72b6\u3001\u7ef4\u5ea6\u3001\u6570\u636e\u7c7b\u578b\u3001\u5927\u5c0f\u3001\u5143\u7d20\u503c\u7b49\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u638c\u63e1\u8fd9\u4e9b\u57fa\u672c\u64cd\u4f5c\u6709\u52a9\u4e8e\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\u548c\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u77e9\u9635\u7684\u7ef4\u5ea6\u548c\u5f62\u72b6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u5904\u7406\u77e9\u9635\u3002\u901a\u8fc7\u8c03\u7528<code>shape<\/code>\u5c5e\u6027\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u77e9\u9635\u7684\u7ef4\u5ea6\u548c\u5f62\u72b6\u3002\u4f8b\u5982\uff0c\u82e5\u4f60\u6709\u4e00\u4e2a\u77e9\u9635<code>A<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>A.shape<\/code>\u6765\u67e5\u770b\u5176\u884c\u6570\u548c\u5217\u6570\u3002\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\uff0c\u8868\u793a\u77e9\u9635\u7684\u7ef4\u5ea6\uff0c\u5982 <code>(\u884c\u6570, \u5217\u6570)<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u67e5\u770b\u77e9\u9635\u4e2d\u7684\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u8981\u67e5\u770b\u77e9\u9635\u4e2d\u5143\u7d20\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>dtype<\/code>\u5c5e\u6027\u3002\u901a\u8fc7\u8c03\u7528<code>A.dtype<\/code>\uff0c\u53ef\u4ee5\u4e86\u89e3\u5230\u77e9\u9635\u4e2d\u5b58\u50a8\u6570\u636e\u7684\u7c7b\u578b\uff0c\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u7b49\u3002\u8fd9\u5bf9\u4e8e\u786e\u4fdd\u8fd0\u7b97\u7684\u6b63\u786e\u6027\u548c\u6027\u80fd\u4f18\u5316\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<p><strong>\u5982\u4f55\u8ba1\u7b97\u77e9\u9635\u7684\u57fa\u672c\u7edf\u8ba1\u4fe1\u606f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u4e2d\u7684\u591a\u79cd\u51fd\u6570\u6765\u8ba1\u7b97\u77e9\u9635\u7684\u7edf\u8ba1\u4fe1\u606f\u3002\u4f8b\u5982\uff0c<code>np.mean(A)<\/code>\u53ef\u4ee5\u8ba1\u7b97\u77e9\u9635\u7684\u5747\u503c\uff0c<code>np.median(A)<\/code>\u53ef\u4ee5\u83b7\u5f97\u4e2d\u4f4d\u6570\uff0c<code>np.std(A)<\/code>\u7528\u4e8e\u8ba1\u7b97\u6807\u51c6\u5dee\u3002\u8fd9\u4e9b\u7edf\u8ba1\u4fe1\u606f\u6709\u52a9\u4e8e\u6570\u636e\u5206\u6790\u548c\u7406\u89e3\u77e9\u9635\u7684\u7279\u5f81\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u67e5\u770b\u77e9\u9635\u4fe1\u606f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u67e5\u770b\u77e9\u9635\u7684\u5f62\u72b6\u3001\u7ef4\u5ea6\u3001\u6570\u636e\u7c7b\u578b\u3001\u5143\u7d20\u503c\u7b49\u4fe1\u606f\u3002\u5177 [&hellip;]","protected":false},"author":3,"featured_media":1005852,"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\/1005847"}],"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=1005847"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1005847\/revisions"}],"predecessor-version":[{"id":1005853,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1005847\/revisions\/1005853"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1005852"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1005847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1005847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1005847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}