sklearn.covariance.EmpiricalCovariance?

class sklearn.covariance.EmpiricalCovariance(*,store_precision=True, assume_centered=False)

[源碼]

最大似然協方差估計量。

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

參數 說明
store_precision bool, default=True
是否存儲估計的精度。
assume_centered bool, default=False
如果為True,則計算之前數據不會中心化。這在處理均值幾乎為零但不完全為零的數據時很有用。如果為False(默認),則在計算之前將數據中心化。
屬性 說明
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時存儲)

示例

>>> import numpy as np
>>> from sklearn.covariance import EmpiricalCovariance
>>> 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 = EmpiricalCovariance().fit(X)
>>> cov.covariance_
array([[0.7569..., 0.2818...],
       [0.2818..., 0.3928...]])
>>> 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)

[源碼]

初始化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 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 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
估算器實例。