matlab One is generated by default in 2 * k + 1 Size window , The insufficient part is filled with the current data .

Estimation of standard deviation using normal distribution （ The value is 1.4826） * Absolute Median deviation , Get an estimated standard deviation .

matlab Zhongmo thinks 3 The outliers beyond the estimated standard deviation are determined .

The code is as follows ：

def hampel(X): length = X.shape - 1 k = 3 nsigma = 3 iLo = np.array([i - k
for i in range(0, length + 1)]) iHi = np.array([i + k for i in range(0, length
+ 1)]) iLo[iLo < 0] = 0 iHi[iHi > length] = length xmad = [] xmedian = [] for i
in range(length + 1): w = X[iLo[i]:iHi[i] + 1] medj = np.median(w) mad =
np.median(np.abs(w - medj)) xmad.append(mad) xmedian.append(medj) xmad =
np.array(xmad) xmedian = np.array(xmedian) scale = 1.4826 # zoom xsigma = scale *
xmad xi = ~(np.abs(X - xmedian) <= nsigma * xsigma) # Find out the outliers （ That is, more than nsigma Standard deviation ） #
Replace outliers with median values xf = X.copy() xf[xi] = xmedian[xi] return xf X = np.array([1, 2, 3,
4, 100, 4, 3, 2, 1]) res = hampel(X) plt.plot(X) plt.plot(res, '--') plt.show()
The results are as follows ：

Outliers have been corrected , You can adjust the window size to your own appropriate value .

Technology
Daily Recommendation