python - 找不到我的数据集的异常值(更具体地说是 IQR)
问题描述
试图在 python 中使用 pandas 查找 excel 表的异常值。我能够找到第一个和第三个四分位数,但不能在不返回的情况下从另一个四分位数中减去一个NaN
。
这是基本代码:
absent = pd.read_excel('Absenteeism_at_work.xls')
print("\nOUTLIERS:")
# q1 = (absent.loc[:741, ['Distance from Residence to Work']].quantile([0.25]))
# q3 = (absent.loc[:741, ['Distance from Residence to Work']].quantile([0.75]))
#print(absent.loc[:741, 'Distance from Residence to Work'].quantile([0.25])) #quartile
#print(q1)
# q1, q3 = absent.loc[:741, ['Distance from Residence to Work', 'Transportation expense', 'Month of absence',
# 'Social smoker', 'Social drinker', 'Education']].quantile([0.25 - 0.75])
print(absent.loc[:741, ['Distance from Residence to Work', 'Transportation expense', 'Month of absence',
'Social smoker', 'Social drinker', 'Education']].quantile([0.75])
- absent.loc[:741, ['Distance from Residence to Work', 'Transportation expense', 'Month of absence',
'Social smoker', 'Social drinker', 'Education']].quantile([0.25]))
输出:
OUTLIERS:
Distance from Residence to Work Transportation expense \
0.25 NaN NaN
0.75 NaN NaN
Month of absence Social smoker Social drinker Education
0.25 NaN NaN NaN NaN
0.75 NaN NaN NaN NaN
解决方案
您的代码只是简单的四分位数范围计算。如果它可以为你工作,那很好。如果您需要真正的异常值检测,这比基于四分位数的模式更复杂,尤其是多变量,您可以求助于 python 包,如 sklearn 或 pyod。
使用分位数函数,您需要清理原始数据以确保它只是数字。特别是,您将 excel 文件导入为数据源。
通过检查数据
tmp_df = 缺席.iloc[:741]
cols = ['从住所到工作的距离','交通费用','缺勤月份','社交吸烟者','社交饮酒者','教育']
打印(tmp_df[col].quantile(0.25,0.75))
打印(tmp_df[col].describe(include='all'))
祝你好运。
怀俄明
推荐阅读
- maven - Intellij 识别 TestNG 注释 @Test 但不识别(优先级 = #)
- android - 底部导航视图图标颜色不准确
- java - 使用 PDFBox 创建文件并下载 [已解决]
- javascript - 即使电话号码正确,asp.net 中的正则表达式验证也会显示错误消息
- c - CMake:如何对多个文件运行自定义命令以生成源文件?
- python - 如何将文件名存储在 sqlite 中的图像动态插入到烧瓶服务的表中?
- python - 读取和更新 XML 中的字母数字值并生成新的 output.xml 文件时出现问题
- python - 如何将绑定卷作为参数传递 Docker
- python - “需要 ZIP64 扩展”)LargeZipFile:文件大小需要 ZIP64 扩展
- html - html按钮组下拉菜单不适用于引导程序