【问题标题】:Inaccurate outliers values does not match with outlier in box plot不准确的异常值与箱线图中的异常值不匹配
【发布时间】:2023-04-07 00:26:02
【问题描述】:

这是我第一次尝试检测异常值,我使用箱线图来检测它。在我看来,代码的输出以某种方式显示下限(最小值)和上限(最大值)返回奇怪的值,因为它以某种方式使每个数据都是异常值。同时,箱线图在逻辑上显示了异常值的正确可视化。我做错了什么以及如何解决这个问题?

import pandas as pd
import numpy as np
import seaborn as sns

cols = pd.DataFrame({'numbers':[100,300,200,400,500,6000,800,200,200]})

sns.boxplot(x = cols.numbers)

def outlierHandling(numbers):
    numbers = sorted(numbers)
    Q1 , Q3 = np.percentile(numbers, [25,75] , interpolation='nearest')
    print('Q1,Q3 : ',Q1,Q3)
    IQR = Q3 - Q1
    lowerBound = Q1 - (1.5 * IQR)
    upperBound = Q3 - (1.5 * IQR)
    print('lowerBound,upperBound : ',lowerBound,upperBound)
    return lowerBound,upperBound

lowerbound,upperbound = outlierHandling(cols.numbers)
print('Outlier values : \n',cols[(cols.numbers < lowerbound) | (cols.numbers > upperbound)])

输出

Q1,Q3 :  200 500
lowerBound,upperBound :  -250.0 50.0
Outlier values : 
    numbers
0      100
1      300
2      200
3      400
4      500
5     6000
6      800
7      200
8      200

【问题讨论】:

    标签: python numpy outliers


    【解决方案1】:

    这里是错误的:

        upperBound = Q3 + (1.5 * IQR)
    

    应该是+而不是-。

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 1970-01-01
      • 2021-11-06
      • 2023-03-12
      • 2014-03-28
      • 2019-06-03
      • 1970-01-01
      • 2017-03-21
      相关资源
      最近更新 更多