【问题标题】:How to get outlier values for a specific category with boxplot_stats如何使用 boxplot_stats 获取特定类别的异常值
【发布时间】:2021-10-26 18:16:23
【问题描述】:

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

# 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()

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

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}]

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

【问题讨论】:

    标签: python pandas matplotlib seaborn boxplot


    【解决方案1】:
    • 需要指定'species'
      • boxplot_stats(data["sepal_length"]) 是所有 'species' 的统计信息。
      • 使用.locBoolean indexing 选择正确的类别。
    • 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)
    

    【讨论】:

    • 过滤传单是否会返回所有异常值?喜欢df[df["sepal_length"] == 4.9]
    • 你写的返回所有 sepal_length 等于 4.9 的行。我的答案仅返回特定物种的 sepal_length 的统计信息,其中包括所有传单。我现在要睡觉了,直到太平洋标准时间上午 9 点(UTC-7)之后才会回复
    • 如何返回异常值行?请在您有空时回复。
    • @Pluviophile 这个问题how to use pandas filter with IQR 展示了如何过滤数据框。在示例数据框中,有 3 个物种,每个物种有 4 个指标。需要为每个组进行计算,因为它们都有不同的指标。
    • 我应该过滤每个组并应用 IQR 并过滤数据吧?
    猜你喜欢
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    相关资源
    最近更新 更多