首页 > 代码库 > 2.9 清理异常值

2.9 清理异常值

import numpy as np
import matplotlib.pyplot as plt

def is_outlier(points,threshold=3.5):
    ‘‘‘Return a boolen array with True if points are out liers and Fa;se otherwise
    data point with a modified zscore fjdsklfadlkfa dfjalskerwwerwerwerwe
    rw
    :parameterwe
    :raisewer
    :keyworder
    we
    r
    we
    r
    etr
xiaxiede
    :returne
    2wwe
    :returnff
    fwr
    wr
    ‘‘‘
    if len(points.shape) == 1:
        points = points[:, None]

    median = np.median(points, axis=0)

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

    med_abs_deviation = np.median(diff)
    modified_z_score=0.6745*diff/med_abs_deviation
    return modified_z_score>threshold

x=np.random.random(100)

buckets=50

x=np.r_[x, -49, 95, 100, -100]
filtered=x[~is_outlier(x)]
plt.figure()
plt.subplot(211)
plt.hist(x,buckets)
plt.xlabel(Raw)

plt.subplot(212)
plt.hist(filtered,buckets)
plt.xlabel(Cleaned)
plt.show()

 

2.9 清理异常值