python-3.x - 去除异常值
问题描述
我尝试使用我创建的以下函数删除异常值,但使用它后我得到了奇怪的值。我删除异常值的方法是否正确?
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
解决方案
尝试如下
df1= df[(df['col']>=below_outliers)&(df['col']<=above_outliers))
推荐阅读
- python - 如何使用 groupby() 函数对数据进行切片?
- wordpress - Wordpress - 在桌面和移动设备上显示不同的图片
- python - 在 Python 中模拟 ArgumentParser 对象
- flutter - Flutter 分页数据表填充高度
- bash - 使用 sed in for 循环从其他文件编辑行
- c# - 无法设置 StringCollection' 字符串
- c++ - 删除链接列表中的重复项时出现指针错误
- javascript - 在 Mongoose 中填充数组的最后一个元素
- alexa - 如何将 Python 库导入 Alexa Skill
- delphi - 如何在 Delphi VCL 中制作半透明面板