Univariate methods look at one column at a time. Some outliers only appear when several features are combined — for example, a person of age 5 with an income of 100,000. Machine learning based detectors handle these multivariate cases.
Isolation Forest
Randomly splits the data with trees. Outliers are easier to isolate, so they end up with shorter paths and get flagged.
from sklearn.ensemble import IsolationForest
X = df[["Age", "Fare"]].dropna()
iso = IsolationForest(contamination=0.02, random_state=42)
labels = iso.fit_predict(X) # -1 = outlier, 1 = normal
print("Outliers found:", (labels == -1).sum())
print(X[labels == -1].head())
Local Outlier Factor (LOF)
Compares the local density around a point with the density around its neighbours. Points in much sparser regions are outliers.
from sklearn.neighbors import LocalOutlierFactor
lof = LocalOutlierFactor(n_neighbors=20, contamination=0.02)
labels_lof = lof.fit_predict(X) # -1 = outlier
print("Outliers found:", (labels_lof == -1).sum())
DBSCAN (density-based clustering)
Points that do not belong to any dense cluster get the label -1 (noise).
from sklearn.cluster import DBSCAN
from sklearn.preprocessing import StandardScaler
Xs = StandardScaler().fit_transform(X)
db = DBSCAN(eps=0.5, min_samples=10).fit(Xs)
print("Noise points:", (db.labels_ == -1).sum())
Comparison
| Method | Idea | Strength | Limitation |
|---|---|---|---|
| Isolation Forest | Outliers are easy to isolate | Fast, works in high dimensions | Needs a contamination estimate |
| LOF | Compare local densities | Finds local anomalies | Slower on large data |
| DBSCAN | Points outside dense clusters | No need to specify the outlier share | Sensitive to eps and min_samples |
| One-Class SVM | Learn a boundary around normal data | Good for novelty detection | Needs tuning and scaling |
Tips
- Scale the features first for distance/density-based methods (LOF, DBSCAN).
contaminationis your guess of the outlier fraction; inspect the flagged rows manually.- Fit on training data only.