{"id":60140,"date":"2023-10-31T11:50:57","date_gmt":"2023-10-31T03:50:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=60140"},"modified":"2023-10-31T11:50:59","modified_gmt":"2023-10-31T03:50:59","slug":"python%e6%80%8e%e4%b9%88%e6%b1%82%e5%b9%b3%e5%9d%87%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/60140.html","title":{"rendered":"Python\u600e\u4e48\u6c42\u5e73\u5747\u6570"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\">\n<p>\u6c42\u5e73\u5747\u6570\u662f\u6570\u636e\u5904\u7406\u4e2d\u7684\u5e38\u89c1\u64cd\u4f5c\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\uff0c\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\uff1a1.\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u6c42\u5e73\u5747\u6570\uff1b2.\u901a\u8fc7NumPy\u5e93\u6c42\u53d6\uff1b3.\u8003\u8651\u52a0\u6743\u5e73\u5747\u6570\uff1b4.\u5904\u7406\u4e0d\u540c\u6570\u636e\u7ed3\u6784\u7684\u5e73\u5747\u503c\uff1b5.\u6ce8\u610f\u5f02\u5e38\u503c\u7684\u5f71\u54cd\u3002\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3\u5e73\u5747\u6570\u7684\u57fa\u672c\u6982\u5ff5\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">1.\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u6c42\u5e73\u5747\u6570<\/h2>\n\n\n\n<p>Python\u63d0\u4f9b\u4e86sum\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u6570\u503c\u5e8f\u5217\u7684\u603b\u548c\u3002\u7ed3\u5408len\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u8ba1\u7b97\u5e73\u5747\u6570\u3002<\/p>\n\n\n\n<p>data = [10, 20, 30, 40, 50]<\/p>\n\n\n\n<p>avg = sum(data) \/ len(data)<\/p>\n\n\n\n<p>print(avg)&nbsp; # \u8f93\u51fa30.0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u901a\u8fc7NumPy\u5e93\u6c42\u53d6<\/h2>\n\n\n\n<p>NumPy\u662fPython\u4e2d\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\u7684\u5e93\uff0c\u5176\u63d0\u4f9b\u4e86mean\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u6c42\u53d6\u5e73\u5747\u6570\u3002<\/p>\n\n\n\n<p>import numpy as np<\/p>\n\n\n\n<p>data = np.array([10, 20, 30, 40, 50])<\/p>\n\n\n\n<p>avg = np.mean(data)<\/p>\n\n\n\n<p>print(avg)&nbsp; # \u8f93\u51fa30.0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u8003\u8651\u52a0\u6743\u5e73\u5747\u6570<\/h2>\n\n\n\n<p>\u5728\u67d0\u4e9b\u573a\u5408\uff0c\u6570\u636e\u7684\u6743\u91cd\u4e0d\u540c\uff0c\u6b64\u65f6\u6211\u4eec\u9700\u8981\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570\u3002NumPy\u7684average\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002<\/p>\n\n\n\n<p>weights = np.array([0.1, 0.2, 0.3, 0.2, 0.2])<\/p>\n\n\n\n<p>weighted_avg = np.average(data, weights=weights)<\/p>\n\n\n\n<p>print(weighted_avg)&nbsp; # \u8f93\u51fa32.0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.\u5904\u7406\u4e0d\u540c\u6570\u636e\u7ed3\u6784\u7684\u5e73\u5747\u503c<\/h2>\n\n\n\n<p>\u9664\u4e86\u5217\u8868\uff0c\u6211\u4eec\u53ef\u80fd\u8fd8\u4f1a\u9047\u5230\u5143\u7ec4\u3001\u96c6\u5408\u6216\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u3002\u4e0d\u8bba\u54ea\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u6570\u7ec4\u6765\u6c42\u5e73\u5747\u6570\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.\u6ce8\u610f\u5f02\u5e38\u503c\u7684\u5f71\u54cd<\/h2>\n\n\n\n<p>\u6570\u636e\u4e2d\u53ef\u80fd\u5b58\u5728\u5f02\u5e38\u503c\uff0c\u8fd9\u4e9b\u503c\u4f1a\u5bf9\u5e73\u5747\u6570\u9020\u6210\u5f71\u54cd\u3002\u5728\u8ba1\u7b97\u5e73\u5747\u6570\u524d\uff0c\u6211\u4eec\u5e94\u5148\u68c0\u67e5\u6570\u636e\uff0c\u8003\u8651\u662f\u5426\u9700\u8981\u6392\u9664\u5f02\u5e38\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528NumPy\u7684percentile\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u767e\u5206\u4f4d\u6570\u6392\u9664\u6781\u7aef\u503c\u3002<\/p>\n\n\n\n<p>\u603b\u7ed3:\u6c42\u5e73\u5747\u6570\u5728Python\u4e2d\u662f\u4e00\u4e2a\u76f4\u89c2\u800c\u7b80\u5355\u7684\u4efb\u52a1\uff0c\u65e0\u8bba\u6211\u4eec\u4f7f\u7528\u57fa\u672c\u7684Python\u51fd\u6570\u8fd8\u662f\u5229\u7528NumPy\u5e93\u3002\u4f46\u91cd\u8981\u7684\u662f\uff0c\u8981\u6839\u636e\u5b9e\u9645\u6570\u636e\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u6ce8\u610f\u6570\u636e\u8d28\u91cf\u7684\u5f71\u54cd\u3002\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u8003\u8651\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u5f02\u5e38\u503c\u5904\u7406\u662f\u786e\u4fdd\u7ed3\u679c\u51c6\u786e\u6027\u7684\u5173\u952e\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"484\" height=\"300\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/10\/\u5982\u4f55\u5728\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5b89\u88c5\u548c\u7ba1\u7406\u5b57\u4f53-2023-10-31T114931.817-1.png?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\" class=\"wp-image-60144\" \/><\/figure><\/div>\n\n\n<p><strong>\u5e38\u89c1\u95ee\u7b54<\/strong>\uff1a<\/p>\n\n\n\n<ul>\n<li>\u95ee: \u5728Python\u4e2d\uff0c\u4e3a\u4ec0\u4e48\u6211\u4eec\u66f4\u504f\u5411\u4e8e\u4f7f\u7528NumPy\u5e93\u6765\u5904\u7406\u6570\u5b57\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4ec5\u4f9d\u8d56\u4e8e\u5185\u7f6e\u51fd\u6570\uff1f<\/li>\n\n\n\n<li>\u7b54\uff1a NumPy\u5e93\u4e3a\u6570\u503c\u8ba1\u7b97\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u652f\u6301\uff0c\u5b83\u4f18\u5316\u4e86\u6570\u7ec4\u548c\u77e9\u9635\u7684\u8ba1\u7b97\uff0c\u5e76\u63d0\u4f9b\u4e86\u5927\u91cf\u6570\u5b66\u51fd\u6570\u6765\u64cd\u4f5c\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u3002\u800cPython\u7684\u5185\u7f6e\u51fd\u6570\u867d\u7136\u57fa\u7840\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u6216\u590d\u6742\u8fd0\u7b97\u65f6\uff0c\u6548\u7387\u53ef\u80fd\u4e0d\u53caNumPy\u3002<\/li>\n<\/ul>\n\n\n\n<ul>\n<li>\u95ee: \u5f53\u6211\u6709\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\u65f6\uff0c\u76f4\u63a5\u8ba1\u7b97\u5e73\u5747\u503c\u662f\u5426\u5408\u9002\uff1f<\/li>\n\n\n\n<li>\u7b54\uff1a \u5f53\u6570\u636e\u96c6\u975e\u5e38\u5927\u65f6\uff0c\u9996\u5148\u8981\u8003\u8651\u6570\u636e\u7684\u8d28\u91cf\u3002\u5f02\u5e38\u503c\u6216\u79bb\u7fa4\u503c\u53ef\u80fd\u4f1a\u5bf9\u5e73\u5747\u6570\u4ea7\u751f\u5f88\u5927\u7684\u5f71\u54cd\u3002\u5728\u8ba1\u7b97\u5927\u6570\u636e\u96c6\u7684\u5e73\u5747\u6570\u4e4b\u524d\uff0c\u53ef\u80fd\u9700\u8981\u5148\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u5f02\u5e38\u503c\u5904\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<ul>\n<li>\u95ee: \u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u4e2d\u4f4d\u6570\uff0c\u5b83\u4e0e\u5e73\u5747\u6570\u6709\u4f55\u4e0d\u540c\uff1f<\/li>\n\n\n\n<li>\u7b54\uff1a \u4e2d\u4f4d\u6570\u662f\u5c06\u6570\u636e\u96c6\u6392\u5e8f\u540e\u4f4d\u4e8e\u4e2d\u95f4\u7684\u6570\u503c\uff0c\u800c\u5e73\u5747\u6570\u662f\u6240\u6709\u6570\u503c\u7684\u603b\u548c\u9664\u4ee5\u6570\u91cf\u3002\u5728Python\u7684NumPy\u5e93\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528median\u51fd\u6570\u6765\u8ba1\u7b97\u4e2d\u4f4d\u6570\u3002\u4e2d\u4f4d\u6570\u5bf9\u5f02\u5e38\u503c\u4e0d\u654f\u611f\uff0c\u6709\u65f6\u53ef\u80fd\u6bd4\u5e73\u5747\u6570\u66f4\u80fd\u53cd\u6620\u6570\u636e\u7684\u771f\u5b9e\u60c5\u51b5\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u6c42\u5e73\u5747\u6570\u662f\u6570\u636e\u5904\u7406\u4e2d\u7684\u5e38\u89c1\u64cd\u4f5c\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\uff0c\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\uff1a1.\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u6c42 [&hellip;]","protected":false},"author":7,"featured_media":60144,"comment_status":"closed","ping_status":"open","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\/60140"}],"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\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=60140"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/60140\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/60144"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=60140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=60140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=60140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}