python 用3σ法则检验x变量是否存在异常值,记录异常值的索引,然后剔除x,y值的索引对应的元素 代码
时间: 2024-06-03 19:10:00 浏览: 82
可以使用numpy库中的percentile函数进行3σ法则检验,代码如下:
import numpy as np
def remove_outliers(x, y):
# 计算x的平均值与标准差
mean = np.mean(x)
std = np.std(x)
# 计算下限与上限
lower_limit = mean - 3 * std
upper_limit = mean + 3 * std
# 记录异常值的索引
outliers_index = np.where(np.logical_or(x < lower_limit, x > upper_limit))[0]
# 剔除x,y值的索引对应的元素
x_clean = np.delete(x, outliers_index)
y_clean = np.delete(y, outliers_index)
return x_clean, y_clean, outliers_index
# 测试代码
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
y = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30])
x_clean, y_clean, outliers_index = remove_outliers(x, y)
print(x_clean)
print(y_clean)
print(outliers_index)
阅读全文