How to detect outliers with Isolation Forest in Machine Learning
This tutorial shows how to detect outliers (anomalous values) in a numerical dataset using Isolation Forest in Machine Learning. It is useful to clean data before training models, prevent outliers from biasing results, and automate anomaly detection in production pipelines. Throughout the article I explain why the algorithm works, provide a controlled synthetic example and show how to validate the results visually and numerically.
Prerequisites
- Python 3.8+ installed (I recommend using a virtual environment)
- Libraries: scikit-learn, pandas, numpy, matplotlib (install via pip:
pip install scikit-learn pandas numpy matplotlib) - Basic knowledge of Python and DataFrame manipulation with pandas
Step 1: Understand what Isolation Forest is
Isolation Forest is a tree-based algorithm that 'isolates' anomalous points faster than normal points. The intuitive idea is simple: anomalies are rare and distinct, so they require fewer splits to become isolated in a tree of random partitions. The algorithm builds several trees (a forest) with random partitions and uses the average depth at which a point is isolated as an anomaly measure.
Advantages: it scales reasonably well with the number of samples (in many cases close to O(n log n) in practice), does not assume parametric distributions and works for multidimensional data. Limitations: sensitive to the choice of contamination (expected fraction of outliers) and may require feature normalization if scales differ greatly.
Step 2: Prepare a data example
Creating a small synthetic dataset makes it easy to see how the algorithm separates normals from outliers. Here we create 300 normal points from a normal distribution and 20 uniform outliers spread over a larger area. This yields a total of 320 samples and a true outlier percentage of 20/320 ≈ 0.0625 (~6.25%).
import numpy as np
import pandas as pd
rng = np.random.RandomState(42)
# Normal data: two features with normal distribution
X_normal = rng.normal(loc=0, scale=1, size=(300, 2))
# Outliers: distant points
X_outliers = rng.uniform(low=-8, high=8, size=(20, 2))
# Combine
X = np.vstack([X_normal, X_outliers])
df = pd.DataFrame(X, columns=['x1', 'x2'])
print(df.shape) # (320, 2)
print(df.head())
Step 3: Train Isolation Forest
Using scikit-learn's implementation is straightforward. Common parameters to tune: n_estimators (number of trees, typically 100), contamination (expected fraction of outliers, here 0.06), max_samples or subsample for large datasets, and random_state for reproducibility.
from sklearn.ensemble import IsolationForest
# Create and fit the model
clf = IsolationForest(n_estimators=100, contamination=0.06, random_state=42)
clf.fit(df)
# Prediction: -1 = outlier, 1 = normal
preds = clf.predict(df)
# Score (the lower, the more anomalous)
scores = clf.decision_function(df)
df['is_outlier'] = (preds == -1)
df['score'] = scores
Step 4: Interpret the results and tune parameters
After training, start by counting how many outliers were detected and inspecting a sample. If you set contamination=0.06 and have 320 samples, you expect to detect about 19–20 outliers. If you detect many more or many fewer, adjust contamination or examine the features: normalization or removal of collinearity may be needed.
# Count outliers
print(df['is_outlier'].value_counts())
# Sample of detected outliers
print(df[df['is_outlier']].head())
# Extreme score values
print(df['score'].nsmallest(10))
Practical tip: don't rely solely on the binary label. Check the distribution of scores (decision_function): outliers typically have strongly negative scores. If you have verified labels, compute metrics like precision/recall. In very large datasets, use max_samples to reduce computational cost while maintaining good sensitivity.
Step 5: Visualize to confirm (2D example)
A simple 2D visualization helps confirm whether the outliers make sense. If you have more dimensions, reduce with PCA before plotting to see a representative projection. Below we show both a simple scatter and an example with PCA.
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
# Simple scatter (2D)
plt.figure(figsize=(7, 5))
plt.scatter(df['x1'], df['x2'], c=df['is_outlier'], cmap='coolwarm', s=30)
plt.title('Outlier detection with Isolation Forest')
plt.xlabel('x1')
plt.ylabel('x2')
plt.show()
# If you had more features:
pca = PCA(n_components=2, random_state=42)
proj = pca.fit_transform(df[['x1', 'x2']])
plt.figure(figsize=(7,5))
plt.scatter(proj[:,0], proj[:,1], c=df['is_outlier'], cmap='coolwarm', s=30)
plt.title('PCA + Isolation Forest (projection)')
plt.show()
Verify the result
Confirm that the 20 uniformly generated points are, for the most part, marked as outliers and that the detected percentage is close to contamination. In an experiment like this, it is plausible to obtain 18–22 detected outliers; if you get a very different number, review preprocessing and parameters. In real data, validate with domain knowledge, labeled samples or business rules before removing or acting on points marked as outliers.
Conclusion
Isolation Forest is a practical solution to detect outliers in numerical data and scales reasonably well for medium to large datasets. Recommended next steps: experiment with different contamination values, vary n_estimators (100–500) and max_samples, combine with feature normalization and use PCA for visualization. Always validate the output with known samples or business rules to avoid removing valid points and document parameter choices for reproducibility.