mojolearn.LocalOutlierFactor#
- class mojolearn.LocalOutlierFactor(n_neighbors=20, *, algorithm='auto', leaf_size=30, metric='minkowski', p=2, metric_params=None, contamination='auto', novelty=False, n_jobs=None)[source]#
Bases:
_XNeighborsUnsupervised outlier detection by the local outlier factor (LOF).
Scores each sample by how isolated it is relative to its k nearest neighbors, following scikit-learn’s
LocalOutlierFactor. Neighbor searches are exact brute force with the euclidean metric; ties between equal distances go to the lower training index, and a duplicate of a training row counts as its neighbor. Computation is float32 (scikit-learn uses float64).With
novelty=False(the default) use fit_predict() to label the training data. Withnovelty=Truefit on clean data and call predict(), decision_function() or score_samples() on new data.- Parameters:
n_neighbors (int, default 20) – Number of neighbors.
fitusesmin(n_neighbors, n_samples - 1).algorithm (str, default ‘auto’) – Accepted for scikit-learn compatibility; every search is exact brute force.
leaf_size (int, default 30) – Accepted for scikit-learn compatibility; unused.
metric (str, default ‘minkowski’) –
'euclidean', or'minkowski'withp=2. Any other metric raisesNotImplementedError.p (int, default 2) – Minkowski power; only 2 is supported.
metric_params (dict or None, default None) – Not supported; a non-empty value raises
NotImplementedError.contamination (‘auto’ or float, default ‘auto’) – Expected outlier fraction, in (0, 0.5].
'auto'setsoffset_to -1.5; a float sets it to that percentile of the training scores.novelty (bool, default False) – Whether the model scores new data (
predict,decision_function,score_samples) instead of the training data (fit_predict).n_jobs (int or None, default None) – Accepted for scikit-learn compatibility; unused.
numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this estimator (
'fast'is Apple only). None uses the process default.
- Variables:
negative_outlier_factor_ (Array of shape (n_samples,)) – Negative LOF of each training sample; lower is more abnormal.
offset_ (float) – Threshold on the score; samples below it are outliers.
n_neighbors_ (int) – Number of neighbors actually used.
n_features_in_ (int) – Number of features seen in
fit.n_samples_fit_ (int) – Number of training samples.
Examples
>>> import numpy as np, mojolearn >>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32) >>> labels = mojolearn.LocalOutlierFactor(n_neighbors=20).fit_predict(X)
- __init__(n_neighbors=20, *, algorithm='auto', leaf_size=30, metric='minkowski', p=2, metric_params=None, contamination='auto', novelty=False, n_jobs=None)[source]#
Methods
Shifted outlier score of new samples:
score_samples(X) - offset_.fit(X[, y])Fit the model on the training data.
fit_predict(X[, y])Fit the model and label the training samples.
get_params([deep])numeric_mode_used()The numeric mode this estimator runs in.
predict(X)Label new samples as inliers or outliers.
Negative local outlier factor of new samples.
set_params(**params)Set constructor parameters.
vendor_used()The GPU backend this estimator runs on.
- fit(X, y=None)[source]#
Fit the model on the training data.
- Parameters:
X (array-like or scipy.sparse matrix of shape (n_samples, n_features)) – Training data; at least 2 samples. Sparse input is densified.
y (None) – Ignored.
- Returns:
self (LocalOutlierFactor) – The fitted estimator.
- fit_predict(X, y=None)[source]#
Fit the model and label the training samples.
Available only with
novelty=False; otherwise raisesAttributeError.- Parameters:
X (array-like of shape (n_samples, n_features)) – Training data.
y (None) – Ignored.
- Returns:
labels (Array of shape (n_samples,), int64) – -1 for outliers, 1 for inliers.
- score_samples(X)[source]#
Negative local outlier factor of new samples.
Available only with
novelty=True; otherwise raisesAttributeError. Lower scores are more abnormal.- Parameters:
X (array-like of shape (n_queries, n_features)) – New samples.
- Returns:
scores (Array of shape (n_queries,), float32)