MAD 定義爲,一元序列 同其中位數誤差的絕對值的中位數(deviation,誤差自己有正有負);python
假定數據服從正態分佈,咱們讓異常點(outliers)落在兩側的 50% 的面積裏,讓正常值落在中間的 50% 的區域裏:google
其中 ,又由 ,可 ⇒ ,查表可知, =0.6749。atom
from scipy.stats import norm
def mad_based_outlier(points, thresh=3.5):
if type(points) is list:
points = np.asarray(points)
if len(points.shape) == 1:
points = points[:, None]
med = np.median(points, axis=0)
abs_dev = np.absolute(points - med)
med_abs_dev = np.median(abs_dev)
mod_z_score = norm.ppf(0.75) * abs_dev / med_abs_dev
return mod_z_score > thresh
MAD 的方法相對於分位數方法的一大優點即在於 MAD 方法對樣本大小是不敏感也便是穩定的魯棒的一種評價指標。spa
def percentile_based_outlier(data, threshold=95):
diff = (100 - threshold) / 2.0
minval, maxval = np.percentile(data, [diff, 100 - diff])
return (data < minval) | (data > maxval)
Pythonic way of detecting outliers in one dimensional observation datacode