Isolation Forest
Detect anomalies in JavaScript and TypeScript with IsolationForest in @kanaries/ml. Includes contamination tuning, anomaly scoring, and sklearn comparison.
View as MarkdownAlgorithm overview
IsolationForest detects anomalies by isolating rare points with shorter path lengths in random partition trees.
This algorithm is especially useful when:
- You have mostly normal behavior with relatively few outliers.
- Labeling anomalies is expensive or unavailable.
- You need near-real-time anomaly scoring in browser or Node.js.
JavaScript implementation
@kanaries/ml provides Isolation Forest in JavaScript for anomaly detection features that need to run inside a product, a browser-based monitoring view, or a Node.js scoring service. That is useful when you want outlier detection near the application layer, for example in fraud heuristics, telemetry triage, or unusual-behavior alerts.
Keeping anomaly scoring in JS makes it easier to connect feature extraction, threshold logic, and downstream product actions without introducing a Python serving dependency.
Quick start
IsolationForest in Python vs JavaScript / TypeScript
If you searched for "IsolationForest in JavaScript" or "IsolationForest in TypeScript", this section maps the familiar scikit-learn call to the equivalent @kanaries/ml usage for browser and Node.js runtimes.
from sklearn.ensemble import IsolationForest
X = [[0, 0], [0.1, 0.2], [0.2, 0.1], [8, 8]]
clf = IsolationForest(n_estimators=50, contamination=0.25, random_state=0)
clf.fit(X)
pred = clf.predict(X)import { Ensemble } from '@kanaries/ml';
const X = [[0, 0], [0.1, 0.2], [0.2, 0.1], [8, 8]];
const clf = new Ensemble.IsolationForest({
subsampling_size: 256,
tree_num: 50,
contamination: 0.25,
random_state: 0,
});
clf.fit(X);
const pred = clf.predict(X); // 1 = anomaly, 0 = normalQuick JavaScript example
import { Ensemble } from '@kanaries/ml';
const X = [[0, 0], [0.1, 0.2], [0.2, 0.1], [8, 8]];
const clf = new Ensemble.IsolationForest({
subsampling_size: 256,
tree_num: 50,
contamination: 0.25,
random_state: 42,
});
clf.fit(X);
const pred = clf.predict(X);
console.log({ pred, score: clf.anomalyScore([8, 8]) });Label convention vs scikit-learn:
@kanaries/mlIsolationForest.predict()returns1for anomalies and0for normal observations (whereas scikit-learn returns-1for anomalies and1for normal). For an interactive walkthrough and contamination slider, see our Isolation Forest Guide & Demo.
Detailed API reference
new Ensemble.IsolationForest(props?: {
subsampling_size?: number; // default: 256
tree_num?: number; // default: 100
contamination?: 'auto' | number; // default: 'auto'
random_state?: number; // optional deterministic seed
})(Legacy positional calls new Ensemble.IsolationForest(256, 50, 0.25) remain supported for backward compatibility, but prefer the options object.)
Parameters
| name | type | default | description |
|---|---|---|---|
| subsampling_size | number | 256 | Number of samples used to build each tree |
| tree_num | number | 100 | Number of isolation trees in the forest |
| contamination | 'auto' | number | 'auto' | Expected proportion of outliers |
| random_state | number | undefined | Seed for deterministic subsampling and splits |
Algorithm
IsolationForest randomly splits features to isolate samples. Points that can be isolated with fewer splits receive shorter average path lengths and higher anomaly scores.
Methods
fit(samplesX: number[][]): voidpredict(samplesX: number[][]): (0 | 1)[](1= anomaly,0= normal)anomalyScore(row: number[]): number(continuous anomaly score in[0, 1])toJSON()/loadModel()(full JSON model serialization)
Implementation workflow
- Train on representative mostly-normal historical samples.
- Predict anomaly labels or scores on incoming events.
- Tune contamination and decision thresholds using alert precision targets.
JavaScript deployment notes
- Train on representative mostly-normal data so anomaly scores reflect real deviations.
- Treat contamination and alert thresholds as product decisions, not just model defaults.
- For real-time scoring, keep feature generation and model prediction in the same Node.js pipeline when possible.
Ensemble Learning
Explore ensemble learning algorithms in JavaScript and TypeScript with @kanaries/ml, including Isolation Forest, AdaBoost, random forests, and bagging models.
RandomForestClassifier
Train Random Forest classifiers in JavaScript and TypeScript with @kanaries/ml. Includes bootstrap sampling, maxFeatures, sklearn comparison, and tuning tips.