Sub topic 14
Extended Isolation Forest and Other Alternatives
This chapter assumes you can train IsolationForest in scikit-learn and read its scores. It also assumes you know the axis-parallel split limitation from the previous chapter. Here you will see how Extended Isolation Forest fixes that limitation, and when a different detector is a better choice.
The problem Extended Isolation Forest solves
A standard isolation tree picks one feature and one split value. Every cut is parallel to an axis. In two dimensions, the regions that result are rectangles. Path lengths then show a pattern of low-score bands that line up with the axes. Points far from the data along a diagonal can look more normal than they should.
Extended Isolation Forest (EIF) changes the split. It draws a random slope vector and a random intercept point, then cuts along a hyperplane. The hyperplane can be tilted at any angle. Path lengths and the score formula stay the same, so you read EIF scores exactly like standard scores.
| Aspect | Isolation Forest | Extended Isolation Forest |
|---|---|---|
| Split shape | Axis-parallel | Random hyperplane |
| Score bias along axes | Present | Reduced |
| Score formula | 2^(-E[h(x)] / c(n)) | Same |
| Availability | scikit-learn | `eif` or `isotree` packages |
EIF is not part of scikit-learn. The eif package implements the original method. The isotree package offers an extended mode through its ndim parameter, plus handling of missing values and categorical features. Check each package's documentation for its current API before you depend on it.
Other detectors worth knowing
Three scikit-learn detectors cover most alternatives:
- Local Outlier Factor (LOF) compares a point's local density with the density of its neighbors. It handles clusters of different density well. It needs neighbor searches, so it gets slow on large data.
- One-Class SVM learns a boundary around the normal data with a kernel. It is sensitive to scaling and to
gamma, and it trains slowly on large datasets. - Elliptic Envelope fits a Gaussian and flags points far from it. It works only when the normal data is roughly one ellipse.
The code below compares three of them on the data from the evaluation chapter. All three expose a score where lower means more normal, so we negate each one.
import numpy as np
from sklearn.ensemble import IsolationForest
from sklearn.neighbors import LocalOutlierFactor
from sklearn.svm import OneClassSVM
from sklearn.metrics import roc_auc_score
rng = np.random.RandomState(0)
X_normal = rng.normal(size=(1000, 2))
X_outliers = rng.uniform(low=-6.0, high=6.0, size=(50, 2))
X = np.vstack([X_normal, X_outliers])
y_true = np.r_[np.zeros(len(X_normal)), np.ones(len(X_outliers))]
iso = IsolationForest(random_state=0).fit(X)
lof = LocalOutlierFactor(n_neighbors=35).fit(X)
svm = OneClassSVM(nu=0.05, gamma="scale").fit(X)
scores = {
"IsolationForest": -iso.score_samples(X),
"LOF": -lof.negative_outlier_factor_,
"OneClassSVM": -svm.score_samples(X),
}
for name, s in scores.items():
print(f"{name}: ROC AUC = {roc_auc_score(y_true, s):.3f}")LocalOutlierFactor with its default novelty=False scores only the training data, through negative_outlier_factor_. To score new data, set novelty=True and use score_samples on the new points.
Choosing between them
Exercise
Your Isolation Forest score map shows bands of low scores that follow the x and y axes, and a point off the diagonal looks normal when it should not. Which method addresses this, and what changes inside the tree?
Show answer
Extended Isolation Forest. Each split uses a randomly oriented hyperplane (random slope and intercept) instead of a cut on a single feature, so the regions are no longer limited to axis-aligned rectangles. The score formula stays the same.