在Python中,可以使用以下方法来去除一组数据中的异常数据:
data = [1, 2, 3, 4, 5, 100, 6, 7, 8, 200]
threshold = 10
cleaned_data = [x for x in data if x <= threshold]
import numpy as np
data = [1, 2, 3, 4, 5, 100, 6, 7, 8, 200]
mean = np.mean(data)
std = np.std(data)
threshold = 2.0
cleaned_data = [x for x in data if abs(x - mean) <= threshold * std]
scipy.stats.zscore
函数进行标准化,并将标准化后的数据与给定的阈值进行比较,将超过阈值的数据视为异常数据。以下是示例代码:from scipy import stats
data = [1, 2, 3, 4, 5, 100, 6, 7, 8, 200]
threshold = 2.0
z_scores = stats.zscore(data)
cleaned_data = [x for x, z in zip(data, z_scores) if abs(z) <= threshold]
根据具体需求和数据特点,选择适合的方法来去除异常数据。