问题描述
我正在使用 z score 方法去除异常值..但是当我设置阈值并打印低于该阈值的数据时,我得到了空数组。 我试过下面的代码。
from scipy import stats
z=np.abs(stats.zscore(df.High))
print(z)
threshold=7
print(np.where(z>7))
(array([],dtype=int64),)
解决方法
Z-Score 本质上是我的实际距离有多少标准差 取平均值!
有关此 here 的更多信息。您在这里所做的是将您的人口(列包含值)转换为 Z 分数并使用实际值作为阈值,但阈值也应该在 Z 空间中!该值由问题的性质决定。
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from scipy import stats
mu,sigma = 5,2
array = np.random.normal(mu,sigma,200)
df = pd.DataFrame(array,columns=["High"])
z=np.abs(stats.zscore(df.High))
print("Actual Value Above 7: ",df[df.High>7])
threshold=7
print("Z Score Value Above 7: ",np.where(z>threshold))
######## mapping 7 to Z space
z_threshold = (threshold - df.High.mean())/df.High.std(ddof=0)
print("Z Score Value Above zscore(7): ",np.where(z>z_threshold))