{"id":972061,"date":"2024-12-27T05:40:58","date_gmt":"2024-12-26T21:40:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/972061.html"},"modified":"2024-12-27T05:41:00","modified_gmt":"2024-12-26T21:41:00","slug":"python%e5%a6%82%e4%bd%95%e6%b1%82%e5%9d%87%e5%80%bc%e6%96%b9%e5%b7%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/972061.html","title":{"rendered":"python\u5982\u4f55\u6c42\u5747\u503c\u65b9\u5dee"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194857\/5ece4f6d-c23e-47e4-adc2-39265d7cde60.webp\" alt=\"python\u5982\u4f55\u6c42\u5747\u503c\u65b9\u5dee\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u8ba1\u7b97\u5747\u503c\u548c\u65b9\u5dee\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001numpy\u5e93\u548cpandas\u5e93\u3002\u63a8\u8350\u4f7f\u7528numpy\u5e93\u548cpandas\u5e93\uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u9ad8\u6548\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u51fd\u6570\u3002\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u800c\u51c6\u786e\u5730\u8ba1\u7b97\u6570\u636e\u7684\u5747\u503c\u548c\u65b9\u5dee\u3002<\/strong> \u5176\u4e2d\uff0cnumpy\u5e93\u56e0\u5176\u9ad8\u6548\u6027\u548c\u7b80\u4fbf\u6027\u800c\u5907\u53d7\u63a8\u5d07\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u8ba1\u7b97\u5747\u503c\u548c\u65b9\u5dee\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/p>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684\u529f\u80fd\u53ef\u4ee5\u7528\u6765\u624b\u52a8\u8ba1\u7b97\u5747\u503c\u548c\u65b9\u5dee\uff0c\u867d\u7136\u4e0d\u5982numpy\u548cpandas\u5feb\u6377\uff0c\u4f46\u6709\u52a9\u4e8e\u7406\u89e3\u57fa\u672c\u539f\u7406\u3002<\/p>\n<\/p>\n<ol>\n<li>\u8ba1\u7b97\u5747\u503c<\/li>\n<\/ol>\n<p><p>\u8981\u8ba1\u7b97\u4e00\u7ec4\u6570\u636e\u7684\u5747\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684sum()\u548clen()\u51fd\u6570\u3002\u5747\u503c\u7684\u8ba1\u7b97\u516c\u5f0f\u662f\u6240\u6709\u6570\u503c\u7684\u603b\u548c\u9664\u4ee5\u6570\u503c\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [10, 20, 30, 40, 50]<\/p>\n<p>mean = sum(data) \/ len(data)<\/p>\n<p>print(&quot;Mean:&quot;, mean)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u6570\u636e\u7684\u5217\u8868<code>data<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>sum(data)<\/code>\u8ba1\u7b97\u6240\u6709\u6570\u503c\u7684\u603b\u548c\uff0c\u5e76\u7528<code>len(data)<\/code>\u5f97\u5230\u6570\u503c\u7684\u6570\u91cf\uff0c\u6700\u540e\u5c06\u603b\u548c\u9664\u4ee5\u6570\u91cf\u5f97\u5230\u5747\u503c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u8ba1\u7b97\u65b9\u5dee<\/li>\n<\/ol>\n<p><p>\u65b9\u5dee\u662f\u6bcf\u4e2a\u6570\u636e\u70b9\u4e0e\u5747\u503c\u7684\u5dee\u7684\u5e73\u65b9\u7684\u5e73\u5747\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u624b\u52a8\u8ba1\u7b97\u8fd9\u4e9b\u5dee\u503c\u7684\u5e73\u65b9\u5e76\u6c42\u5e73\u5747\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mean = sum(data) \/ len(data)<\/p>\n<p>variance = sum((x - mean)  2 for x in data) \/ len(data)<\/p>\n<p>print(&quot;Variance:&quot;, variance)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u8ba1\u7b97\u5747\u503c\uff0c\u7136\u540e\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8ba1\u7b97\u6bcf\u4e2a\u6570\u636e\u70b9\u4e0e\u5747\u503c\u7684\u5dee\u7684\u5e73\u65b9\uff0c\u6700\u540e\u6c42\u8fd9\u4e9b\u5e73\u65b9\u7684\u5e73\u5747\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Numpy\u5e93<\/p>\n<\/p>\n<p><p>Numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u6570\u7ec4\u6570\u636e\u7684\u51fd\u6570\uff0c\u8fd9\u4f7f\u5f97\u8ba1\u7b97\u5747\u503c\u548c\u65b9\u5dee\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<ol>\n<li>\u8ba1\u7b97\u5747\u503c<\/li>\n<\/ol>\n<p><p>Numpy\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>mean()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u6570\u7ec4\u7684\u5747\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.array([10, 20, 30, 40, 50])<\/p>\n<p>mean = np.mean(data)<\/p>\n<p>print(&quot;Mean using numpy:&quot;, mean)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u6211\u4eec\u9996\u5148\u5c06\u6570\u636e\u8f6c\u6362\u4e3aNumpy\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528<code>np.mean()<\/code>\u51fd\u6570\u8ba1\u7b97\u5747\u503c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u8ba1\u7b97\u65b9\u5dee<\/li>\n<\/ol>\n<p><p>\u7c7b\u4f3c\u5730\uff0cNumpy\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>var()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u6570\u7ec4\u7684\u65b9\u5dee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">variance = np.var(data)<\/p>\n<p>print(&quot;Variance using numpy:&quot;, variance)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>np.var()<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u51fa\u6570\u636e\u7684\u65b9\u5dee\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Pandas\u5e93<\/p>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u901a\u5e38\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u4f7f\u7528Pandas\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8ba1\u7b97DataFrame\u6216Series\u7684\u5747\u503c\u548c\u65b9\u5dee\u3002<\/p>\n<\/p>\n<ol>\n<li>\u8ba1\u7b97\u5747\u503c<\/li>\n<\/ol>\n<p><p>Pandas\u7684<code>mean()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8eDataFrame\u6216Series\u6765\u8ba1\u7b97\u5747\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = pd.Series([10, 20, 30, 40, 50])<\/p>\n<p>mean = data.mean()<\/p>\n<p>print(&quot;Mean using pandas:&quot;, mean)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u6570\u636e\u521b\u5efa\u4e3a\u4e00\u4e2aPandas Series\uff0c\u7136\u540e\u4f7f\u7528<code>mean()<\/code>\u65b9\u6cd5\u8ba1\u7b97\u5747\u503c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u8ba1\u7b97\u65b9\u5dee<\/li>\n<\/ol>\n<p><p>Pandas\u7684<code>var()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u65b9\u5dee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">variance = data.var()<\/p>\n<p>print(&quot;Variance using pandas:&quot;, variance)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0e\u8ba1\u7b97\u5747\u503c\u7c7b\u4f3c\uff0c\u4f7f\u7528<code>var()<\/code>\u65b9\u6cd5\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u8ba1\u7b97\u65b9\u5dee\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3\u4e0e\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li>\u6570\u636e\u5206\u6790\u4e2d\u7684\u5e94\u7528<\/li>\n<\/ol>\n<p><p>\u5747\u503c\u548c\u65b9\u5dee\u662f\u6570\u636e\u5206\u6790\u4e2d\u6700\u57fa\u672c\u7684\u7edf\u8ba1\u91cf\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u6570\u636e\u7684\u96c6\u4e2d\u8d8b\u52bf\u548c\u79bb\u6563\u7a0b\u5ea6\u3002\u5728\u5927\u6570\u636e\u5206\u6790\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u7edf\u8ba1\u5efa\u6a21\u4e2d\uff0c\u5747\u503c\u548c\u65b9\u5dee\u662f\u6570\u636e\u9884\u5904\u7406\u548c\u7279\u5f81\u5de5\u7a0b\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\u65f6\uff0cPython\u5185\u7f6e\u51fd\u6570\u5df2\u7ecf\u8db3\u591f\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u6216\u8005\u9700\u8981\u8fdb\u884c\u590d\u6742\u6570\u636e\u5206\u6790\u65f6\uff0c\u4f7f\u7528Numpy\u548cPandas\u5e93\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4f18\u5316\u7684\u7b97\u6cd5\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u6ce8\u610f\u4e8b\u9879<\/li>\n<\/ol>\n<p><p>\u5728\u8ba1\u7b97\u65b9\u5dee\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u65b9\u5dee\u7684\u5b9a\u4e49\u3002\u4e0a\u6587\u4e2d\u7684\u65b9\u5dee\u8ba1\u7b97\u4f7f\u7528\u7684\u662f\u603b\u4f53\u65b9\u5dee\u516c\u5f0f\uff0c\u5373\u9664\u4ee5\u6570\u636e\u7684\u6570\u91cfn\uff1b\u800c\u6837\u672c\u65b9\u5dee\u5219\u9700\u8981\u9664\u4ee5n-1\u3002Numpy\u548cPandas\u7684<code>var()<\/code>\u51fd\u6570\u9ed8\u8ba4\u8ba1\u7b97\u6837\u672c\u65b9\u5dee\uff0c\u82e5\u9700\u8ba1\u7b97\u603b\u4f53\u65b9\u5dee\uff0c\u53ef\u8bbe\u7f6e\u53c2\u6570<code>ddof=0<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u603b\u4f53\u65b9\u5dee<\/p>\n<p>variance_population = np.var(data, ddof=0)<\/p>\n<p>print(&quot;Population Variance using numpy:&quot;, variance_population)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728\u5404\u79cd\u573a\u666f\u4e2d\u6709\u6548\u5730\u8ba1\u7b97\u6570\u636e\u7684\u5747\u503c\u548c\u65b9\u5dee\uff0c\u4e3a\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u7814\u7a76\u63d0\u4f9b\u6709\u529b\u652f\u6301\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\u5b57\u7684\u5747\u503c\u548c\u65b9\u5dee\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684<code>statistics<\/code>\u6a21\u5757\u6765\u8ba1\u7b97\u5747\u503c\u548c\u65b9\u5dee\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>mean()<\/code>\u548c<code>variance()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6570\u5b57\u5217\u8868<code>data = [10, 20, 30, 40, 50]<\/code>\uff0c\u53ef\u4ee5\u8fd9\u6837\u8ba1\u7b97\u5747\u503c\u548c\u65b9\u5dee\uff1a<\/p>\n<pre><code class=\"language-python\">import statistics\n\ndata = [10, 20, 30, 40, 50]\n\u5747\u503c = statistics.mean(data)\n\u65b9\u5dee = statistics.variance(data)\n\nprint(&quot;\u5747\u503c:&quot;, \u5747\u503c)\nprint(&quot;\u65b9\u5dee:&quot;, \u65b9\u5dee)\n<\/code><\/pre>\n<p><strong>\u5982\u679c\u6211\u60f3\u4f7f\u7528NumPy\u5e93\u6765\u8ba1\u7b97\u5747\u503c\u548c\u65b9\u5dee\uff0c\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u548c\u7edf\u8ba1\u529f\u80fd\u3002\u8981\u8ba1\u7b97\u5747\u503c\u548c\u65b9\u5dee\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.mean()<\/code>\u548c<code>numpy.var()<\/code>\u51fd\u6570\u3002\u9996\u5148\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\uff0c\u7136\u540e\u6267\u884c\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\ndata = np.array([10, 20, 30, 40, 50])\n\u5747\u503c = np.mean(data)\n\u65b9\u5dee = np.var(data)\n\nprint(&quot;\u5747\u503c:&quot;, \u5747\u503c)\nprint(&quot;\u65b9\u5dee:&quot;, \u65b9\u5dee)\n<\/code><\/pre>\n<p><strong>\u8ba1\u7b97\u5747\u503c\u548c\u65b9\u5dee\u65f6\uff0c\u6211\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u8ba1\u7b97\u5747\u503c\u548c\u65b9\u5dee\u65f6\uff0c\u786e\u4fdd\u6570\u636e\u96c6\u4e0d\u4e3a\u7a7a\uff0c\u56e0\u4e3a\u7a7a\u5217\u8868\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u6b64\u5916\uff0c\u5bf9\u4e8e\u65b9\u5dee\u7684\u8ba1\u7b97\uff0c\u4e86\u89e3\u201c\u6837\u672c\u65b9\u5dee\u201d\u548c\u201c\u603b\u4f53\u65b9\u5dee\u201d\u7684\u533a\u522b\u975e\u5e38\u91cd\u8981\u3002\u6837\u672c\u65b9\u5dee\u901a\u5e38\u4f7f\u7528<code>n-1<\/code>\u4f5c\u4e3a\u5206\u6bcd\uff0c\u800c\u603b\u4f53\u65b9\u5dee\u4f7f\u7528<code>n<\/code>\u3002\u5728Python\u7684<code>statistics<\/code>\u6a21\u5757\u4e2d\uff0c<code>variance()<\/code>\u51fd\u6570\u8ba1\u7b97\u7684\u662f\u6837\u672c\u65b9\u5dee\uff0c\u800c<code>pvariance()<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u603b\u4f53\u65b9\u5dee\u3002\u6839\u636e\u4f60\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u51fd\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u8ba1\u7b97\u5747\u503c\u548c\u65b9\u5dee\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001numpy\u5e93\u548cpandas\u5e93\u3002\u63a8\u8350\u4f7f\u7528num [&hellip;]","protected":false},"author":3,"featured_media":972062,"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\/972061"}],"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=972061"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972061\/revisions"}],"predecessor-version":[{"id":972063,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972061\/revisions\/972063"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/972062"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=972061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=972061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=972061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}