np去除极端值

keywords: python, numpy, extreme value

import numpy as np
data = np.random.rand(10)
print(data)
array([0.01969838, 0.91228414, 0.63533581, 0.52443755, 0.30228801,
       0.67190996, 0.89798288, 0.11410085, 0.51185741, 0.54711881])
mean = np.mean(data)
std = np.std(data)
data[data > mean + 2 * std]=mean
data[data < mean - 2 * std]=mean
mean = np.mean(data)
std = np.std(data)
data = [x  if ((x > mean - 2 * std) and (x < mean + 2 * std)) else mean for x in data]

你可能感兴趣的:(np去除极端值)