{"id":954135,"date":"2024-12-27T01:52:04","date_gmt":"2024-12-26T17:52:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/954135.html"},"modified":"2024-12-27T01:52:05","modified_gmt":"2024-12-26T17:52:05","slug":"python%e5%a6%82%e4%bd%95%e6%89%be%e5%87%ba%e5%bc%82%e5%b8%b8%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/954135.html","title":{"rendered":"python\u5982\u4f55\u627e\u51fa\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\/25094423\/5c9533a3-3691-493c-a132-63117c972278.webp\" alt=\"python\u5982\u4f55\u627e\u51fa\u5f02\u5e38\u503c\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d:<br \/><strong>\u5728Python\u4e2d\u627e\u51fa\u5f02\u5e38\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u7edf\u8ba1\u65b9\u6cd5\u3001\u53ef\u89c6\u5316\u6280\u672f\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\uff1a\u6807\u51c6\u5dee\u6cd5\u3001\u7bb1\u7ebf\u56fe\u6cd5\u3001Z\u5206\u6570\u6cd5\u3001IQR\u6cd5\u3001\u4ee5\u53ca\u57fa\u4e8e\u673a\u5668\u5b66\u4e60\u7684\u5b64\u7acb\u68ee\u6797\u6cd5\u3002<\/strong> \u4f7f\u7528\u6807\u51c6\u5dee\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u68c0\u6d4b\u6b63\u6001\u5206\u5e03\u6570\u636e\u4e2d\u7684\u5f02\u5e38\u503c\u3002\u901a\u8fc7\u8ba1\u7b97\u6570\u636e\u96c6\u4e2d\u6bcf\u4e2a\u6570\u636e\u70b9\u4e0e\u5e73\u5747\u503c\u7684\u504f\u5dee\uff0c\u901a\u5e38\u5c06\u504f\u5dee\u8d85\u8fc7\u4e24\u5230\u4e09\u4e2a\u6807\u51c6\u5dee\u7684\u70b9\u89c6\u4e3a\u5f02\u5e38\u503c\u3002\u6807\u51c6\u5dee\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u4f46\u5bf9\u4e8e\u975e\u6b63\u6001\u5206\u5e03\u7684\u6570\u636e\u53ef\u80fd\u4e0d\u592a\u9002\u7528\u3002  <\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6807\u51c6\u5dee\u6cd5<\/p>\n<\/p>\n<p><p>\u6807\u51c6\u5dee\u6cd5\u662f\u57fa\u4e8e\u6570\u636e\u7684\u5747\u503c\u548c\u6807\u51c6\u5dee\u6765\u8bc6\u522b\u5f02\u5e38\u503c\u7684\u5e38\u7528\u7edf\u8ba1\u65b9\u6cd5\u3002\u5176\u57fa\u672c\u601d\u60f3\u662f\uff0c\u5982\u679c\u6570\u636e\u70b9\u4e0e\u5747\u503c\u7684\u504f\u5dee\u5927\u4e8e\u4e00\u5b9a\u500d\u6570\u7684\u6807\u51c6\u5dee\uff0c\u5219\u8ba4\u4e3a\u8be5\u6570\u636e\u70b9\u4e3a\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u6807\u51c6\u5dee\u6cd5\u7684\u8ba1\u7b97\u8fc7\u7a0b\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u8ba1\u7b97\u6570\u636e\u7684\u5747\u503c\uff08mean\uff09\u548c\u6807\u51c6\u5dee\uff08standard deviation\uff09\u3002\u7136\u540e\uff0c\u6839\u636e\u8bbe\u5b9a\u7684\u9608\u503c\uff08\u901a\u5e38\u4e3a2\u62163\u4e2a\u6807\u51c6\u5dee\uff09\uff0c\u8bc6\u522b\u51fa\u504f\u79bb\u5747\u503c\u8d85\u8fc7\u8be5\u9608\u503c\u7684\u70b9\u4f5c\u4e3a\u5f02\u5e38\u503c\u3002<\/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 = [10, 12, 12, 13, 12, 11, 10, 14, 100] # \u5047\u8bbe100\u662f\u4e00\u4e2a\u5f02\u5e38\u503c<\/p>\n<h2><strong>\u8ba1\u7b97\u5747\u503c\u548c\u6807\u51c6\u5dee<\/strong><\/h2>\n<p>mean = np.mean(data)<\/p>\n<p>std_dev = np.std(data)<\/p>\n<h2><strong>\u8bbe\u5b9a\u9608\u503c\u4e3a2\u4e2a\u6807\u51c6\u5dee<\/strong><\/h2>\n<p>threshold = 2<\/p>\n<h2><strong>\u627e\u51fa\u5f02\u5e38\u503c<\/strong><\/h2>\n<p>outliers = [x for x in data if abs(x - mean) &gt; threshold * std_dev]<\/p>\n<p>print(&quot;\u5f02\u5e38\u503c:&quot;, outliers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6570\u636e100\u4e0e\u5176\u4f59\u6570\u636e\u7684\u5747\u503c\u76f8\u5dee\u5f88\u5927\uff0c\u56e0\u6b64\u88ab\u8bc6\u522b\u4e3a\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u6807\u51c6\u5dee\u6cd5\u7684\u4f18\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u6807\u51c6\u5dee\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u6b63\u6001\u5206\u5e03\u7684\u6570\u636e\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u975e\u6b63\u6001\u5206\u5e03\u7684\u6570\u636e\uff0c\u6807\u51c6\u5dee\u6cd5\u53ef\u80fd\u4e0d\u591f\u51c6\u786e\uff0c\u56e0\u4e3a\u5b83\u5047\u5b9a\u6570\u636e\u5177\u6709\u5bf9\u79f0\u7684\u5206\u5e03\u3002\u6b64\u5916\uff0c\u5bf9\u4e8e\u542b\u6709\u591a\u4e2a\u5f02\u5e38\u503c\u7684\u6570\u636e\u96c6\uff0c\u6807\u51c6\u5dee\u5bb9\u6613\u53d7\u5230\u5f02\u5e38\u503c\u672c\u8eab\u7684\u5f71\u54cd\uff0c\u4ece\u800c\u5bfc\u81f4\u8bc6\u522b\u4e0d\u51c6\u786e\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u7bb1\u7ebf\u56fe\u6cd5<\/p>\n<\/p>\n<p><p>\u7bb1\u7ebf\u56fe\u6cd5\u901a\u8fc7\u7ed8\u5236\u7bb1\u7ebf\u56fe\u6765\u76f4\u89c2\u5730\u8bc6\u522b\u5f02\u5e38\u503c\u3002\u7bb1\u7ebf\u56fe\u6cd5\u57fa\u4e8e\u56db\u5206\u4f4d\u6570\uff08quartiles\uff09\u6765\u5b9a\u4e49\u5f02\u5e38\u503c\uff0c\u901a\u5e38\u8ba4\u4e3a\u5728\u4e0a\u56db\u5206\u4f4d\u6570\uff08Q3\uff09\u548c\u4e0b\u56db\u5206\u4f4d\u6570\uff08Q1\uff09\u4e4b\u5916\u76841.5\u500d\u56db\u5206\u4f4d\u8ddd\uff08IQR\uff09\u8303\u56f4\u4e4b\u5916\u7684\u6570\u636e\u70b9\u4e3a\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u7bb1\u7ebf\u56fe\u6cd5\u7684\u8ba1\u7b97\u8fc7\u7a0b\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u8ba1\u7b97\u6570\u636e\u7684\u4e0a\u56db\u5206\u4f4d\u6570\uff08Q3\uff09\u3001\u4e0b\u56db\u5206\u4f4d\u6570\uff08Q1\uff09\u548c\u56db\u5206\u4f4d\u8ddd\uff08IQR\uff09\u3002\u7136\u540e\uff0c\u4f7f\u7528IQR\u5b9a\u4e49\u6570\u636e\u7684\u4e0a\u4e0b\u8fb9\u754c\uff0c\u8d85\u51fa\u8be5\u8fb9\u754c\u7684\u6570\u636e\u70b9\u88ab\u89c6\u4e3a\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [10, 12, 12, 13, 12, 11, 10, 14, 100] # \u5047\u8bbe100\u662f\u4e00\u4e2a\u5f02\u5e38\u503c<\/p>\n<h2><strong>\u7ed8\u5236\u7bb1\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.boxplot(data)<\/p>\n<p>plt.title(&quot;Boxplot for Outlier Detection&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7bb1\u7ebf\u56fe\u53ef\u4ee5\u76f4\u89c2\u5730\u89c2\u5bdf\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u548c\u5f02\u5e38\u503c\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u7bb1\u7ebf\u56fe\u6cd5\u7684\u4f18\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u7bb1\u7ebf\u56fe\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u6570\u636e\u5206\u5e03\u60c5\u51b5\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u6570\u636e\u5206\u5e03\u4e0d\u5747\u5300\u6216\u5177\u6709\u957f\u5c3e\u5206\u5e03\u7684\u6570\u636e\u96c6\uff0c\u7bb1\u7ebf\u56fe\u6cd5\u53ef\u80fd\u4f1a\u8bef\u5224\u67d0\u4e9b\u6570\u636e\u70b9\u4e3a\u5f02\u5e38\u503c\u3002\u6b64\u5916\uff0c\u7bb1\u7ebf\u56fe\u6cd5\u4e0d\u9002\u7528\u4e8e\u591a\u7ef4\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001Z\u5206\u6570\u6cd5<\/p>\n<\/p>\n<p><p>Z\u5206\u6570\u6cd5\u662f\u57fa\u4e8e\u6570\u636e\u6807\u51c6\u5316\u7684\u5f02\u5e38\u503c\u68c0\u6d4b\u65b9\u6cd5\uff0c\u5176\u57fa\u672c\u601d\u60f3\u662f\u901a\u8fc7\u8ba1\u7b97\u6bcf\u4e2a\u6570\u636e\u70b9\u7684Z\u5206\u6570\uff08\u4e0e\u5747\u503c\u7684\u6807\u51c6\u5316\u504f\u5dee\uff09\u6765\u8bc6\u522b\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>1. Z\u5206\u6570\u6cd5\u7684\u8ba1\u7b97\u8fc7\u7a0b\uff1a<\/strong><\/p>\n<\/p>\n<p><p>Z\u5206\u6570\u662f\u901a\u8fc7\u5c06\u6570\u636e\u70b9\u51cf\u53bb\u5747\u503c\uff0c\u5e76\u9664\u4ee5\u6807\u51c6\u5dee\u5f97\u5230\u7684\u3002\u901a\u5e38\uff0cZ\u5206\u6570\u7edd\u5bf9\u503c\u5927\u4e8e2\u62163\u7684\u70b9\u88ab\u8ba4\u4e3a\u662f\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97Z\u5206\u6570<\/p>\n<p>z_scores = [(x - mean) \/ std_dev for x in data]<\/p>\n<h2><strong>\u627e\u51fa\u5f02\u5e38\u503c<\/strong><\/h2>\n<p>outliers = [data[i] for i, z in enumerate(z_scores) if abs(z) &gt; 2]<\/p>\n<p>print(&quot;\u5f02\u5e38\u503c:&quot;, outliers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. Z\u5206\u6570\u6cd5\u7684\u4f18\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<p><p>Z\u5206\u6570\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u9002\u7528\u4e8e\u6b63\u6001\u5206\u5e03\u7684\u6570\u636e\u96c6\uff0c\u4e14\u80fd\u591f\u63d0\u4f9b\u6807\u51c6\u5316\u7684\u5f02\u5e38\u503c\u5ea6\u91cf\u3002\u7136\u800c\uff0cZ\u5206\u6570\u6cd5\u540c\u6837\u4e0d\u9002\u7528\u4e8e\u975e\u6b63\u6001\u5206\u5e03\u7684\u6570\u636e\uff0c\u5e76\u4e14\u5bf9\u5f02\u5e38\u503c\u7684\u6570\u91cf\u548c\u4f4d\u7f6e\u8f83\u4e3a\u654f\u611f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001IQR\u6cd5<\/p>\n<\/p>\n<p><p>IQR\u6cd5\u662f\u57fa\u4e8e\u56db\u5206\u4f4d\u6570\u95f4\u8ddd\uff08IQR\uff09\u6765\u5b9a\u4e49\u5f02\u5e38\u503c\u7684\u7edf\u8ba1\u65b9\u6cd5\uff0c\u4e0e\u7bb1\u7ebf\u56fe\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u4e0d\u4f9d\u8d56\u4e8e\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><p><strong>1. IQR\u6cd5\u7684\u8ba1\u7b97\u8fc7\u7a0b\uff1a<\/strong><\/p>\n<\/p>\n<p><p>IQR\u6cd5\u901a\u8fc7\u8ba1\u7b97\u4e0a\u56db\u5206\u4f4d\u6570\uff08Q3\uff09\u3001\u4e0b\u56db\u5206\u4f4d\u6570\uff08Q1\uff09\u548c\u56db\u5206\u4f4d\u8ddd\uff08IQR\uff09\uff0c\u7136\u540e\u4f7f\u7528IQR\u5b9a\u4e49\u6570\u636e\u7684\u4e0a\u4e0b\u8fb9\u754c\uff0c\u8d85\u51fa\u8be5\u8fb9\u754c\u7684\u6570\u636e\u70b9\u88ab\u89c6\u4e3a\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u56db\u5206\u4f4d\u6570<\/p>\n<p>Q1 = np.percentile(data, 25)<\/p>\n<p>Q3 = np.percentile(data, 75)<\/p>\n<p>IQR = Q3 - Q1<\/p>\n<h2><strong>\u5b9a\u4e49\u4e0a\u4e0b\u8fb9\u754c<\/strong><\/h2>\n<p>lower_bound = Q1 - 1.5 * IQR<\/p>\n<p>upper_bound = Q3 + 1.5 * IQR<\/p>\n<h2><strong>\u627e\u51fa\u5f02\u5e38\u503c<\/strong><\/h2>\n<p>outliers = [x for x in data if x &lt; lower_bound or x &gt; upper_bound]<\/p>\n<p>print(&quot;\u5f02\u5e38\u503c:&quot;, outliers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. IQR\u6cd5\u7684\u4f18\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<p><p>IQR\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u4e0d\u4f9d\u8d56\u4e8e\u6570\u636e\u7684\u5206\u5e03\u5f62\u6001\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u5206\u5e03\u3002\u7136\u800c\uff0cIQR\u6cd5\u5bf9\u6570\u636e\u7684\u957f\u5c3e\u5206\u5e03\u8f83\u4e3a\u654f\u611f\uff0c\u53ef\u80fd\u4f1a\u5c06\u67d0\u4e9b\u6781\u7aef\u503c\u8bef\u5224\u4e3a\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5b64\u7acb\u68ee\u6797\u6cd5<\/p>\n<\/p>\n<p><p>\u5b64\u7acb\u68ee\u6797\u6cd5\u662f\u4e00\u79cd\u57fa\u4e8e\u673a\u5668\u5b66\u4e60\u7684\u5f02\u5e38\u503c\u68c0\u6d4b\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u591a\u7ef4\u6570\u636e\u96c6\u3002\u5176\u57fa\u672c\u601d\u60f3\u662f\u901a\u8fc7\u6784\u5efa\u968f\u673a\u68ee\u6797\uff0c\u5c06\u6570\u636e\u70b9\u5b64\u7acb\u5316\u6765\u8bc6\u522b\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u5b64\u7acb\u68ee\u6797\u6cd5\u7684\u8ba1\u7b97\u8fc7\u7a0b\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5b64\u7acb\u68ee\u6797\u901a\u8fc7\u6784\u5efa\u591a\u68f5\u968f\u673a\u6811\uff0c\u6bcf\u68f5\u6811\u901a\u8fc7\u968f\u673a\u9009\u62e9\u7279\u5f81\u548c\u9608\u503c\u6765\u5212\u5206\u6570\u636e\u3002\u6570\u636e\u70b9\u5728\u6811\u4e2d\u88ab\u5b64\u7acb\u7684\u7a0b\u5ea6\u8d8a\u9ad8\uff0c\u5176\u88ab\u8ba4\u4e3a\u662f\u5f02\u5e38\u503c\u7684\u53ef\u80fd\u6027\u8d8a\u5927\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.ensemble import IsolationForest<\/p>\n<h2><strong>\u521b\u5efa\u5b64\u7acb\u68ee\u6797\u6a21\u578b<\/strong><\/h2>\n<p>model = IsolationForest(contamination=0.1)<\/p>\n<h2><strong>\u62df\u5408\u6570\u636e<\/strong><\/h2>\n<p>model.fit(np.array(data).reshape(-1, 1))<\/p>\n<h2><strong>\u9884\u6d4b\u5f02\u5e38\u503c<\/strong><\/h2>\n<p>outliers = model.predict(np.array(data).reshape(-1, 1))<\/p>\n<p>print(&quot;\u5f02\u5e38\u503c:&quot;, [data[i] for i in range(len(data)) if outliers[i] == -1])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u5b64\u7acb\u68ee\u6797\u6cd5\u7684\u4f18\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5b64\u7acb\u68ee\u6797\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u9002\u7528\u4e8e\u591a\u7ef4\u6570\u636e\u96c6\uff0c\u80fd\u591f\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u5206\u5e03\u3002\u7136\u800c\uff0c\u8be5\u65b9\u6cd5\u9700\u8981\u8bbe\u5b9a\u53c2\u6570\uff08\u5982\u6c61\u67d3\u7387\uff09\uff0c\u5e76\u4e14\u5728\u5904\u7406\u5c0f\u6570\u636e\u96c6\u65f6\u53ef\u80fd\u8868\u73b0\u4e0d\u4f73\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5f02\u5e38\u503c\u68c0\u6d4b\uff0c\u5305\u62ec\u7edf\u8ba1\u65b9\u6cd5\uff08\u6807\u51c6\u5dee\u6cd5\u3001\u7bb1\u7ebf\u56fe\u6cd5\u3001Z\u5206\u6570\u6cd5\u3001IQR\u6cd5\uff09\u548c\u673a\u5668\u5b66\u4e60\u65b9\u6cd5\uff08\u5b64\u7acb\u68ee\u6797\u6cd5\uff09\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u9700\u8981\u8003\u8651\u6570\u636e\u7684\u5206\u5e03\u5f62\u6001\u3001\u7ef4\u5ea6\u4ee5\u53ca\u5e94\u7528\u573a\u666f\u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u5f02\u5e38\u503c\u68c0\u6d4b\uff0c\u4ee5\u83b7\u5f97\u66f4\u4e3a\u51c6\u786e\u548c\u5168\u9762\u7684\u7ed3\u679c\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u7406\u89e3\u6570\u636e\u7684\u7279\u6027\u548c\u5206\u5e03\u662f\u8bc6\u522b\u5f02\u5e38\u503c\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bc6\u522b\u6570\u636e\u4e2d\u7684\u5f02\u5e38\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8bc6\u522b\u5f02\u5e38\u503c\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u7edf\u8ba1\u65b9\u6cd5\u548c\u673a\u5668\u5b66\u4e60\u6280\u672f\u3002\u7edf\u8ba1\u65b9\u6cd5\u5982Z-score\u548cIQR\uff08\u56db\u5206\u4f4d\u6570\u95f4\u8ddd\uff09\u53ef\u4ee5\u5e2e\u52a9\u786e\u5b9a\u6570\u636e\u70b9\u662f\u5426\u504f\u79bb\u6b63\u5e38\u8303\u56f4\u3002\u6b64\u5916\uff0c\u4f7f\u7528Scikit-learn\u5e93\u4e2d\u7684Isolation Forest\u548cLocal Outlier Factor\u7b49\u6a21\u578b\uff0c\u4e5f\u80fd\u6709\u6548\u5730\u8bc6\u522b\u5f02\u5e38\u503c\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5bf9\u6570\u636e\u96c6\u8fdb\u884c\u5206\u6790\u5e76\u627e\u51fa\u663e\u8457\u4e0d\u540c\u7684\u70b9\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Z-score\u6cd5\u65f6\uff0c\u5982\u4f55\u786e\u5b9a\u5f02\u5e38\u503c\u7684\u9608\u503c\uff1f<\/strong><br \/>Z-score\u6cd5\u901a\u5e38\u4f7f\u7528\u6807\u51c6\u5dee\u6765\u5224\u65ad\u6570\u636e\u70b9\u7684\u504f\u79bb\u7a0b\u5ea6\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\uff0cZ-score\u8d85\u8fc73\u6216\u4f4e\u4e8e-3\u7684\u503c\u88ab\u89c6\u4e3a\u5f02\u5e38\u503c\u3002\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u8c03\u6574\u8fd9\u4e2a\u9608\u503c\u3002\u4f8b\u5982\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cZ-score\u5927\u4e8e2\u6216\u5c0f\u4e8e-2\u4e5f\u53ef\u4ee5\u88ab\u89c6\u4e3a\u5f02\u5e38\u503c\uff0c\u8fd9\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u7279\u6027\u548c\u5206\u6790\u76ee\u7684\u3002<\/p>\n<p><strong>\u4f7f\u7528Pandas\u5e93\u65f6\uff0c\u5982\u4f55\u5feb\u901f\u7b5b\u9009\u51fa\u5f02\u5e38\u503c\uff1f<\/strong><br \/>\u5728Pandas\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528<code>DataFrame<\/code>\u7684\u529f\u80fd\u5feb\u901f\u7b5b\u9009\u5f02\u5e38\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u6bcf\u5217\u7684IQR\uff0c\u4f7f\u7528<code>quantile<\/code>\u65b9\u6cd5\u786e\u5b9a\u4e0a\u4e0b\u9650\uff0c\u7136\u540e\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15\u6765\u9009\u53d6\u6570\u636e\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u5047\u8bbedf\u662f\u4f60\u7684DataFrame\nQ1 = df.quantile(0.25)\nQ3 = df.quantile(0.75)\nIQR = Q3 - Q1\nlower_bound = Q1 - 1.5 * IQR\nupper_bound = Q3 + 1.5 * IQR\n\noutliers = df[(df &lt; lower_bound) | (df &gt; upper_bound)]\n<\/code><\/pre>\n<p>\u6b64\u65b9\u6cd5\u80fd\u6709\u6548\u8bc6\u522b\u5e76\u63d0\u53d6\u6570\u636e\u4e2d\u7684\u5f02\u5e38\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d:\u5728Python\u4e2d\u627e\u51fa\u5f02\u5e38\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u7edf\u8ba1\u65b9\u6cd5\u3001\u53ef\u89c6\u5316\u6280\u672f\u548c\u673a\u5668\u5b66\u4e60\u6a21\u578b\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\uff1a\u6807 [&hellip;]","protected":false},"author":3,"featured_media":954140,"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\/954135"}],"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=954135"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/954135\/revisions"}],"predecessor-version":[{"id":954141,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/954135\/revisions\/954141"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/954140"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=954135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=954135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=954135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}