不准确的离群值与箱形图中的离群值不匹配
Inaccurate outliers values does not match with outlier in box plot
这是我第一次尝试检测异常值,我使用箱形图来检测它。不知何故,代码的输出显示了下限(最小值)和上限(最大值)return 在我看来很奇怪的值,因为它以某种方式使每个数据都是异常值。同时,箱形图在逻辑上正确显示了异常值。我做错了什么以及如何解决这个问题?
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
这里是错误的:
upperBound = Q3 + (1.5 * IQR)
应该是+而不是-。
这是我第一次尝试检测异常值,我使用箱形图来检测它。不知何故,代码的输出显示了下限(最小值)和上限(最大值)return 在我看来很奇怪的值,因为它以某种方式使每个数据都是异常值。同时,箱形图在逻辑上正确显示了异常值。我做错了什么以及如何解决这个问题?
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
这里是错误的:
upperBound = Q3 + (1.5 * IQR)
应该是+而不是-。