mojolearn.IsolationForest#

class mojolearn.IsolationForest(*, n_estimators=100, max_samples='auto', max_depth=None, max_features=1.0, bootstrap=False, contamination='auto', random_state=None, warm_start=False, n_jobs=None, verbose=False, output_type=None)[source]#

Bases: NumericModeMixin

Isolation Forest anomaly detector with scikit-learn’s interface.

Builds random isolation trees on subsamples of X and scores each row by how quickly the trees isolate it. Parameters and defaults follow scikit-learn and cuML. Parameters that are not supported raise NotImplementedError instead of being ignored.

X is converted to float32; NaN or infinity in X raises ValueError. random_state=None means seed 0, so the default fit is deterministic, unlike scikit-learn and cuML. The fitted model keeps the training matrix; depending on the installed build, a scoring call may rebuild the forest from it, which gives the same forest bit for bit.

Parameters:
  • n_estimators (int, default 100) – Number of trees; at least 1.

  • max_samples (‘auto’, int or float, default ‘auto’) – Rows per tree: ‘auto’ means min(256, n_samples), a positive int is clipped to n_samples, a float in (0, 1] is a fraction of n_samples.

  • max_depth (int or None, default None) – Maximum tree depth. None means ceil(log2(max_samples_)).

  • max_features (int or float, default 1.0) – Features per tree: an int in [1, n_features] or a float in (0, 1].

  • bootstrap (bool, default False) – Draw each tree’s subsample with replacement.

  • contamination (‘auto’ or float, default ‘auto’) – Expected fraction of anomalies. ‘auto’ sets offset_ to -0.5; a float in (0, 0.5] sets it from the training scores.

  • random_state (int or None, default None) – Seed in [0, 2**32 - 1]. None means 0.

  • warm_start (bool, default False) – Only False is accepted.

  • n_jobs (None, default None) – Only None is accepted.

  • verbose (bool, default False) – Only False is accepted.

  • output_type (None, default None) – Only None is accepted; results are mojolearn.Array.

Variables:
  • offset_ (float) – Shift used by decision_function: -0.5 for contamination='auto', otherwise the 100 * contamination percentile of the training scores (linear interpolation).

  • max_samples_ (int) – The resolved per-tree subsample size.

  • n_features_in_ (int) – Number of features seen in fit.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> model = mojolearn.IsolationForest(n_estimators=100).fit(X)
>>> labels = model.predict(X)  # -1 anomaly, 1 inlier
__init__(*, n_estimators=100, max_samples='auto', max_depth=None, max_features=1.0, bootstrap=False, contamination='auto', random_state=None, warm_start=False, n_jobs=None, verbose=False, output_type=None)[source]#

Methods

decision_function(X)

score_samples(X) - offset_, computed in float32; negative means predicted anomalous.

fit(X[, y, sample_weight])

Fit the isolation forest.

fit_predict(X[, y, sample_weight])

Fit on X and return predict(X).

get_params([deep])

load(path)

Load a model saved by save.

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Label each row: -1 for an anomaly, 1 for an inlier.

save(path)

Write the fitted model to path as an npz file.

score_samples(X)

Anomaly score of each row; lower is more anomalous.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y=None, sample_weight=None)[source]#

Fit the isolation forest.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data, converted to float32.

  • y (ignored)

  • sample_weight (None, default None) – Only None is accepted; other values raise NotImplementedError.

Returns:

self

score_samples(X)[source]#

Anomaly score of each row; lower is more anomalous.

This is the negated isolation-forest anomaly score, as in scikit-learn and cuML.

Returns:

Array of shape (n_samples,), float32

decision_function(X)[source]#

score_samples(X) - offset_, computed in float32; negative means predicted anomalous.

Returns:

Array of shape (n_samples,), float32

predict(X)[source]#

Label each row: -1 for an anomaly, 1 for an inlier.

Returns:

Array of shape (n_samples,), int32

fit_predict(X, y=None, sample_weight=None)[source]#

Fit on X and return predict(X).

save(path)[source]#

Write the fitted model to path as an npz file.

The file holds the training matrix and the resolved parameters the forest is built from, so it is as large as the training data.

classmethod load(path)[source]#

Load a model saved by save.

The saved parameters pass through the constructor again, so the same checks apply to a loaded file.