{"id":1133697,"date":"2025-01-08T21:08:54","date_gmt":"2025-01-08T13:08:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133697.html"},"modified":"2025-01-08T21:08:56","modified_gmt":"2025-01-08T13:08:56","slug":"python%e5%8a%a0%e6%9d%83%e5%b9%b3%e5%9d%87%e6%95%b0%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97%e5%99%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1133697.html","title":{"rendered":"python\u52a0\u6743\u5e73\u5747\u6570\u5982\u4f55\u8ba1\u7b97\u5668"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102851\/83592a94-8455-495d-a59b-0a215f1ee0da.webp\" alt=\"python\u52a0\u6743\u5e73\u5747\u6570\u5982\u4f55\u8ba1\u7b97\u5668\" \/><\/p>\n<p><p> <strong>\u8ba1\u7b97Python\u52a0\u6743\u5e73\u5747\u6570\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u624b\u52a8\u8ba1\u7b97\u3001numpy\u5e93\u6216pandas\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6709\u6548\u7684\u65b9\u6cd5\uff1a\u624b\u52a8\u8ba1\u7b97\u3001\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528pandas\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528numpy\u5e93<\/strong>\u662f\u6700\u5e38\u7528\u548c\u6700\u4fbf\u6377\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684\u51fd\u6570\u6765\u5904\u7406\u52a0\u6743\u5e73\u5747\u6570\u8ba1\u7b97\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u8ba9\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u624b\u52a8\u8ba1\u7b97<\/h2>\n<\/p>\n<p><p>\u5728\u624b\u52a8\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570\u65f6\uff0c\u6211\u4eec\u9700\u8981\u4e24\u4e2a\u5217\u8868\uff1a\u4e00\u4e2a\u662f\u6570\u503c\u5217\u8868\uff0c\u53e6\u4e00\u4e2a\u662f\u6743\u91cd\u5217\u8868\u3002\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>[ \\text{\u52a0\u6743\u5e73\u5747\u6570} = \\frac{\\sum_{i=1}^{n} (x_i \\cdot w_i)}{\\sum_{i=1}^{n} w_i} ]<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c( x_i ) \u662f\u6570\u503c\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c( w_i ) \u662f\u6743\u91cd\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def weighted_average(values, weights):<\/p>\n<p>    weighted_sum = sum(v * w for v, w in zip(values, weights))<\/p>\n<p>    total_weight = sum(weights)<\/p>\n<p>    return weighted_sum \/ total_weight<\/p>\n<p>values = [10, 20, 30]<\/p>\n<p>weights = [1, 2, 3]<\/p>\n<p>print(weighted_average(values, weights))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8ba1\u7b97\u6bcf\u4e2a\u6570\u503c\u548c\u5176\u5bf9\u5e94\u6743\u91cd\u7684\u4e58\u79ef\uff0c\u7136\u540e\u6c42\u548c\u3002\u63a5\u7740\uff0c\u6211\u4eec\u8ba1\u7b97\u6743\u91cd\u7684\u603b\u548c\uff0c\u6700\u540e\u5c06\u52a0\u6743\u548c\u9664\u4ee5\u603b\u6743\u91cd\u5f97\u5230\u52a0\u6743\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528numpy\u5e93<\/h2>\n<\/p>\n<p><p><strong>numpy<\/strong> \u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u51fd\u6570\u6765\u7b80\u5316\u5404\u79cd\u7edf\u8ba1\u8ba1\u7b97\u3002\u4f7f\u7528numpy\u5e93\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570\u975e\u5e38\u65b9\u4fbf\uff0c\u56e0\u4e3a\u5b83\u6709\u4e00\u4e2a\u4e13\u95e8\u7684\u51fd\u6570<code>numpy.average()<\/code>\uff0c\u5141\u8bb8\u6211\u4eec\u76f4\u63a5\u4f20\u5165\u6743\u91cd\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>values = np.array([10, 20, 30])<\/p>\n<p>weights = np.array([1, 2, 3])<\/p>\n<p>weighted_avg = np.average(values, weights=weights)<\/p>\n<p>print(weighted_avg)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>numpy.array<\/code>\u5c06\u5217\u8868\u8f6c\u6362\u4e3anumpy\u6570\u7ec4\uff0c\u7136\u540e\u8c03\u7528<code>numpy.average()<\/code>\u51fd\u6570\uff0c\u4f20\u5165\u6570\u503c\u548c\u6743\u91cd\u6570\u7ec4\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u81ea\u52a8\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528pandas\u5e93<\/h2>\n<\/p>\n<p><p><strong>pandas<\/strong> \u662f\u53e6\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528pandas\u5e93\u6765\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570\uff0c\u7279\u522b\u662f\u5f53\u6211\u4eec\u5904\u7406\u7684\u662f\u6570\u636e\u8868\u6216\u6570\u636e\u6846\u65f6\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u6709\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;values&#39;: [10, 20, 30], &#39;weights&#39;: [1, 2, 3]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>weighted_avg = (df[&#39;values&#39;] * df[&#39;weights&#39;]).sum() \/ df[&#39;weights&#39;].sum()<\/p>\n<p>print(weighted_avg)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6570\u503c\u548c\u6743\u91cd\u7684\u6570\u636e\u6846<code>DataFrame<\/code>\uff0c\u7136\u540e\u8ba1\u7b97\u6bcf\u4e2a\u6570\u503c\u548c\u5176\u5bf9\u5e94\u6743\u91cd\u7684\u4e58\u79ef\uff0c\u5e76\u6c42\u548c\u3002\u63a5\u7740\uff0c\u6211\u4eec\u8ba1\u7b97\u6743\u91cd\u7684\u603b\u548c\uff0c\u6700\u540e\u5c06\u52a0\u6743\u548c\u9664\u4ee5\u603b\u6743\u91cd\u5f97\u5230\u52a0\u6743\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u52a0\u6743\u5e73\u5747\u6570\u7684\u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><p>\u52a0\u6743\u5e73\u5747\u6570\u5728\u8bb8\u591a\u5b9e\u9645\u5e94\u7528\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u8003\u8651\u4e0d\u540c\u9879\u76ee\u7684\u91cd\u8981\u6027\u6216\u6743\u91cd\u65f6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u91d1\u878d\u6295\u8d44\u7ec4\u5408<\/h3>\n<\/p>\n<p><p>\u5728\u91d1\u878d\u9886\u57df\uff0c\u52a0\u6743\u5e73\u5747\u6570\u901a\u5e38\u7528\u4e8e\u8ba1\u7b97\u6295\u8d44\u7ec4\u5408\u7684\u9884\u671f\u6536\u76ca\u7387\u3002\u6bcf\u4e2a\u8d44\u4ea7\u7684\u6536\u76ca\u7387\u88ab\u8d4b\u4e88\u4e00\u4e2a\u6743\u91cd\uff0c\u6743\u91cd\u901a\u5e38\u662f\u8be5\u8d44\u4ea7\u5728\u6574\u4e2a\u6295\u8d44\u7ec4\u5408\u4e2d\u7684\u6bd4\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5b66\u751f\u6210\u7ee9\u8bc4\u4f30<\/h3>\n<\/p>\n<p><p>\u5728\u6559\u80b2\u9886\u57df\uff0c\u52a0\u6743\u5e73\u5747\u6570\u53ef\u4ee5\u7528\u4e8e\u8bc4\u4f30\u5b66\u751f\u7684\u603b\u6210\u7ee9\u3002\u4e0d\u540c\u7684\u8003\u8bd5\u6216\u4f5c\u4e1a\u53ef\u80fd\u6709\u4e0d\u540c\u7684\u91cd\u8981\u6027\u6216\u5206\u503c\uff0c\u4f7f\u7528\u52a0\u6743\u5e73\u5747\u6570\u53ef\u4ee5\u66f4\u516c\u5e73\u5730\u8bc4\u4f30\u5b66\u751f\u7684\u6574\u4f53\u8868\u73b0\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u6570\u636e\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u52a0\u6743\u5e73\u5747\u6570\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u6709\u504f\u5dee\u7684\u6570\u636e\u3002\u901a\u8fc7\u7ed9\u4e0d\u540c\u7684\u6570\u636e\u70b9\u8d4b\u4e88\u4e0d\u540c\u7684\u6743\u91cd\uff0c\u53ef\u4ee5\u5f97\u5230\u66f4\u51c6\u786e\u7684\u5206\u6790\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f18\u5316\u548c\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<p><p>\u867d\u7136\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570\u76f8\u5bf9\u7b80\u5355\uff0c\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6709\u4e00\u4e9b\u4f18\u5316\u548c\u6ce8\u610f\u4e8b\u9879\u9700\u8981\u8003\u8651\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u6570\u636e\u9a8c\u8bc1<\/h3>\n<\/p>\n<p><p>\u786e\u4fdd\u8f93\u5165\u7684\u6570\u636e\u662f\u6709\u6548\u7684\u3002\u4f8b\u5982\uff0c\u6743\u91cd\u4e0d\u80fd\u4e3a\u8d1f\u6570\uff0c\u6570\u503c\u548c\u6743\u91cd\u7684\u5217\u8868\u957f\u5ea6\u5fc5\u987b\u76f8\u540c\u3002\u5982\u679c\u6570\u636e\u65e0\u6548\uff0c\u8ba1\u7b97\u7ed3\u679c\u53ef\u80fd\u4f1a\u4e0d\u51c6\u786e\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u4f7f\u7528numpy\u6216pandas\u5e93\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6027\u80fd\u3002\u8fd9\u4e9b\u5e93\u662f\u7528C\u8bed\u8a00\u7f16\u5199\u7684\uff0c\u8ba1\u7b97\u6548\u7387\u8fdc\u9ad8\u4e8e\u7eafPython\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u5904\u7406\u7f3a\u5931\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u6570\u636e\u4e2d\uff0c\u53ef\u80fd\u4f1a\u6709\u7f3a\u5931\u503c\u3002\u5728\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570\u65f6\uff0c\u9700\u8981\u51b3\u5b9a\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u7f3a\u5931\u503c\u3002\u53ef\u4ee5\u9009\u62e9\u5ffd\u7565\u7f3a\u5931\u503c\uff0c\u6216\u8005\u4f7f\u7528\u586b\u5145\u65b9\u6cd5\u6765\u5904\u7406\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>values = np.array([10, 20, np.nan, 30])<\/p>\n<p>weights = np.array([1, 2, 1, 3])<\/p>\n<h2><strong>\u4f7f\u7528pandas\u5904\u7406\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;values&#39;: values, &#39;weights&#39;: weights})<\/p>\n<p>df = df.dropna()  # \u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c<\/p>\n<p>weighted_avg = (df[&#39;values&#39;] * df[&#39;weights&#39;]).sum() \/ df[&#39;weights&#39;].sum()<\/p>\n<p>print(weighted_avg)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u751f\u6210\u5305\u542b\u7f3a\u5931\u503c\u7684\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528pandas\u7684<code>dropna()<\/code>\u51fd\u6570\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\uff0c\u6700\u540e\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u6269\u5c55\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u52a0\u6743\u5e73\u5747\u6570\u8ba1\u7b97\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u6269\u5c55\u8fd9\u4e00\u6982\u5ff5\uff0c\u5e94\u7528\u4e8e\u66f4\u590d\u6742\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u65f6\u95f4\u52a0\u6743\u5e73\u5747<\/h3>\n<\/p>\n<p><p>\u5728\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u65f6\u95f4\u52a0\u6743\u5e73\u5747\u6570\u6765\u8003\u8651\u6570\u636e\u70b9\u7684\u65f6\u95f4\u56e0\u7d20\u3002\u8f83\u65b0\u7684\u6570\u636e\u70b9\u901a\u5e38\u66f4\u91cd\u8981\uff0c\u56e0\u6b64\u53ef\u4ee5\u8d4b\u4e88\u8f83\u9ad8\u7684\u6743\u91cd\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>dates = pd.date_range(&#39;20230101&#39;, periods=4)<\/p>\n<p>values = np.array([10, 20, 30, 40])<\/p>\n<p>weights = np.array([1, 2, 3, 4])<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;dates&#39;: dates, &#39;values&#39;: values, &#39;weights&#39;: weights})<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u52a0\u6743\u5e73\u5747\u6570<\/strong><\/h2>\n<p>df = df.set_index(&#39;dates&#39;)<\/p>\n<p>weighted_avg = (df[&#39;values&#39;] * df[&#39;weights&#39;]).sum() \/ df[&#39;weights&#39;].sum()<\/p>\n<p>print(weighted_avg)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u52a0\u6743\u56de\u5f52\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u56de\u5f52\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u52a0\u6743\u56de\u5f52\u6765\u5904\u7406\u5f02\u65b9\u5dee\u6570\u636e\u3002\u901a\u8fc7\u7ed9\u4e0d\u540c\u7684\u6570\u636e\u70b9\u8d4b\u4e88\u4e0d\u540c\u7684\u6743\u91cd\uff0c\u53ef\u4ee5\u63d0\u9ad8\u56de\u5f52\u6a21\u578b\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import statsmodels.api as sm<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>X = np.array([1, 2, 3, 4]).reshape(-1, 1)<\/p>\n<p>y = np.array([10, 20, 30, 40])<\/p>\n<p>weights = np.array([1, 2, 3, 4])<\/p>\n<h2><strong>\u6dfb\u52a0\u5e38\u6570\u9879<\/strong><\/h2>\n<p>X = sm.add_constant(X)<\/p>\n<h2><strong>\u8fdb\u884c\u52a0\u6743\u56de\u5f52\u5206\u6790<\/strong><\/h2>\n<p>model = sm.WLS(y, X, weights=weights)<\/p>\n<p>results = model.fit()<\/p>\n<p>print(results.summary())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>statsmodels<\/code>\u5e93\u8fdb\u884c\u52a0\u6743\u56de\u5f52\u5206\u6790\u3002\u9996\u5148\u751f\u6210\u793a\u4f8b\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528<code>WLS<\/code>\uff08\u52a0\u6743\u6700\u5c0f\u4e8c\u4e58\u6cd5\uff09\u51fd\u6570\u8fdb\u884c\u56de\u5f52\u5206\u6790\uff0c\u5e76\u8f93\u51fa\u56de\u5f52\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u8ba1\u7b97Python\u52a0\u6743\u5e73\u5747\u6570\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u624b\u52a8\u8ba1\u7b97\u3001\u4f7f\u7528numpy\u5e93\u548c\u4f7f\u7528pandas\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u52a0\u6743\u5e73\u5747\u6570\u5e7f\u6cdb\u5e94\u7528\u4e8e\u91d1\u878d\u6295\u8d44\u3001\u5b66\u751f\u6210\u7ee9\u8bc4\u4f30\u548c\u6570\u636e\u5206\u6790\u7b49\u9886\u57df\u3002\u901a\u8fc7\u4f18\u5316\u8ba1\u7b97\u65b9\u6cd5\u548c\u5904\u7406\u7f3a\u5931\u503c\uff0c\u53ef\u4ee5\u63d0\u9ad8\u8ba1\u7b97\u7684\u51c6\u786e\u6027\u548c\u6548\u7387\u3002\u6269\u5c55\u5e94\u7528\u5305\u62ec\u65f6\u95f4\u52a0\u6743\u5e73\u5747\u548c\u52a0\u6743\u56de\u5f52\u5206\u6790\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u52a0\u6743\u5e73\u5747\u6570\u7684\u8ba1\u7b97\u65b9\u6cd5\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u8bf7\u968f\u65f6\u4e0e\u6211\u8054\u7cfb\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u52a0\u6743\u5e73\u5747\u6570\u7684\u8ba1\u7b97\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528NumPy\u5e93\u6765\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>numpy.average()<\/code>\u51fd\u6570\uff0c\u5e76\u4f20\u5165\u6570\u636e\u548c\u6743\u91cd\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\ndata = [10, 20, 30]\nweights = [0.1, 0.3, 0.6]\nweighted_average = np.average(data, weights=weights)\nprint(weighted_average)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4f1a\u8f93\u51fa\u52a0\u6743\u5e73\u5747\u6570\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u52a0\u6743\u5e73\u5747\u6570\u4e0e\u666e\u901a\u5e73\u5747\u6570\u6709\u4ec0\u4e48\u4e0d\u540c\u4e4b\u5904\uff1f<\/strong><br \/>\u52a0\u6743\u5e73\u5747\u6570\u8003\u8651\u4e86\u6bcf\u4e2a\u6570\u636e\u70b9\u7684\u91cd\u8981\u6027\u6216\u6743\u91cd\uff0c\u800c\u666e\u901a\u5e73\u5747\u6570\u5219\u662f\u6240\u6709\u6570\u636e\u70b9\u7684\u7b80\u5355\u5e73\u5747\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u70b9\u53ef\u80fd\u5e76\u4e0d\u662f\u540c\u7b49\u91cd\u8981\uff0c\u4f7f\u7528\u52a0\u6743\u5e73\u5747\u6570\u53ef\u4ee5\u66f4\u51c6\u786e\u5730\u53cd\u6620\u6574\u4f53\u8d8b\u52bf\u3002\u4f8b\u5982\uff0c\u5728\u8bc4\u4f30\u5b66\u751f\u7684\u6210\u7ee9\u65f6\uff0c\u671f\u672b\u8003\u8bd5\u53ef\u80fd\u6bd4\u5e73\u65f6\u4f5c\u4e1a\u66f4\u91cd\u8981\uff0c\u56e0\u6b64\u53ef\u4ee5\u7ed9\u4e88\u66f4\u9ad8\u7684\u6743\u91cd\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\u4ee5\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570\uff1f<\/strong><br \/>\u5904\u7406\u7f3a\u5931\u503c\u65f6\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528NumPy\u6216Pandas\u5e93\u4e2d\u7684\u65b9\u6cd5\u8fc7\u6ee4\u6389\u7f3a\u5931\u7684\u6570\u636e\u548c\u76f8\u5e94\u7684\u6743\u91cd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.isnan()<\/code>\u6765\u68c0\u67e5\u7f3a\u5931\u503c\uff0c\u5e76\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15\u6765\u6392\u9664\u5b83\u4eec\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u7f3a\u5931\u503c\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\ndata = [10, np.nan, 30]\nweights = [0.1, 0.3, 0.6]\n\n# \u8fc7\u6ee4\u6389\u7f3a\u5931\u503c\nfiltered_data = [d for d in data if not np.isnan(d)]\nfiltered_weights = [w for d, w in zip(data, weights) if not np.isnan(d)]\n\nweighted_average = np.average(filtered_data, weights=filtered_weights)\nprint(weighted_average)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u8ba1\u7b97\u65f6\u4e0d\u53d7\u7f3a\u5931\u503c\u7684\u5f71\u54cd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8ba1\u7b97Python\u52a0\u6743\u5e73\u5747\u6570\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u624b\u52a8\u8ba1\u7b97\u3001numpy\u5e93\u6216pandas\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6709\u6548\u7684\u65b9\u6cd5\uff1a\u624b [&hellip;]","protected":false},"author":3,"featured_media":1133702,"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\/1133697"}],"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=1133697"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133697\/revisions"}],"predecessor-version":[{"id":1133703,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133697\/revisions\/1133703"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1133702"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1133697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1133697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1133697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}