繁体   English   中英

使用 Python 根据数据集中的多个条件识别和删除异常值

[英]Identifying and removing outliers based on more than one condition in a dataset using Python

我正在为回归建模准备一个数据集。 我想在这样做之前删除所有异常值。 该数据集有 7 个本质上是连续的变量。 其中五个变量可以普遍解决。 但是,首先需要在男性和女性参与者之间划分两个变量,这两个变量是身高和体重。 显然这两个测量值在男性和女性之间会有所不同,因此为了获取异常值,我需要区分男性和女性的数据,然后评估/删除每个人的身高和体重的异常值,然后将这些数据与我的数据合并已经准备好了。 有没有一种简单的方法可以做到这一点? 到目前为止,我一直在相邻的 5 个变量上使用四分位数范围,这些变量不需要除以男性和女性,对每个变量使用这个代码......

Q1 = df["Variable"].quantile(0.25)
Q3 = df["Variable"].quantile(0.75)

IQR = Q3-Q1
Lower_Fence = Q1 - (1.5*IQR)
Upper_Fence = Q3 + (1.5*IQR)

print(Lower_Fence)
 print(Upper_Fence)

df[((df["Variable"] < Lower_Fence) | (df["Variable"]  > Upper_Fence))] # Detection of outliers
df[~((df["Variable"] < Lower_Fence) | (df["Variable"]  > Upper_Fence))]` # Removal of outliers

我对 python 比较陌生。

我正在使用的数据的图片

您可以为您的“异常值”逻辑定义 function,然后将其重复应用于所有列,无论是否使用 groupby:

def is_outlier(s, quantiles=[.25, .75], thresholds=[-.5, .5]):
    # change the thresholds to [-1.5, 1.5] to reflect IQR as per your question
    a, b = s.quantile(quantiles)
    iqr = b - a
    lo, hi = np.array(thresholds) * iqr + [a, b]
    return (s < lo) | (s > hi)

简单测试:

n = 20
np.random.seed(0)
df = pd.DataFrame(dict(
    status=np.random.choice(['dead', 'alive'], n),
    gender=np.random.choice(['M', 'F'], n),
    weight=np.random.normal(150, 40, n),
    diastolic=np.random.normal(80, 10, n),
    cholesterol=np.random.normal(200, 20, n),
))

示例用法:

mask = is_outlier(df['diastolic'])  # overall outliers
# or
mask = df.groupby('gender')['weight'].apply(is_outlier)  # per gender group

过滤数据的用法:

mask = False

# overall outliers
for k in ['diastolic', 'cholesterol']:  # etc
    mask |= is_outlier(df[k])

# per-gender outliers
gb = df.groupby('gender')
for k in ['weight']:  # and any other columns needed for per-gender
    mask |= gb[k].apply(is_outlier)

# finally, select the non-outliers
df_filtered = df.loc[~mask]

顺便说一句,请注意每个性别的异常值与整体有何不同,例如“体重”:

df.groupby('gender')['weight'].apply(is_outlier) == is_outlier(df['weight'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM