Outlier Detection Cheat Sheet
Covers statistical and machine-learning methods for identifying outliers, including Z-score, IQR, Isolation Forest, and Local Outlier Factor, with runnable examples.
2 PagesIntermediateMar 5, 2026
Detection Methods
Common approaches ranked from simple to model-based.
- Z-score- Flags points where |value - mean| / std exceeds a threshold (commonly 3); assumes roughly normal data
- IQR method- Flags points below Q1 - 1.5*IQR or above Q3 + 1.5*IQR; robust to non-normal distributions
- Isolation Forest- Isolates points by random recursive splitting; anomalies need fewer splits to isolate
- Local Outlier Factor (LOF)- Compares local density of a point to its neighbors' density; good for varying-density clusters
- Elliptic Envelope- Fits a robust Gaussian ellipse to the data; points outside it are outliers (assumes elliptical data)
- DBSCAN- Density-based clustering where points not assigned to any cluster are treated as outliers
IQR Method in Pandas
Flag outliers in a single column using the interquartile range.
python
Q1 = df['value'].quantile(0.25)Q3 = df['value'].quantile(0.75)IQR = Q3 - Q1lower, upper = Q1 - 1.5 * IQR, Q3 + 1.5 * IQRoutliers = df[(df['value'] < lower) | (df['value'] > upper)]print(f"Found {len(outliers)} outliers outside [{lower:.2f}, {upper:.2f}]")
Isolation Forest
Unsupervised outlier detection for multivariate data.
python
from sklearn.ensemble import IsolationForestclf = IsolationForest(n_estimators=100, contamination=0.05, random_state=42)clf.fit(X)# -1 = outlier, 1 = inlierlabels = clf.predict(X)scores = clf.decision_function(X) # Higher = more normal
Local Outlier Factor
Detect outliers based on local density deviation.
python
from sklearn.neighbors import LocalOutlierFactorlof = LocalOutlierFactor(n_neighbors=20, contamination=0.05)labels = lof.fit_predict(X) # -1 = outlier, 1 = inlierscores = lof.negative_outlier_factor_ # More negative = more anomalous
Pro Tip
Z-score and the elliptic envelope both assume roughly Gaussian, single-cluster data -- with skewed distributions or multiple clusters, prefer IQR, Isolation Forest, or LOF instead.
Was this cheat sheet helpful?
Explore Topics
#OutlierDetection#OutlierDetectionCheatSheet#DataScience#Intermediate#DetectionMethods#IQRMethodInPandas#IsolationForest#LocalOutlierFactor#Functions#MachineLearning#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance