sklearn.covariance.EllipticEnvelope?
class sklearn.covariance.EllipticEnvelope(*, store_precision=True, assume_centered=False, support_fraction=None, contamination=0.1, random_state=None)
用于檢測高斯分布數據集中異常值的對象。
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
store_precision | bool, default=True 指定是否存儲估計的精度。 |
assume_centered | bool, default=False 如果為True,則計算穩健位置和協方差估計的支持度,并從中重新計算協方差估計,而不會先將數據中心化。這在處理均值顯著等于零但不完全為零的數據時很有用。如果為False,則直接使用FastMCD算法計算穩健位置和協方差,而無需進行其他處理。 |
support_fraction | float, default=None 支持MCD原始估算的點數比例。如果為None,則將在算法中使用support_fraction的最小值: [n_sample + n_features + 1] / 2 ,范圍是(0,1)。 |
contamination | float, default=0.1 數據集的污染量,即數據集中異常值的比例,范圍是(0,0.5)。 |
random_state | int or RandomState instance, default=None 確定用于數據混洗的偽隨機數生成器。在多個函數調用之間傳遞同一個整數值以獲得重復的結果。請參閱: Glossary <random_state> 。 |
屬性 | 說明 |
---|---|
location_ | ndarray of shape (n_features,) 估計的位置 |
covariance_ | ndarray of shape (n_features, n_features) 估計的穩健協方差矩陣 |
precision_ | ndarray of shape (n_features, n_features) 估計的偽逆矩陣。(僅在store_precision為True時存儲) |
support_ | ndarray of shape (n_samples,) 用于計算位置和形狀的穩健估計的觀測掩模。 |
offset_ | float 偏移量,用于根據原始分數定義決策函數。我們有以下關系: decision_function = score_samples - offset_ 。偏移量取決于污染量參數,污染量被定義為預預計在訓練中的異常值數量(使決策函數小于0的樣本數)。0.20版中的新功能。 |
raw_location_ | ndarray of shape (n_features,) 校正和重加權之前的原始穩健估計位置。 |
raw_covariance_ | ndarray of shape (n_features, n_features) 校正和重新加權之前的原始穩健估計協方差。 |
raw_support_ | ndarray of shape (n_samples,) 在校正和重新加權之前,用于計算位置和形狀的原始穩健估計的觀測數據掩模。 |
dist_ | ndarray of shape (n_samples,) ( fit 中的)訓練集觀測值的馬氏距離。 |
另見
注
在高維設置中,根據協方差估計進行的異常值檢測可能會中斷或效果不佳。特別是,人們將常常與n_samples > n_features ** 2
的數據打交道。
參考資料
R68ae096da0e4-1 Rousseeuw, P.J., Van Driessen, K. “A fast algorithm for the minimum covariance determinant estimator” Technometrics 41(3), 212 (1999)
示例
>>> 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]])
array([ 1, -1])
>>> cov.covariance_
array([[0.7411..., 0.2535...],
[0.2535..., 0.3053...]])
>>> cov.location_
array([0.0813... , 0.0427...])
方法
方法 | 說明 |
---|---|
correct_covariance (self, data) |
對原始的最小協方差行列式估計值進行校正。 |
decision_function (self, X) |
計算給定觀測值的決策函數。 |
error_norm (self, comp_cov[, norm, scaling, …]) |
計算兩個協方差估計量之間的均方誤差。 |
fit (self, X[, y]) |
擬合EllipticEnvelope模型。 |
fit_predict (self, X[, y]) |
對X執行擬合并返回樣本的標簽。 |
get_params (self[, deep]) |
獲取此估計器的參數。 |
get_precision (self) |
獲取精確矩陣。 |
mahalanobis (self, X) |
計算給定觀測值的平方馬氏距離。 |
predict (self, X) |
根據擬合模型預測樣本的標簽(1為內點,-1為離群點)。 |
reweight_covariance (self, data) |
重新加權原始最小協方差行列式估計值。 |
score (self, X, y[, sample_weight]) |
返回給定測試數據和標簽上的平均精度。 |
score_samples (self, X) |
計算負馬氏距離。 |
set_params (self, **params) |
設置此估算器的參數。 |
__init__(self, *, store_precision=True, assume_centered=False, support_fraction=None, contamination=0.1, random_state=None)
初始化self. 請參閱help(type(self))以獲得準確的說明。
correct_covariance(self, data)[source]
對原始的最小協方差行列式估計值進行修正。
使用Rousseeuw和Van Driessen在[RVD]中建議的經驗修正因子進行修正。
參數 | 說明 |
---|---|
data | array-like of shape (n_samples, n_features) 具有p個特征和n個樣本的數據矩陣。數據集必須是用于計算原始估計值的數據集。 |
返回值 | 說明 |
---|---|
covariance_corrected | ndarray of shape (n_features, n_features) 校正過的穩定協方差估計。 |
參考
[RVD] A Fast Algorithm for the Minimum Covariance Determinant Estimator, 1999, American Statistical Association and the American Society for Quality, TECHNOMETRICS
decision_function(self, X)
計算給定觀測值的決策函數。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 數據矩陣。 |
返回值 | 說明 |
---|---|
decision | ndarray of shape (n_samples, ) 樣本的決策函數。它等于移動的馬氏距離。異常值的閾值為0,從而確保與其他異常值檢測算法的兼容性。 |
error_norm(self, comp_cov, norm='frobenius', scaling=True, squared=True)
計算兩個協方差估計量之間的均方誤差。(在Frobenius規范的意義上)。
參數 | 說明 |
---|---|
comp_cov | array-like of shape (n_features, n_features) 要比較的協方差。 |
norm | orm{“frobenius”, “spectral”}, default=”frobenius” 用于計算誤差的規范類型,可用的誤差類型: - ‘frobenius’ (default): ?- ‘spectral’: 這里的A是 (comp_cov - self.covariance_) 的誤差 |
scaling | bool, default=True 如果為True(默認),則平方誤差范數除以n_features。如果為False,則不會重新調整平方誤差范數。 |
squared | bool, default=True 是計算平方誤差范數還是誤差范數。如果為True(默認),則返回平方誤差范數。如果為False,則返回誤差范數。 |
返回值 | 說明 |
---|---|
result | floatself 和comp_cov 協方差估計量之間的均方誤差(按照Frobenius范式的含義) 。 |
fit(self, X, y=None)
擬合EllipticEnvelope模型。
參數 | 說明 |
---|---|
X | {array-like, sparse matrix} of shape (n_samples, n_features) 訓練數據 |
y | Ignored 未使用,出于API一致性目的而存在。 |
fit_predict(self, X, y=None)
對X執行擬合并返回樣本的標簽。
對于離群值返回-1,對于非離群值返回1。
參數 | 說明 |
---|---|
X | {array-like, sparse matrix, dataframe} of shape (n_samples, n_features) |
y | Ignored 未使用,出于API一致性目的而存在。 |
返回值 | 說明 |
---|---|
y | ndarray of shape (n_samples,) 1代表非離群點,-1代表離群點 |
get_params(self, deep=True)
獲取此估計器的參數。
參數 | 說明 |
---|---|
deep | bool, default=True 如果為True,將返回估算器和所包含子對象的參數。 |
返回值 | 說明 |
---|---|
params | mapping of string to any 參數名稱映射到其值。 |
get_precision(self)
獲取精確度矩陣。
返回值 | 說明 |
---|---|
precision_ | array-like of shape (n_features, n_features) 與當前協方差對象關聯的精度矩陣。 |
mahalanobis(self, X)
計算給定觀測值的平方馬氏距離。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 觀測值,用來計算馬氏距離。假設觀測數據與fit中使用的數據來自相同的分布。 |
返回值 | 說明 |
---|---|
dist | ndarray of shape (n_samples,) 觀測值的平方馬氏距離。 |
predict(self, X)
根據擬合模型預測X對應的樣本標簽(1為非離群點,-1為離群點)。
reweight_covariance(self, data)
重新加權原始最小協方差行列式估計值。
使用[RVDriessen]中描述的Rousseeuw方法(相當于在計算位置和協方差估計之前從數據集中刪除離群值)對觀測值進行加權。
參數 | 說明 |
---|---|
data | array-like of shape (n_samples, n_features) 具有p個特征和n個樣本的數據矩陣。數據集必須是用于計算原始估算值的數據集。 |
返回值 | 說明 |
---|---|
location_reweighted | ndarray of shape (n_features,) 重新加權的穩健位置估算。 |
covariance_reweighted | ndarray of shape (n_features, n_features) 重新加權的穩健協方差估計。 |
support_reweighted | ndarray of shape (n_samples,), dtype=bool 用于計算重新加權的穩健位置和協方差估計的觀測掩模。 |
參考
[RVDriessen] A Fast Algorithm for the Minimum Covariance Determinant Estimator, 1999, American Statistical Association and the American Society for Quality, TECHNOMETRICS
score(self, X, y, sample_weight=None)
返回給定測試數據和標簽上的平均準確度。
在多標簽分類中,這是子集準確性,這是一個嚴格的指標,因為你需要為每個樣本正確預測每個標簽集。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 測試樣本。 |
y | array-like of shape (n_samples,) or (n_samples, n_outputs) 樣本對應的真實標簽。 |
sample_weight | array-like of shape (n_samples,), default=None 樣本權重。 |
返回值 | 說明 |
---|---|
score | float self.predict(X)的平均準確度 |
score_samples(self, X)
計算負馬氏距離。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 數據矩陣。 |
返回值 | 說明 |
---|---|
negative_mahal_distances | array-like of shape (n_samples,) 與Mahalanobis距離相反。 |
set_params(self, **params)
設置此估計器的參數。
該方法適用于簡單的估計器以及嵌套對象(如piplines)。后者具有形式為<component>__<parameter>
的參數,這樣就讓更新嵌套對象的每個組件成為了可能。
參數 | 說明 |
---|---|
**params | dict 估計器參數。 |
返回值 | 說明 |
---|---|
self | object 估計器對象。 |