Isolation Forest Models API¶
The family name does not imply one score scale. See the isolation score table before thresholding outputs.
ASDIsolationForest ¶
ASDIsolationForest(n_estimators: int = 100, max_samples: int = 256, window_size: int | None = None, retrain_interval: int | None = None, seed: int | None = None)
Bases: BaseModel
Isolation Forest for streaming data using a sliding reference window.
iForestASD periodically discards the current batch Isolation Forest and
trains a complete replacement forest on a recent reference window. This
implementation keeps that window bounded, samples each isolation tree from
it, and retrains after retrain_interval new observations.
The original paper does not provide an author-maintained public repository.
The window/retraining structure is cross-checked against the open-source
PySAD IForestASD reference implementation, which implements Algorithm 2
from the paper without its simulation-specific concept-drift step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_estimators
|
int
|
Number of isolation trees in each replacement forest. |
100
|
max_samples
|
int
|
Maximum reference-window samples used to build each tree. |
256
|
window_size
|
int | None
|
Number of recent samples retained for retraining. If
|
None
|
retrain_interval
|
int | None
|
New samples between forest replacements. If |
None
|
seed
|
int | None
|
Random seed for reproducibility. |
None
|
References
Ding, Z., & Fei, M. (2013). An Anomaly Detection Approach Based on Isolation Forest Algorithm for Streaming Data using Sliding Window. https://doi.org/10.3182/20130902-3-CN-3020.00044 Reference implementation: https://github.com/selimfirat/pysad Liu, F. T., Ting, K. M., & Zhou, Z.-H. (2008). Isolation Forest. https://doi.org/10.1109/ICDM.2008.17
HalfSpaceTrees ¶
HalfSpaceTrees(n_trees: int = 10, height: int = 8, window_size: int = 250, seed: int | None = None)
Bases: BaseModel
Half-Space Trees for streaming anomaly detection.
Half-Space Trees (HST) is an ensemble method for detecting anomalies in streaming data. It builds multiple random trees that partition the feature space using half-space cuts (axis-aligned splits).
The algorithm tracks the "mass" (visit count) at each node during training. Anomalies are identified by having low mass - they fall into regions of feature space that are rarely visited.
Each tree first creates the randomly perturbed work space described in the paper. Every internal node then selects a random feature and bisects that feature's current interval at its midpoint. Reference and latest-window masses are recorded at every traversed node.
The random work spaces are constructed for inputs in [0, 1]. Scale
other feature ranges before passing them to this detector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_trees
|
int
|
Number of trees in the ensemble. Default is 10. |
10
|
height
|
int
|
Maximum depth of each tree. Default is 8. |
8
|
window_size
|
int
|
Number of samples per reference window. After window_size samples, mass counters are reset. Default is 250. |
250
|
seed
|
int | None
|
Random seed for reproducibility. Default is None. |
None
|
Examples:
from aberrant.model.iforest import HalfSpaceTrees
from aberrant.transform.preprocessing import MinMaxScaler
stream = [
{"x": 0.0, "y": 0.1},
{"x": 0.2, "y": 0.1},
{"x": 0.1, "y": 0.3},
]
pipeline = MinMaxScaler() | HalfSpaceTrees(n_trees=5, seed=42)
pipeline.learn_one(stream[0])
scores = []
for point in stream[1:]:
score = pipeline.score_one(point)
scores.append(score)
pipeline.learn_one(point)
assert len(scores) == 2
References
Tan, S. C., Ting, K. M., & Liu, T. F. (2011). Fast anomaly detection for streaming data. In Proceedings of the Twenty-Second International Joint Conference on Artificial Intelligence (pp. 1511-1516). https://www.ijcai.org/Proceedings/11/Papers/254.pdf
learn_one ¶
Update the model with a new observation.
This increments mass counters along the path to the leaf in each tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
Feature dictionary with string keys and float values. Values should be in [0, 1] range for best results. |
required |
score_one ¶
Compute anomaly score for a point.
The score is based on the mass (visit frequency) accumulated along the path to the leaf. Lower mass indicates anomaly.
The score is normalized to [0, 1] where higher values indicate more anomalous points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
Feature dictionary with string keys and float values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Anomaly score in [0, 1]. Higher = more anomalous. |
MondrianIsolationForest ¶
MondrianIsolationForest(n_estimators: int = 100, subspace_size: int = 256, lambda_: float = 1.0, seed: int | None = None)
Bases: BaseModel
Online isolation forest built from Mondrian trees.
The tree update follows online Mondrian block-extension mechanics, while anomaly scoring uses Isolation Forest path-length normalization. The original Mondrian Forest is a supervised classification model and does not define this anomaly score, so this class is a custom hybrid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_estimators
|
int
|
Number of trees in the forest. |
100
|
subspace_size
|
int
|
Number of features sampled per tree. |
256
|
lambda_
|
float
|
Mondrian lifetime budget. |
1.0
|
seed
|
int | None
|
Random seed for reproducibility. |
None
|
References
Lakshminarayanan, B., Roy, D. M., & Teh, Y. W. (2014). Mondrian Forests: Efficient Online Random Forests. https://proceedings.neurips.cc/paper_files/paper/2014/hash/195f15384c2a79cedf293e4a847ce85c-Abstract.html
OnlineIsolationForest ¶
OnlineIsolationForest(num_trees: int = 100, max_leaf_samples: int = 32, tree_type: Literal['fixed', 'adaptive'] = 'adaptive', subsample: float = 1.0, window_size: int = 2048, branching_factor: int = 2, metric: Literal['axisparallel'] = 'axisparallel', n_jobs: int = 1, seed: int | None = None)
Bases: BaseModel
Incremental isolation forest with sliding-window unlearning.
Scores are path-length isolation scores in [0, 1]. Learned batches are
retained until window_size is exceeded, then the oldest points are
unlearned from the trees.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_trees
|
int
|
Number of independently seeded online isolation trees. |
100
|
max_leaf_samples
|
int
|
Base leaf population at which splitting becomes eligible. |
32
|
tree_type
|
Literal['fixed', 'adaptive']
|
|
'adaptive'
|
subsample
|
float
|
Independent per-tree probability of selecting a point for a
tree update, in |
1.0
|
window_size
|
int
|
Maximum number of learned points retained for sliding-window unlearning. |
2048
|
branching_factor
|
int
|
Number of children created at each split. It must be greater than one. |
2
|
metric
|
Literal['axisparallel']
|
Split geometry. Only |
'axisparallel'
|
n_jobs
|
int
|
Tree-worker count. |
1
|
seed
|
int | None
|
Root seed from which independent per-tree generators are spawned. |
None
|
References
Leveni, F., Weigert Cassales, G., Pfahringer, B., Bifet, A., & Boracchi, G. (2024). Online Isolation Forest. https://proceedings.mlr.press/v235/leveni24a.html
Initialize an Online Isolation Forest.
score_one ¶
Score one validated feature mapping without mutating its schema.
RandomCutForest ¶
RandomCutForest(n_trees: int = 40, sample_size: int = 256, shingle_size: int = 1, warmup_samples: int | None = None, normalize_score: bool = False, score_scale: float = 8.0, seed: int | None = None)
Bases: BaseModel
Random Cut Forest for online anomaly detection.
This model keeps an ensemble of random cut trees over a bounded sample of
recent shingled points. learn_one performs one-sample updates and
forgetting, while score_one returns an anomaly score for one sample.
Tree insertion/removal and raw anomaly scoring follow Random Cut Tree
mechanics. score_one temporarily inserts each query, computes its
collusive displacement (CoDisp), removes it, and restores RNG state. Optional
exponential scaling is a monotonic presentation transform over raw CoDisp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_trees
|
int
|
Number of random cut trees. |
40
|
sample_size
|
int
|
Maximum number of stored shingled points. |
256
|
shingle_size
|
int
|
Number of consecutive points concatenated per tree insert. |
1
|
warmup_samples
|
int | None
|
Number of inserted shingles before non-zero scoring.
If |
None
|
normalize_score
|
bool
|
If True, map raw CoDisp to [0, 1]. |
False
|
score_scale
|
float
|
Scale for score normalization when enabled. |
8.0
|
seed
|
int | None
|
Random seed for reproducibility. |
None
|
References
Guha, S., Mishra, N., Roy, G., & Schrijvers, O. (2016). Robust Random Cut Forest Based Anomaly Detection on Streams. https://proceedings.mlr.press/v48/guha16.html Reference implementation: https://github.com/aws/random-cut-forest-by-aws
StreamRandomHistogramForest ¶
StreamRandomHistogramForest(n_estimators: int = 25, max_depth: int = 5, window_size: int = 256, seed: int | None = None)
Bases: BaseModel
STREamRHF tree-based unsupervised anomaly detector.
The forest follows the authors' implementation structure:
- split attributes are sampled proportionally to
log(kurtosis + 1), - every node keeps fixed random quantiles for attribute and split selection,
- an insertion rebuilds a subtree when its selected attribute changes,
- each completed current window replaces the reference forest,
- anomaly score is the sum of
log(n / leaf_size)across trees after candidate insertion.
score_one previews insertion along the affected tree paths so it
reproduces the candidate-inclusive score without mutating learned state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_estimators
|
int
|
Number of independently seeded random histogram trees. |
25
|
max_depth
|
int
|
Maximum tree depth. |
5
|
window_size
|
int
|
Number of samples in the initial reference window and in each subsequent replacement window. It must exceed one. |
256
|
seed
|
int | None
|
Root seed from which independent per-tree random streams are derived. |
None
|
References
Nesic, S., et al. (2022). STREamRHF: Tree-Based Unsupervised Anomaly Detection for Data Streams. https://doi.org/10.1109/AICCSA56895.2022.10017876 Original implementation: https://github.com/stefannesic/streamRHF
XStream ¶
XStream(k: int = 100, n_chains: int = 100, depth: int = 15, cms_width: int = 1024, cms_num_hashes: int = 4, window_size: int = 256, init_sample_size: int = 256, density: float = 1.0 / 3.0, max_feature_cache_size: int | None = 10000, seed: int | None = None)
Bases: BaseModel
StreamHash and half-space-chain detector for evolving feature streams.
Scores remain zero until init_sample_size projected points initialize
the chains and one complete reference window has been observed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
Dimension of the StreamHash feature projection. |
100
|
n_chains
|
int
|
Number of independently sampled half-space chains. |
100
|
depth
|
int
|
Number of levels and count sketches in each chain. |
15
|
cms_width
|
int
|
Number of counters in every count-min sketch row. |
1024
|
cms_num_hashes
|
int
|
Number of independently hashed rows per count-min sketch. |
4
|
window_size
|
int
|
Number of learned projected points per current/reference window swap. |
256
|
init_sample_size
|
int
|
Number of projected points used to establish chain scales. These points also populate the reference-window counters. |
256
|
density
|
float
|
Fraction of projected coordinates updated by each input
feature's deterministic signed projection, in |
1.0 / 3.0
|
max_feature_cache_size
|
int | None
|
Maximum cached feature-name projections, with
least-recently-used eviction. |
10000
|
seed
|
int | None
|
Seed for chain, shift, and sketch-hash generation. Feature-name projections are also derived deterministically from this seed. |
None
|
References
Manzoor, E., Lamba, H., & Akoglu, L. (2018). xStream: Outlier Detection in Feature-Evolving Data Streams. KDD '18. https://doi.org/10.1145/3219819.3220107