基于中值 - 绝对偏差(MAD)的离群值检测

时间:2014-04-02 07:13:25

标签: python numpy scipy

我想使用@Joe Kington的答案来应用基于中值 - 绝对偏差(MAD)的异常值检测,如下所示:

Pythonic way of detecting outliers in one dimensional observation data

然而,我的代码出了什么问题,我无法弄清楚如何将异常值指定为MY DATA的纳米值:

import numpy as np
data = np.array([55,32,4,5,6,7,8,9,11,0,2,1,3,4,5,6,7,8,25,25,25,25,10,11,12,25,26,27,28],dtype=float)
median = np.median(data, axis=0)
diff = np.sum((data - median)**2, axis=-1)
diff = np.sqrt(diff)
med_abs_deviation = np.median(diff)
modified_z_score = 0.6745 * diff / med_abs_deviation
data_without_outliers = data[modified_z_score < 3.5]
?????
print data_without_outliers

2 个答案:

答案 0 :(得分:0)

使用时有什么问题:

data[modified_z_score > 3.5] = np.nan

请注意,这仅在data是浮点数组时才有效(如果您正在计算MAD,它应该是这样)。

答案 1 :(得分:0)

问题可能是线路:

diff = np.sum((data - median)**2, axis=-1)

应用np.sum()会将结果折叠为标量。 删除前一总和,您的代码将起作用