Sklearn异常检测模型一览
Robust covariance:
https://scikit-learn.org/stable/modules/generated/sklearn.covariance.EllipticEnvelope.html#sklearn.covariance.EllipticEnvelope
# Robust convariance
import numpy as np
from sklearn.covariance import EllipticEnvelope
true_cov = np.array([[.8, .3],
[.3, .4]])
X = np.random.RandomState(0).multivariate_normal(mean=[0, 0],
cov=true_cov,
size=500)
cov = EllipticEnvelope(random_state=0).fit(X)
# predict returns 1 for an inlier and -1 for an outlier
cov.predict([[0, 0],
[3, 3]])
One-Class SVM:
https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM
# SVM有监督 from sklearn.svm import OneClassSVM X = [[0], [0.44], [100], [0.46], [1]] clf = OneClassSVM(gamma='auto').fit(X) clf.predict(X) # clf.score_samples(X)
Isolation Forest:
https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest
# IsolationForest from sklearn.ensemble import IsolationForest # X = [[-1.1], [0.3], [0.5], [100]] X=[[3910], [70], [3910], [3920], [3890]] clf = IsolationForest(random_state=0).fit(X) clf.predict(X) # clf.fit([[0.1], [1400], [90],[1210]])
Local Outlier Factor:
https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor
# Local Outlier Factor import numpy as np from sklearn.neighbors import LocalOutlierFactor X = [[-1.1], [0.2], [101.1], [0.3]] clf = LocalOutlierFactor(n_neighbors=2) clf.fit_predict(X) clf.negative_outlier_factor_
参考:https://blog.csdn.net/hustqb/article/details/75216241
sklearn原文:https://scikit-learn.org/stable/auto_examples/miscellaneous/plot_anomaly_comparison.html