sklearn.covariance.ShrunkCovariance?

class sklearn.covariance.ShrunkCovariance(*, store_precision=True, assume_centered=False, shrinkage=0.1)

[源碼]

收縮協方差估計器。

用戶指南中閱讀更多內容。

參數 說明
store_precision bool, default=True
是否存儲估計的精度
assume_centered bool, default=False
如果為True,則在計算之前數據不會中心化。這在處理均值幾乎為零但不完全為零的數據時很有用。如果為False,則數據將在計算之前進行中心化。
shrinkage float, default=0.1
用于收縮估計的凸組合系數。范圍是[0,1]。
屬性 說明
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時存儲)

正則協方差由下式給出:

(1-收縮率)* cov +收縮率* mu * np.identity(n_features)

其中mu = trace(cov)/ n_features

示例

>>> import numpy as np
>>> from sklearn.covariance import ShrunkCovariance
>>> from sklearn.datasets import make_gaussian_quantiles
>>> real_cov = np.array([[.8.3],
...                      [.3.4]])
>>> rng = np.random.RandomState(0)
>>> X = rng.multivariate_normal(mean=[00],
...                                   cov=real_cov,
...                                   size=500)
>>> cov = ShrunkCovariance().fit(X)
>>> cov.covariance_
array([[0.7387..., 0.2536...],
       [0.2536..., 0.4110...]])
>>> cov.location_
array([0.0622..., 0.0193...])

方法

方法 說明
error_norm(self, comp_cov[, norm, scaling, …]) 計算兩個協方差估計量之間的均方誤差。
fit(self, X[, y]) 根據給定的訓練數據和參數擬合收縮協方差模型。
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, shrinkage=0.1)

[源碼]

初始化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 float
selfcomp_cov協方差估計量之間的均方誤差(按照Frobenius范式的含義) 。
fit(self,X,y = None )

[源碼]

根據給定的訓練數據和參數,擬合縮收縮協方差模型。

參數 說明
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
估計器對象。