{"id":1060171,"date":"2024-12-31T15:31:37","date_gmt":"2024-12-31T07:31:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1060171.html"},"modified":"2024-12-31T15:31:39","modified_gmt":"2024-12-31T07:31:39","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%b1%82%e5%b9%b3%e5%9d%87%e6%95%b0%e6%96%b9%e5%b7%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1060171.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u6c42\u5e73\u5747\u6570\u65b9\u5dee"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/a9c94072-bb87-4567-8e37-70903c90b735.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u6c42\u5e73\u5747\u6570\u65b9\u5dee\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u6c42\u5e73\u5747\u6570\u548c\u65b9\u5dee\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5982\u4f7f\u7528\u57fa\u7840\u7684\u6570\u5b66\u516c\u5f0f\u3001NumPy\u5e93\u6216\u8005Pandas\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u662f\u6700\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3aNumPy\u662f\u4e13\u4e3a\u79d1\u5b66\u8ba1\u7b97\u8bbe\u8ba1\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528NumPy\u5e93\u6c42\u5e73\u5747\u6570\u548c\u65b9\u5dee\u7684\u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165NumPy\u5e93\u3002<\/li>\n<li>\u4f7f\u7528<code>np.mean()<\/code>\u51fd\u6570\u8ba1\u7b97\u5e73\u5747\u6570\u3002<\/li>\n<li>\u4f7f\u7528<code>np.var()<\/code>\u51fd\u6570\u8ba1\u7b97\u65b9\u5dee\u3002<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528\u57fa\u7840\u6570\u5b66\u516c\u5f0f\u3001NumPy\u5e93\u548cPandas\u5e93\u6765\u8ba1\u7b97\u5e73\u5747\u6570\u548c\u65b9\u5dee\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u57fa\u7840\u6570\u5b66\u516c\u5f0f\u8ba1\u7b97\u5e73\u5747\u6570\u548c\u65b9\u5dee<\/h3>\n<\/p>\n<p><h4>\u5e73\u5747\u6570<\/h4>\n<\/p>\n<p><p>\u5e73\u5747\u6570\u662f\u6570\u636e\u96c6\u7684\u7b97\u672f\u5e73\u5747\u503c\uff0c\u8ba1\u7b97\u65b9\u6cd5\u4e3a\u6240\u6709\u6570\u636e\u70b9\u7684\u603b\u548c\u9664\u4ee5\u6570\u636e\u70b9\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_mean(data):<\/p>\n<p>    return sum(data) \/ len(data)<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<p>mean = calculate_mean(data)<\/p>\n<p>print(&quot;\u5e73\u5747\u6570:&quot;, mean)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u65b9\u5dee<\/h4>\n<\/p>\n<p><p>\u65b9\u5dee\u662f\u6570\u636e\u96c6\u5404\u6570\u636e\u70b9\u4e0e\u5e73\u5747\u6570\u4e4b\u95f4\u5dee\u5f02\u7684\u5e73\u65b9\u7684\u5e73\u5747\u503c\uff0c\u8ba1\u7b97\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_variance(data):<\/p>\n<p>    mean = calculate_mean(data)<\/p>\n<p>    variance = sum((x - mean)  2 for x in data) \/ len(data)<\/p>\n<p>    return variance<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<p>variance = calculate_variance(data)<\/p>\n<p>print(&quot;\u65b9\u5dee:&quot;, variance)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93\u8ba1\u7b97\u5e73\u5747\u6570\u548c\u65b9\u5dee<\/h3>\n<\/p>\n<p><h4>\u5b89\u88c5NumPy\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u4e4b\u524d\uff0c\u9700\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\u3002\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><h4>\u8ba1\u7b97\u5e73\u5747\u6570\u548c\u65b9\u5dee<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u7b80\u5316\u8ba1\u7b97\u8fc7\u7a0b\uff0c\u5e76\u4e14\u63d0\u9ad8\u8ba1\u7b97\u7684\u6548\u7387\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528NumPy\u5e93\u8ba1\u7b97\u5e73\u5747\u6570\u548c\u65b9\u5dee\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u6570<\/strong><\/h2>\n<p>mean = np.mean(data)<\/p>\n<p>print(&quot;\u5e73\u5747\u6570:&quot;, mean)<\/p>\n<h2><strong>\u8ba1\u7b97\u65b9\u5dee<\/strong><\/h2>\n<p>variance = np.var(data)<\/p>\n<p>print(&quot;\u65b9\u5dee:&quot;, variance)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas\u5e93\u8ba1\u7b97\u5e73\u5747\u6570\u548c\u65b9\u5dee<\/h3>\n<\/p>\n<p><h4>\u5b89\u88c5Pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u9700\u8981\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><h4>\u8ba1\u7b97\u5e73\u5747\u6570\u548c\u65b9\u5dee<\/h4>\n<\/p>\n<p><p>Pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u5404\u79cd\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Pandas\u5e93\u8ba1\u7b97\u5e73\u5747\u6570\u548c\u65b9\u5dee\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data, columns=[&#39;values&#39;])<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u6570<\/strong><\/h2>\n<p>mean = df[&#39;values&#39;].mean()<\/p>\n<p>print(&quot;\u5e73\u5747\u6570:&quot;, mean)<\/p>\n<h2><strong>\u8ba1\u7b97\u65b9\u5dee<\/strong><\/h2>\n<p>variance = df[&#39;values&#39;].var(ddof=0)  # ddof=0\u8868\u793a\u6837\u672c\u65b9\u5dee<\/p>\n<p>print(&quot;\u65b9\u5dee:&quot;, variance)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001Python\u4e2d\u8ba1\u7b97\u5e73\u5747\u6570\u548c\u65b9\u5dee\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>\u6570\u636e\u5206\u6790\u4e0e\u7edf\u8ba1<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e0e\u7edf\u8ba1\u4e2d\uff0c\u5e73\u5747\u6570\u548c\u65b9\u5dee\u662f\u63cf\u8ff0\u6570\u636e\u96c6\u7684\u91cd\u8981\u7edf\u8ba1\u91cf\u3002\u5e73\u5747\u6570\u7528\u4e8e\u63cf\u8ff0\u6570\u636e\u96c6\u7684\u4e2d\u5fc3\u4f4d\u7f6e\uff0c\u800c\u65b9\u5dee\u7528\u4e8e\u63cf\u8ff0\u6570\u636e\u96c6\u7684\u79bb\u6563\u7a0b\u5ea6\u3002<\/p>\n<\/p>\n<p><h4><a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e0e\u6570\u636e\u6316\u6398<\/h4>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e0e\u6570\u636e\u6316\u6398\u4e2d\uff0c\u5e73\u5747\u6570\u548c\u65b9\u5dee\u5e38\u7528\u4e8e\u6570\u636e\u9884\u5904\u7406\u548c\u7279\u5f81\u5de5\u7a0b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e73\u5747\u6570\u548c\u65b9\u5dee\u8fdb\u884c\u6570\u636e\u6807\u51c6\u5316\u5904\u7406\uff0c\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001Python\u4e2d\u6c42\u5e73\u5747\u6570\u548c\u65b9\u5dee\u7684\u4f18\u5316\u7b56\u7565<\/h3>\n<\/p>\n<p><h4>\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u77e2\u91cf\u5316\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>NumPy\u5e93\u901a\u8fc7\u5e95\u5c42C\u8bed\u8a00\u5b9e\u73b0\uff0c\u6027\u80fd\u4f18\u8d8a\uff0c\u9002\u5408\u5927\u6570\u636e\u96c6\u7684\u8ba1\u7b97\u3002\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u77e2\u91cf\u5316\u8ba1\u7b97\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u5e76\u884c\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8d85\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Python\u7684\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u5e76\u884c\u8ba1\u7b97\u6765\u52a0\u901f\u5e73\u5747\u6570\u548c\u65b9\u5dee\u7684\u8ba1\u7b97\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6c42\u5e73\u5747\u6570\u548c\u65b9\u5dee\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u57fa\u7840\u6570\u5b66\u516c\u5f0f\u3001NumPy\u5e93\u548cPandas\u5e93\u3002<strong>\u4f7f\u7528NumPy\u5e93\u662f\u6700\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4e13\u4e3a\u79d1\u5b66\u8ba1\u7b97\u8bbe\u8ba1\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u51fd\u6570\u3002\u6b64\u5916\uff0cPandas\u5e93\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u65b9\u9762\u4e5f\u975e\u5e38\u5f3a\u5927\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u5e73\u5747\u6570\u548c\u65b9\u5dee\uff0c\u5e76\u7ed3\u5408\u4f18\u5316\u7b56\u7565\u4ee5\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u4e00\u7ec4\u6570\u7684\u5e73\u5747\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u548c<code>len()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u4e00\u7ec4\u6570\u7684\u5e73\u5747\u6570\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u5c06\u6240\u6709\u6570\u503c\u76f8\u52a0\uff0c\u7136\u540e\u9664\u4ee5\u6570\u503c\u7684\u6570\u91cf\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5217\u8868<code>data = [10, 20, 30]<\/code>\uff0c\u53ef\u4ee5\u7528\u4ee5\u4e0b\u4ee3\u7801\u8ba1\u7b97\u5e73\u5747\u6570\uff1a  <\/p>\n<pre><code class=\"language-python\">average = sum(data) \/ len(data)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8ba1\u7b97\u65b9\u5dee\uff1f<\/strong><br \/>\u8ba1\u7b97\u65b9\u5dee\u65f6\uff0c\u9996\u5148\u9700\u8981\u6c42\u51fa\u5e73\u5747\u6570\uff0c\u7136\u540e\u8ba1\u7b97\u6bcf\u4e2a\u6570\u4e0e\u5e73\u5747\u6570\u7684\u5dee\u503c\u7684\u5e73\u65b9\uff0c\u6700\u540e\u6c42\u51fa\u8fd9\u4e9b\u5e73\u65b9\u5dee\u7684\u5e73\u5747\u6570\u3002\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u7b80\u5316\u8fd9\u4e00\u8fc7\u7a0b\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528NumPy\u8ba1\u7b97\u65b9\u5dee\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\ndata = [10, 20, 30]\nvariance = np.var(data)\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Python\u5e93\u662f\u5426\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8ba1\u7b97\u5e73\u5747\u6570\u548c\u65b9\u5dee\uff1f<\/strong><br \/>\u5f53\u7136\uff0c\u4f7f\u7528Python\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\u5982NumPy\u548cPandas\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u7edf\u8ba1\u8ba1\u7b97\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u51fd\u6570\u6765\u8ba1\u7b97\u5e73\u5747\u6570\u548c\u65b9\u5dee\u3002\u4f8b\u5982\uff0c\u5728Pandas\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>mean()<\/code>\u548c<code>var()<\/code>\u65b9\u6cd5\u6765\u8ba1\u7b97\u6570\u636e\u6846\u6216\u7cfb\u5217\u7684\u5e73\u5747\u6570\u548c\u65b9\u5dee\uff0c\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6c42\u5e73\u5747\u6570\u548c\u65b9\u5dee\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5982\u4f7f\u7528\u57fa\u7840\u7684\u6570\u5b66\u516c\u5f0f\u3001NumPy\u5e93\u6216\u8005Pandas\u5e93\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1060180,"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\/1060171"}],"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=1060171"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060171\/revisions"}],"predecessor-version":[{"id":1060181,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060171\/revisions\/1060181"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1060180"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1060171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1060171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1060171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}