首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除异常值

删除异常值
EN

Stack Overflow用户
提问于 2019-08-03 00:33:47
回答 1查看 96关注 0票数 0

我尝试使用我创建的以下函数删除异常值,但在使用它之后,我得到了奇怪的值。我去除异常值的方法正确吗?

代码语言:javascript
复制
def remove_outliers(df,numeric_features):
'''
remove_outliers is a function which removes outliers by removing any
point above the mean by 2 standard deviations or below the mean by 2 standard deviations
df is the dataframe which the outliers are to be removed from
numeric_features are the numeric columns which might contain outliers
return new data frame
'''

#Iterate all the columns in numeric features
for col in numeric_features:

    mean = df[col].mean() #Find mean of column
    std = np.std(df[col],axis = 0)#find standard deviation of column

    #Variables used to find outliers
    above_outliers = mean + 2*std
    below_outliers = mean - 2*std

    outlier_indexes = df[col].loc[lambda x: (x>=above_outliers)|(x<=below_outliers)]

    #drop outliers from the dataframe column
    df= df.drop(outlier_indexes.index)
return df
EN

回答 1

Stack Overflow用户

发布于 2019-08-03 00:36:46

尝试如下所示

代码语言:javascript
复制
  df1=  df[(df['col']>=below_outliers)&(df['col']<=above_outliers))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57330448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档