【发布时间】:2023-02-18 04:45:01
【问题描述】:
我的 df 有一个价格栏,看起来像
0 2125.000000
1 14469.483703
2 14101.832820
3 20287.619019
4 14469.483703
...
12561 2490.000000
12562 2931.283333
12563 1779.661017
12566 2200.000000
12567 2966.666667
我想删除 price_m2 列中带有异常值的所有 df 行。我尝试了两种方法:
第一:
df_w_o = df[np.abs(df.price_m2-df.price_m2.mean())<=(1*df.price_m2.std())]
第二:
df['z_score'] = (df['price_m2'] - df['price_m2'].mean()) / df['price_m2'].std()
df_w_o = df[(df['z_score'] < 1) & (df['z_score'] > -1)]
当我检查我的最小最大值后我得到:
print(df_w_o.price_m2.min())
print(df_w_o.price_m2.max())
0.0
25438.022812290565
前我得到的移除:
print(df.price_m2.min())
print(df.price_m2.max())
0.0
589933.4267822268
这感觉不对,我如何才能获得本应与房地产有关的数据的如此大的价格范围。在此示例中,0 是极低值,在移除异常值后仍然存在。
【问题讨论】:
-
请记住,outilers 在正态分布中位于
> mean+2*std和< mean-2*std,两条尾巴。 -
你是说这个
df_w_o = df[(df['z_score'] < 1) & (df['z_score'] > -1)]应该是df_w_o = df[(df['z_score'] < std) & (df['z_score'] > -std)]?我使用 1std 的理由是:因为它是一个狭窄地理区域的数据价格集,我假设 1 倍 std 应该更准确