Anomaly Detection in Python
Bekhruz (Bex) Tuychiev
Kaggle Master, Data Science Content Creator
IQR = Q3 - Q1
import matplotlib.pyplot as plt
plt.boxplot(sales)
plt.xlabel("Product sales")
plt.boxplot(sales, whis=2.5)
plt.xlabel("Product sales")
# Calculate the percentiles q1 = sales.quantile(0.25) q3 = sales.quantile(0.75)
# Calculate IQR IQR = q3 - q1 # Set Multiplying factor factor = 2.5
# 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