Boxplots en IQR

Anomaly Detection in Python

Bekhruz (Bex) Tuychiev

Kaggle Master, Data Science Content Creator

Boxplots: samenvatting

  • Boxplots:
    • Zijn visuele versies van de 5-getallensamenvatting
    • Tonen ligging, spreiding en scheefheid
    • Geven uitschieters aan

Een voorbeeldboxplot met annotaties.

Anomaly Detection in Python

Onderdelen van een boxplot

Een boxplot met pijlannotaties bij het 25e en 75e percentiel en de doos.

Anomaly Detection in Python

Snorren

Nog een boxplot met meer onderdelen geannoteerd: de mediaan, een voorbeelduitschieter, onder- en bovengrens voor uitschieters.

Anomaly Detection in Python

Interkwartielafstand (IQR)

  • IQR - interkwartielafstand
  • IQR = Q3 - Q1
  • Snorlengtes: combinatie van IQR en een factor

Dezelfde boxplot als op de vorige slide maar kleiner.

Anomaly Detection in Python

Snorlengtes berekenen

  • Factor = 1,5
  • Ondergrens: $Q1 - 1.5 * IQR$
  • Bovengrens: $Q3 + 1.5 * IQR$

Dezelfde boxplot als op de vorige slide.

Anomaly Detection in Python

Boxplots tekenen

import matplotlib.pyplot as plt

plt.boxplot(sales)
plt.xlabel("Product sales")

Een boxplot van productverkopen met veel uitschieters die een zwarte lijn vormen boven de bovengrens.

Anomaly Detection in Python

Snorlengtes regelen

plt.boxplot(sales, whis=2.5)
plt.xlabel("Product sales")

Een boxplot van productverkopen met minder uitschieters boven de bovenste snor.

Anomaly Detection in Python

IQR in code

# Calculate the percentiles
q1 = sales.quantile(0.25)
q3 = sales.quantile(0.75)


# Calculate IQR IQR = q3 - q1 # Set Multiplying factor factor = 2.5
Anomaly Detection in Python

Uitschieters vinden met IQR

# Calculate the limits
lower_limit = q1 - (IQR * factor)


upper_limit = q3 + (IQR * factor)
# Create masks
is_lower = sales < lower_limit
is_upper = sales > upper_limit


# Filter outliers = sales[is_lower | is_upper] # Print the # of outliers print(len(outliers))
29
Anomaly Detection in Python

Flexibiliteit van de methode

  • Boxplots en IQR-filtering bieden veel flexibiliteit
  • Maakt eigen regels voor uitschieters mogelijk
Anomaly Detection in Python

Laten we oefenen!

Anomaly Detection in Python

Preparing Video For Download...