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:
NumericModeMixinIsolation Forest anomaly detector with scikit-learn’s interface.
Builds random isolation trees on subsamples of
Xand scores each row by how quickly the trees isolate it. Parameters and defaults follow scikit-learn and cuML. Parameters that are not supported raiseNotImplementedErrorinstead of being ignored.Xis converted to float32; NaN or infinity inXraisesValueError.random_state=Nonemeans 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:
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
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
Xand returnpredict(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
pathas an npz file.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