python - 在python中用中位数替换异常值的麻烦
问题描述
for col in ('DiabetesPedigreeFunction','Insulin'):
Q1=df[col].quantile(0.25)
Q3=df[col].quantile(0.75)
IQR=Q3-Q1
upper_limit= Q3+1.5*IQR
lower_limit= Q1-1.5*IQR
db_median= float(df[col].median())
In_median= float(df[col].median())
df[col]=np.where(df[col]>upper_limit,db_median,df[col])
df[col]=np.where(df[col]>upper_limit,In_median,df[col])
代码运行良好,但是,使用箱线图检查...异常值仍然存在,也使用.describe()
...异常值仍然被注意到。
任何帮助请
解决方案
在此处输入图像描述 使用后发布的代码
>>> list_cols = ['DiabetesPedigreeFunction','Insulin']
>>> df[list_cols] = np.where(((df[list_cols] - df[list_cols].mean()) / df[list_cols].std()).abs() >= 3, df[list_cols].median(), df[list_cols])
结果与我之前的代码相似...查看图片
>>> df["DiabetesPedigreeFunction"].describe()
count 768.000000
mean 0.449800
std 0.279715
min 0.078000
25% 0.243750
50% 0.371750
75% 0.602000
max 1.461000
Name: DiabetesPedigreeFunction, dtype: float64
推荐阅读
- java - 从非 Servlet 类 (Java/Tomcat) 中的 WebApp 文件夹中获取文件
- laravel - 它在有两个值时返回“null”,但在 laravel 的 foreach 循环中有单个值时返回值
- azure - 除了使用 ARM 模板之外,还有其他方法可以部署 Azure 数据工厂吗?
- python - 一些库的 pip install requirements.txt 和 -t
- java - 如何调用在上层类中声明的变量
- python - 使用 pandas 时如何克服 Mac 中的操作系统错误
- sql - 如何为动态 SQL 查询创建结构?
- ruby-on-rails - 如何关闭引导模式?
- rest - 用于替换 PUT 的多个资源的 ReST API
- kubernetes - 无法启动 Kube-apiserver 服务