sklearn.covariance.GraphicalLasso?
class sklearn.covariance.GraphicalLasso(alpha=0.01, *, mode='cd', tol=0.0001, enet_tol=0.0001, max_iter=100, verbose=False, assume_centered=False)
使用l1-懲罰估計量的稀疏逆協方差估計。
在用戶指南中閱讀更多內容。
v0.20版中已更改: GraphLasso已重命名為GraphicalLasso
| 參數 | 說明 |
|---|---|
| alpha | float, default=0.01 正則化參數:alpha值越大,正則化程度越高,逆協方差矩陣越稀疏。范圍是(0,inf]。 |
| mode | {‘cd’, ‘lars’}, default=’cd’ 設置lasso求解器:坐標下降(cd)或LARS。LARS用于特征數量大于樣本數量的非常稀疏的情況。在數值更穩定的情況首先cd。 |
| tol | float, default=1e-4 聲明收斂的容差:如果兩次迭代結果的差值低于此值,則停止迭代。范圍是(0,inf]。 |
| enet_tol | float, default=1e-4 用于計算下降方向的彈性網求解器的容差。該參數控制給定列更新搜索方向的準確性,而不是整體參數估計的準確性。僅用于mode ='cd'。范圍是(0,inf]。 |
| max_iter | int, default=100 最大迭代次數。 |
| verbose | bool, default=False 如果設置為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) 估計的偽逆矩陣。 |
| n_iter_ | int 運行的迭代次數。 |
另見
示例
>>> import numpy as np
>>> from sklearn.covariance import GraphicalLasso
>>> true_cov = np.array([[0.8, 0.0, 0.2, 0.0],
... [0.0, 0.4, 0.0, 0.0],
... [0.2, 0.0, 0.3, 0.1],
... [0.0, 0.0, 0.1, 0.7]])
>>> np.random.seed(0)
>>> X = np.random.multivariate_normal(mean=[0, 0, 0, 0],
... cov=true_cov,
... size=200)
>>> cov = GraphicalLasso().fit(X)
>>> np.around(cov.covariance_, decimals=3)
array([[0.816, 0.049, 0.218, 0.019],
[0.049, 0.364, 0.017, 0.034],
[0.218, 0.017, 0.322, 0.093],
[0.019, 0.034, 0.093, 0.69 ]])
>>> np.around(cov.location_, decimals=3)
array([0.073, 0.04 , 0.038, 0.143])
方法
| 方法 | 說明 |
|---|---|
error_norm(self, comp_cov[, norm, scaling, …]) |
計算兩個協方差估計量之間的均方誤差。 |
fit(self, X[, y]) |
使GraphicalLasso模型擬合X。 |
get_params(self[, deep]) |
獲取此估計器的參數。 |
get_precision(self) |
獲取精確矩陣。 |
mahalanobis(self, X) |
計算給定觀測值的平方馬氏距離。 |
score(self, X_test[, y]) |
使用self.covariance_計算高斯數據集的對數似然值,作為其協方差矩陣的估計量。 |
set_params(self, **params) |
設置此估計器的參數。 |
__init__(self, alpha=0.01, *, mode='cd', tol=0.0001, enet_tol=0.0001, max_iter=100, verbose=False, 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 | {“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 )
擬合GraphicalLasso模型。
| 參數 | 說明 |
|---|---|
| X | array-like of shape (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 估計器對象。 |
