PYTHON_异常值检测
借鉴于 http://scikit-learn.org/stable/modules/outlier_detection.html#novelty-and-outlier-detection
novelty detection
这些训练数据没有被异常值所污染,我们有兴趣在新的观测中发现异常。
outlier detection
训练数据中包含异常值,和我们需要合适的训练数据中心模式忽略的越轨的意见。
机器学习(无监督学习)
                学习:estimator.fit(X_train)
                
                预测:estimator.predict(X_test),异常值为-1
               
二、novelty detection
from sklearn import svm xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500)) # Generate train data 生成训练数据 X = 0.3 * np.random.randn(100, 2) X_train = np.r_[X + 2, X - 2] # Generate some regular novel observations 生成一些常规的新奇观察 X = 0.3 * np.random.randn(20, 2) X_test = np.r_[X + 2, X - 2] # Generate some abnormal novel observations 产生一些异常新颖的观察 X_outliers = np.random.uniform(low=-4, high=4, size=(20, 2)) # fit the model 模型学习 clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1) clf.fit(X_train) y_pred_train = clf.predict(X_train) y_pred_test = clf.predict(X_test) y_pred_outliers = clf.predict(X_outliers) n_error_train = y_pred_train[y_pred_train == -1].size n_error_test = y_pred_test[y_pred_test == -1].size n_error_outliers = y_pred_outliers[y_pred_outliers == 1].size三、Outlier Detection
covariance.EmpiricalCovariance算法
在高斯分布数据上显示具有马氏距离的协方差估计的示例。
gen_cov = np.eye(n_features) gen_cov[0, 0] = 2. X = np.dot(np.random.randn(n_samples, n_features), gen_cov) # add some outliers 添加一些异常值 outliers_cov = np.eye(n_features) outliers_cov[np.arange(1, n_features), np.arange(1, n_features)] = 7. X[-n_outliers:] = np.dot(np.random.randn(n_outliers, n_features),outliers_cov) # fit a Minimum Covariance Determinant (MCD) robust estimator to data # 拟合最小协方差行列式(MCD)对数据的鲁棒估计 robust_cov = MinCovDet().fit(X) # compare estimators learnt from the full data set with true parameters # 比较估计器从完整的数据集和真实参数的学习 emp_cov = EmpiricalCovariance().fit(X) # Computes the squared Mahalanobis distances of given observations. # 计算给定观测值的平方Mahalanobis距离。 Y = emp_cov.mahalanobis(X)ensemble.IsolationForest算法
在高维数据集中执行异常值检测的一种有效方法是使用随机森林
neighbors.LocalOutlierFactor(LOF)算法
对中等高维数据集执行异常值检测的另一种有效方法是使用局部离群因子(LOF)算法。
结合以上四种异常检测方法建模比较:
                sklearn.svm(支持向量机)
                
                sklearn.covariance.EllipticEnvelope(高斯分布的协方差估计)
                
                sklearn.ensemble.IsolationForest(随机森林)
                
                sklearn.neighbors.LocalOutlierFactor(LOF)
               


 
                         
       