不正确的离群值与箱形图中的离群值不匹配

时间:2020-02-22 05:13:13

标签: python numpy outliers

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

This is the box plot

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

1 个答案:

答案 0 :(得分:1)

这是错误:

    upperBound = Q3 + (1.5 * IQR)

应该为+,而不是-。