从matplotlib找到异常点:boxplot

Abh*_*bhi 9 python matplotlib outliers

我正在使用boxplot绘制非正态分布,并有兴趣使用matplotlib的boxplot函数找出异常值.

除了情节,我有兴趣找出我的代码中的点的值,这些点在箱图中显示为异常值.有没有什么办法可以从boxplot对象中提取这些值以用于我的下游代码?

HYR*_*YRY 18

你的意思是那两条黑线上下的点吗?

from pylab import *
spread= rand(50) * 100
center = ones(25) * 50
flier_high = rand(10) * 100 + 100
flier_low = rand(10) * -100
data =concatenate((spread, center, flier_high, flier_low), 0)
r = boxplot(data)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

存储来自boxplot的返回字典,您可以从中获取所有信息,例如:

top_points = r["fliers"][0].get_data()[1]
bottom_points = r["fliers"][2].get_data()[1]
plot(np.ones(len(top_points)), top_points, "+")
plot(np.ones(len(bottom_points)), bottom_points, "+")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 当前`r["fliers"][2].get_data()[1]`返回`IndexError: list index out of range`,而`r["fliers"][0].get_data()[1]`返回**顶部和底部异常值**,因此使用它一次检索所有异常值。 (2认同)