如何使用 boxplot_stats 获取特定类别的离群值

How to get outlier values for a specific category with boxplot_stats

我正在尝试使用箱线图提取异常值。

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
# set a grey background (use sns.set_theme() if seaborn version 0.11.0 or above) 
sns.set(style="darkgrid")
df = sns.load_dataset('iris')

sns.boxplot(y=df["species"], x=df["sepal_length"])
plt.show()

上图显示异常值。我尝试使用 提取异常值。但是传单显示一个空数组。

from matplotlib.cbook import boxplot_stats  
boxplot_stats(data["sepal_length"])

输出

[{'cihi': 5.966646952167348,
  'cilo': 5.633353047832651,
  'fliers': array([], dtype=float64),
  'iqr': 1.3000000000000007,
  'mean': 5.843333333333334,
  'med': 5.8,
  'q1': 5.1,
  'q3': 6.4,
  'whishi': 7.9,
  'whislo': 4.3}]

有没有办法提取箱线图中显示的异常值?

  • 需要指定'species'
    • boxplot_stats(data["sepal_length"])是所有'species'.
    • 的统计数据
    • 使用 .loc and Boolean indexing 来 select 正确的类别。
  • answer 展示了如何使用 pandas 方法进行计算。
  • matplotlib.pyplot.boxplotNotes 部分显示了异常值的计算方式。
  • 测试于 python 3.8.11pandas 1.3.2matplotlib 3.4.3seaborn 0.11.2
from matplotlib import boxplot_stats
import seaborn as sns

# load the data
df = sns.load_dataset('iris')

boxplot_stats(df.loc[df.species.eq('virginica'), "sepal_length"])

[{'mean': 6.587999999999998,
  'iqr': 0.6750000000000007,
  'cilo': 6.350128717727511,
  'cihi': 6.649871282272489,
  'whishi': 7.9,
  'whislo': 5.6,
  'fliers': array([4.9]),
  'q1': 6.225,
  'med': 6.5,
  'q3': 6.9}]

获取所有异常值

for species, data in df.groupby('species'):
    data = data.iloc[:, :-1]  # drop off the species column
    print(f'Outliers for: {species}')
    stats = boxplot_stats(data)
    for col, stat in zip(data.columns, stats):
        print(f"{col}: {stat['fliers'].tolist()}")
    print('\n')

[out]:
Outliers for: setosa
sepal_length: []
sepal_width: [2.3, 4.4]
petal_length: [1.1, 1.0, 1.9, 1.9]
petal_width: [0.5, 0.6]


Outliers for: versicolor
sepal_length: []
sepal_width: []
petal_length: [3.0]
petal_width: []


Outliers for: virginica
sepal_length: [4.9]
sepal_width: [2.2, 3.8, 3.8]
petal_length: []
petal_width: []

seaborn.catplot

sns.catplot(kind='box', data=df.melt(id_vars='species'), x='value', y='variable', hue='species', aspect=1.5)