{"id":1079271,"date":"2025-01-08T12:19:20","date_gmt":"2025-01-08T04:19:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1079271.html"},"modified":"2025-01-08T12:19:22","modified_gmt":"2025-01-08T04:19:22","slug":"python%e7%ae%b1%e5%bd%a2%e5%9b%be%e5%a6%82%e4%bd%95%e5%8e%bb%e9%99%a4%e5%bc%82%e5%b8%b8%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1079271.html","title":{"rendered":"python\u7bb1\u5f62\u56fe\u5982\u4f55\u53bb\u9664\u5f02\u5e38\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182403\/9117d65f-d65e-432a-8c1d-a80ed5c2e25a.webp\" alt=\"python\u7bb1\u5f62\u56fe\u5982\u4f55\u53bb\u9664\u5f02\u5e38\u503c\" \/><\/p>\n<p><p> <strong>\u8981\u53bb\u9664Python\u7bb1\u5f62\u56fe\u4e2d\u7684\u5f02\u5e38\u503c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u8fc7\u6ee4\u548c\u6e05\u7406\u6280\u672f\u6765\u5904\u7406\u6570\u636e\u3002<\/strong> \u7bb1\u5f62\u56fe\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u7528\u4e8e\u5c55\u793a\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u53ca\u5176\u5f02\u5e38\u503c\u3002\u4e3a\u4e86\u53bb\u9664\u5f02\u5e38\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8ba1\u7b97\u56db\u5206\u4f4d\u8ddd\uff08IQR\uff09<\/strong><\/li>\n<li><strong>\u786e\u5b9a\u4e0a\u4e0b\u754c\u9650<\/strong><\/li>\n<li><strong>\u7b5b\u9009\u6570\u636e<\/strong><\/li>\n<li><strong>\u91cd\u65b0\u7ed8\u5236\u7bb1\u5f62\u56fe<\/strong><\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u6bcf\u4e00\u6b65\u7684\u5177\u4f53\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8ba1\u7b97\u56db\u5206\u4f4d\u8ddd\uff08IQR\uff09<\/h3>\n<\/p>\n<p><p>\u56db\u5206\u4f4d\u8ddd\uff08Interquartile Range, IQR\uff09\u662f\u6570\u636e\u5206\u5e03\u4e2d\u4f4d\u6570\u7684\u8303\u56f4\u3002\u5b83\u7684\u8ba1\u7b97\u65b9\u6cd5\u662f\u4e0a\u56db\u5206\u4f4d\u6570\uff08Q3\uff09\u51cf\u53bb\u4e0b\u56db\u5206\u4f4d\u6570\uff08Q1\uff09\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u6570\u636e\u5b58\u50a8\u5728DataFrame\u4e2d<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;data&#39;: [1, 2, 5, 7, 9, 10, 15, 18, 19, 22, 24, 25, 30, 35, 40]})<\/p>\n<h2><strong>\u8ba1\u7b97Q1\u548cQ3<\/strong><\/h2>\n<p>Q1 = df[&#39;data&#39;].quantile(0.25)<\/p>\n<p>Q3 = df[&#39;data&#39;].quantile(0.75)<\/p>\n<h2><strong>\u8ba1\u7b97IQR<\/strong><\/h2>\n<p>IQR = Q3 - Q1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u786e\u5b9a\u4e0a\u4e0b\u754c\u9650<\/h3>\n<\/p>\n<p><p>\u6839\u636eIQR\uff0c\u901a\u5e38\u5c06\u5f02\u5e38\u503c\u5b9a\u4e49\u4e3a\u5728 <code>Q1 - 1.5 * IQR<\/code> \u4ee5\u4e0b\u6216 <code>Q3 + 1.5 * IQR<\/code> \u4ee5\u4e0a\u7684\u6570\u636e\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u786e\u5b9a\u4e0a\u4e0b\u754c\u9650<\/p>\n<p>lower_bound = Q1 - 1.5 * IQR<\/p>\n<p>upper_bound = Q3 + 1.5 * IQR<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7b5b\u9009\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u7b5b\u9009\u51fa\u4e0d\u5305\u542b\u5f02\u5e38\u503c\u7684\u6570\u636e\uff0c\u4ece\u800c\u53bb\u9664\u6570\u636e\u4e2d\u7684\u5f02\u5e38\u503c\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7b5b\u9009\u6570\u636e<\/p>\n<p>filtered_df = df[(df[&#39;data&#39;] &gt;= lower_bound) &amp; (df[&#39;data&#39;] &lt;= upper_bound)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u91cd\u65b0\u7ed8\u5236\u7bb1\u5f62\u56fe<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u8fc7\u6ee4\u540e\u7684\u6570\u636e\u91cd\u65b0\u7ed8\u5236\u7bb1\u5f62\u56fe\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u7bb1\u5f62\u56fe<\/strong><\/h2>\n<p>plt.boxplot(filtered_df[&#39;data&#39;])<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5b9e\u4f8b\u6f14\u793a<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u4e0a\u8ff0\u6b65\u9aa4\u6765\u53bb\u9664\u5f02\u5e38\u503c\u5e76\u91cd\u65b0\u7ed8\u5236\u7bb1\u5f62\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 5, 7, 9, 10, 15, 18, 19, 22, 24, 25, 30, 35, 40]<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;data&#39;: data})<\/p>\n<h2><strong>\u8ba1\u7b97Q1\u548cQ3<\/strong><\/h2>\n<p>Q1 = df[&#39;data&#39;].quantile(0.25)<\/p>\n<p>Q3 = df[&#39;data&#39;].quantile(0.75)<\/p>\n<h2><strong>\u8ba1\u7b97IQR<\/strong><\/h2>\n<p>IQR = Q3 - Q1<\/p>\n<h2><strong>\u786e\u5b9a\u4e0a\u4e0b\u754c\u9650<\/strong><\/h2>\n<p>lower_bound = Q1 - 1.5 * IQR<\/p>\n<p>upper_bound = Q3 + 1.5 * IQR<\/p>\n<h2><strong>\u7b5b\u9009\u6570\u636e<\/strong><\/h2>\n<p>filtered_df = df[(df[&#39;data&#39;] &gt;= lower_bound) &amp; (df[&#39;data&#39;] &lt;= upper_bound)]<\/p>\n<h2><strong>\u7ed8\u5236\u7bb1\u5f62\u56fe<\/strong><\/h2>\n<p>plt.boxplot(filtered_df[&#39;data&#39;])<\/p>\n<p>plt.title(&#39;Boxplot without Outliers&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5904\u7406\u591a\u5217\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u6570\u636e\u96c6\u5305\u542b\u591a\u5217\u6570\u636e\uff0c\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a\u7279\u5f81\u7684\u5f02\u5e38\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u591a\u5217\u6570\u636e<\/p>\n<p>data = {<\/p>\n<p>    &#39;feature1&#39;: [1, 2, 5, 7, 9, 10, 15, 18, 19, 22, 24, 25, 30, 35, 40],<\/p>\n<p>    &#39;feature2&#39;: [2, 3, 6, 8, 11, 12, 16, 19, 20, 23, 25, 26, 31, 36, 41]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5b9a\u4e49\u51fd\u6570\u6765\u7b5b\u9009\u6570\u636e<\/strong><\/h2>\n<p>def remove_outliers(df, column):<\/p>\n<p>    Q1 = df[column].quantile(0.25)<\/p>\n<p>    Q3 = df[column].quantile(0.75)<\/p>\n<p>    IQR = Q3 - Q1<\/p>\n<p>    lower_bound = Q1 - 1.5 * IQR<\/p>\n<p>    upper_bound = Q3 + 1.5 * IQR<\/p>\n<p>    return df[(df[column] &gt;= lower_bound) &amp; (df[column] &lt;= upper_bound)]<\/p>\n<h2><strong>\u7b5b\u9009\u6bcf\u4e00\u5217\u7684\u6570\u636e<\/strong><\/h2>\n<p>filtered_df = df.copy()<\/p>\n<p>for column in filtered_df.columns:<\/p>\n<p>    filtered_df = remove_outliers(filtered_df, column)<\/p>\n<h2><strong>\u7ed8\u5236\u7bb1\u5f62\u56fe<\/strong><\/h2>\n<p>plt.boxplot([filtered_df[&#39;feature1&#39;], filtered_df[&#39;feature2&#39;]], labels=[&#39;Feature 1&#39;, &#39;Feature 2&#39;])<\/p>\n<p>plt.title(&#39;Boxplot without Outliers for Multiple Features&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u8ba1\u7b97\u56db\u5206\u4f4d\u8ddd\uff08IQR\uff09\u3001\u786e\u5b9a\u4e0a\u4e0b\u754c\u9650\u3001\u7b5b\u9009\u6570\u636e\u5e76\u91cd\u65b0\u7ed8\u5236\u7bb1\u5f62\u56fe\uff0c\u4f60\u53ef\u4ee5\u6709\u6548\u53bb\u9664Python\u7bb1\u5f62\u56fe\u4e2d\u7684\u5f02\u5e38\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u4f7f\u5f97\u6570\u636e\u7684\u5206\u5e03\u66f4\u52a0\u6e05\u6670\uff0c\u4fbf\u4e8e\u8fdb\u4e00\u6b65\u7684\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u548c\u53bb\u9664\u7bb1\u5f62\u56fe\u4e2d\u7684\u5f02\u5e38\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Pandas\u548cMatplotlib\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u8bc6\u522b\u548c\u53bb\u9664\u7bb1\u5f62\u56fe\u4e2d\u7684\u5f02\u5e38\u503c\u3002\u9996\u5148\uff0c\u901a\u8fc7\u8ba1\u7b97\u56db\u5206\u4f4d\u6570(Q1\u548cQ3)\u53ca\u5176\u56db\u5206\u4f4d\u8ddd(IQR)\uff0c\u53ef\u4ee5\u786e\u5b9a\u5f02\u5e38\u503c\u7684\u754c\u9650\u3002\u5b9a\u4e49\u4e0a\u9650\u548c\u4e0b\u9650\u540e\uff0c\u5229\u7528\u6761\u4ef6\u7b5b\u9009\u5c06\u6570\u636e\u96c6\u4e2d\u8d85\u51fa\u8fd9\u4e9b\u754c\u9650\u7684\u503c\u6392\u9664\uff0c\u4ece\u800c\u5f97\u5230\u53bb\u9664\u5f02\u5e38\u503c\u540e\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u53bb\u9664\u5f02\u5e38\u503c\u540e\uff0c\u5982\u4f55\u91cd\u65b0\u7ed8\u5236\u7bb1\u5f62\u56fe\uff1f<\/strong><br \/>\u5728\u53bb\u9664\u5f02\u5e38\u503c\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u6216Seaborn\u7b49\u5e93\u91cd\u65b0\u7ed8\u5236\u7bb1\u5f62\u56fe\u3002\u53ea\u9700\u5c06\u6e05\u6d17\u540e\u7684\u6570\u636e\u4f20\u9012\u7ed9\u7ed8\u56fe\u51fd\u6570\uff0c\u4fbf\u53ef\u751f\u6210\u65b0\u7684\u7bb1\u5f62\u56fe\uff0c\u8fd9\u6837\u80fd\u591f\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u53ca\u5176\u7edf\u8ba1\u7279\u5f81\uff0c\u786e\u4fdd\u56fe\u5f62\u66f4\u5177\u4ee3\u8868\u6027\u3002<\/p>\n<p><strong>\u53bb\u9664\u5f02\u5e38\u503c\u4f1a\u5bf9\u6570\u636e\u5206\u6790\u7ed3\u679c\u4ea7\u751f\u54ea\u4e9b\u5f71\u54cd\uff1f<\/strong><br \/>\u53bb\u9664\u5f02\u5e38\u503c\u53ef\u80fd\u4f1a\u663e\u8457\u6539\u53d8\u6570\u636e\u7684\u5747\u503c\u3001\u6807\u51c6\u5dee\u53ca\u5176\u4ed6\u7edf\u8ba1\u6307\u6807\uff0c\u4ece\u800c\u5f71\u54cd\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u7ed3\u679c\u3002\u867d\u7136\u53bb\u9664\u5f02\u5e38\u503c\u6709\u52a9\u4e8e\u63d0\u9ad8\u6a21\u578b\u7684\u51c6\u786e\u6027\uff0c\u4f46\u4e5f\u9700\u8c28\u614e\u64cd\u4f5c\uff0c\u4ee5\u514d\u4e22\u5931\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002\u5728\u8fdb\u884c\u6570\u636e\u5206\u6790\u65f6\uff0c\u5efa\u8bae\u4fdd\u7559\u5f02\u5e38\u503c\u7684\u8bb0\u5f55\uff0c\u5e76\u5728\u62a5\u544a\u4e2d\u8bf4\u660e\u5904\u7406\u7684\u539f\u56e0\u548c\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u53bb\u9664Python\u7bb1\u5f62\u56fe\u4e2d\u7684\u5f02\u5e38\u503c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u8fc7\u6ee4\u548c\u6e05\u7406\u6280\u672f\u6765\u5904\u7406\u6570\u636e\u3002 \u7bb1\u5f62\u56fe\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u53ef\u89c6\u5316\u5de5\u5177 [&hellip;]","protected":false},"author":3,"featured_media":1079280,"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\/1079271"}],"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=1079271"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1079271\/revisions"}],"predecessor-version":[{"id":1079285,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1079271\/revisions\/1079285"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1079280"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1079271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1079271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1079271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}