你可以做的是计算数据的前后梯度,假设步长为1。离群值是那些条件适用的元素:向后和向前的坡度没有相同的标志:趋势变化
后向梯度的绝对值大于左邻域绝对值的1%
我对你的解释是,这两种说法都必须是真的。在
让f成为数据的一维numpy数组。在f=np.array([
0.0937,
0.0934,
0.0933,
0.0931,
0.0933,
0.0936,
0.1091, #< == outlier,
0.0938,
0.0945,
0.0949,
0.0956,
0.1082,
0.1065, #< == outlier since -,
0.1123,
0.1198
])
bg = 0.0*f # backward gradient, we want them to have the same size as f
fg = 0.0*f # forward gradient, we want them to have the same size as f
bg[1:] = f[1:]-f[:-1]
fg[:-1] = f[1:]-f[:-1]
outliers = (bg*fg<0) * np.hstack((False, np.where(np.abs(bg[1:])>0.01*np.abs(f[:-1]),True,False) ))
# You don't want to remove an element and the next
outliers[1:] = outliers[1:]*np.where( outliers[:-1], False, True )
print 'Outliers = ', f[outliers]
print 'Good = ', f[np.where( outliers, False, True)]
我用你的数据做了这个例子,只要用任何东西替换f。在