sklearn.covariance.LedoitWolf?
class sklearn.covariance.LedoitWolf(*, store_precision=True, assume_centered=False, block_size=1000)
Ledoit-Wolf估計器
Ledoit-Wolf是一種特殊的收斂形式,其收縮系數是使用O. Ledoit和M. Wolf公式計算得出的,該公式在“A Well-Conditioned Estimator for Large-Dimensional Covariance Matrices”中進行了描述,作者是Ledoit和Wolf,《多元分析雜志》,第88卷,第2期,2004年2月,第365-411頁。
在用戶指南中閱讀更多內容。
| 參數 | 說明 |
|---|---|
| store_precision | bool, default=True 指定是否存儲估計的精度。 |
| assume_centered | bool, default=False 如果為True,則在計算之前數據不會中心化。這在處理均值幾乎為零但不完全為零的數據時很有用。如果為False(默認),則數據將在計算之前進行中心化。 |
| block_size | int, default=1000 在Ledoit-Wolf估計期間將協方差矩陣進行分塊。這純粹是內存優化,不會影響結果。 |
| 屬性 | 說明 |
|---|---|
| covariance_ | ndarray of shape (n_features, n_features) 估計的協方差矩陣。 |
| location_ | ndarray of shape (n_features,) 估計位置,即估計平均值。 |
| precision_ | ndarray of shape (n_features, n_features) 估計的偽逆矩陣。(僅在store_precision為True時存儲) |
| shrinkage_ | float 用于收縮估計中的凸組合系數。范圍是[0,1]。 |
注
正則協方差為:
(1 - shrinkage) * cov + shrinkage * mu * np.identity(n_features)
其中mu = trace(cov)/ n_features,收斂率由Ledoit和Wolf公式給出(請參閱參考資料)
參考資料
“A Well-Conditioned Estimator for Large-Dimensional Covariance Matrices”, Ledoit and Wolf, Journal of Multivariate Analysis, Volume 88, Issue 2, February 2004, pages 365-411.
示例
>>> import numpy as np
>>> from sklearn.covariance import LedoitWolf
>>> real_cov = np.array([[.4, .2],
... [.2, .8]])
>>> np.random.seed(0)
>>> X = np.random.multivariate_normal(mean=[0, 0],
... cov=real_cov,
... size=50)
>>> cov = LedoitWolf().fit(X)
>>> cov.covariance_
array([[0.4406..., 0.1616...],
[0.1616..., 0.8022...]])
>>> cov.location_
array([ 0.0595... , -0.0075...])
方法
| 方法 | 說明 |
|---|---|
error_norm(self, comp_cov[, norm, scaling, …]) |
計算兩個協方差估計量之間的均方誤差。 |
fit(self, X[, y]) |
根據給定的訓練數據和參數,擬合Ledoit-Wolf收斂協方差模型。 |
get_params(self[, deep]) |
獲取此估算器的參數。 |
get_precision(self) |
獲取精確度矩陣 |
mahalanobis(self, X) |
計算給定觀測值的平方馬氏距離。 |
score(self, X_test[, y]) |
使用self.covariance_計算高斯數據集的對數似然值,作為其協方差矩陣的估計量。 |
set_params(self, **params) |
設置此估算器的參數。 |
__init__(self, *, store_precision=True, assume_centered=False, block_size=1000)
初始化self. 請參閱help(type(self))以獲得準確的說明。
error_norm(self, comp_cov, norm='frobenius', scaling=True, squared=True)
[源碼]
計算兩個協方差估計量之間的均方誤差。(在Frobenius規范的意義上)。
| 參數 | 說明 |
|---|---|
| comp_cov | array-like of shape (n_features, n_features) 要比較的協方差。 |
| norm | {“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 )
根據給定的訓練數據和參數,擬合Ledoit-Wolf收斂協方差模型。
| 參數 | 說明 |
|---|---|
| X | array-like of shape (n_samples, n_features) 訓練數據,其中 n_samples是樣本數量,n_features是特征數量。 |
| y | Ignored 未使用,出于API一致性目的而存在。 |
| 返回值 | 說明 |
|---|---|
| self | object |
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,) 觀測值的平方馬氏距離。 |
score(self, X_test, y=None)
使用self.covariance_作為協方差矩陣的估計值來計算高斯數據集的對數似然 。
| 參數 | 說明 |
|---|---|
| X_test | array-like of shape (n_samples, n_features) 計算似然性的測試數據集,其中n_samples是樣本數,n_features是特征數。假定X_test與擬合(包括中心化)使用的數據來自相同的分布。 |
| y | Ignored 未使用,出于API一致性目的而存在。 |
| 返回值 | 說明 |
|---|---|
| res | float 數據集以 self.covariance_作為其協方差矩陣的估計量的似然性。 |
set_params(self, **params)
設置此估計器的參數。
該方法適用于簡單的估計器以及嵌套對象(例如管道)。后者具有形式參數<component>__<parameter>以便可以更新嵌套對象的每個組件。
| 參數 | 說明 |
|---|---|
| **params | dict 估算器參數。 |
| 返回值 | 參數 |
|---|---|
| self | object 估算器對象。 |
