sklearn.decomposition.DictionaryLearning?
class sklearn.decomposition.DictionaryLearning(n_components=None, *, alpha=1, max_iter=1000, tol=1e-08, fit_algorithm='lars', transform_algorithm='omp', transform_n_nonzero_coefs=None, transform_alpha=None, n_jobs=None, code_init=None, dict_init=None, verbose=False, split_sign=False, random_state=None, positive_code=False, positive_dict=False, transform_max_iter=1000)
字典學習
找到能用稀疏代碼表示數據的字典(a set of atoms)。
解決優化問題:
(U^*,V^*) = argmin 0.5 || Y - U V ||_2^2 + alpha * || U ||_1
(U,V)
with || V_k ||_2 = 1 for all 0 <= k < n_components
在用戶指南中閱讀更多內容
參數 | 說明 |
---|---|
n_components | int, default=n_features 提取的字典元素數量 |
alpha | float, default=1.0 稀疏控制參數 |
max_iter | int, default=1000 要執行的最大迭代次數 |
tol | float, default=1e-8 數值誤差容限 |
fit_algorithm | {‘lars’, ‘cd’}, default=’lars’ lars:使用最小角度回歸方法解決lasso問題(linear_model.lars_path) cd:使用坐標下降法計算Lasso解(linear_model.Lasso)。 如果估計的樣本是稀疏的,Lars會更快。 新版本0.17: 采用cd坐標下降方法,以提高速度。 |
transform_algorithm | {‘lasso_lars’, ‘lasso_cd’, ‘lars’, ‘omp’, ‘threshold’}, default=’omp’ 數據變換算法lars:使用最小角度回歸法(linear_model.lars_path) 。lasso_lars:使用lars計算Lasso解。lasso_cd:使用坐標下降法計算Lasso解(linear_model.Lasso)。如果估計的樣本是稀疏的,lasso_lars會更快。omp:使用正交匹配追蹤估計稀疏解閾值:將投影字典中所有小于alpha的系數都壓縮為 dictionary * X' 新的版本0.17:lasso_cd坐標下降方法,以提高速度。 |
transform_n_nonzero_coefs | int, default=0.1*n_features 在解的每一列中目標的非零系數的數目。這只用于 algorithm='lars' 和 algorithm='omp' ,并且在 omp 情況下被alpha 覆蓋。 |
transform_alpha | float, default=1.0 如果 algorithm='lasso_lars' 或 algorithm='lasso_cd' , 則alpha 是對L1范數的懲罰。如果algorithm='threshold' , alpha 是閾值的絕對值,低于這個閾值,系數將被壓縮為零。若 algorithm='omp' ,則alpha 為容差參數:目標重構誤差的值。在本例中,它覆蓋n_nonzero_coefs 。 |
n_jobs | int or None, default=None 要運行的并行計算數量。 None 就是1,除非在joblib.parallel_backend 上下文。-1表示使用所有處理器。更多細節請參見Glossary。 |
code_init | array of shape (n_samples, n_components), default=None 代碼的初始值,用于warm restart |
dict_init | array of shape (n_components, n_features), default=None 字典的初始值,用于warm restart |
verbose | bool, default=False 控制程序的冗長。 |
split_sign | bool, default=False 是否將稀疏特征向量分割為其負部分和正部分的連接。這可以提高下游分類器的性能。 |
random_state | int, RandomState instance or None, optional (default=None) 用于在沒有指定 dict_init 時初始化字典,在shuffle 被設置為True 時隨機變換數據,以及更新字典。在多個函數調用中傳遞可重復的結果。詳見Glossary。 |
positive_code | bool, default=False 在尋找代碼時是否加強積極性。 新版本0.20。 |
positive_dict | bool, default=False 在尋找字典時是否加強積極性。 新版本0.20。 |
transform_max_iter | int, default=1000 如果 algorithm='lasso_lars' 或 algorithm='lasso_cd' ,,則執行最大迭代次數。新版本0.22。 |
屬性 | 說明 |
---|---|
components_array | [n_components, n_features] |
error_ | array |
n_iter_ | int |
另見:
筆記
參考資料
J. Mairal, F. Bach, J. Ponce, G. Sapiro, 2009: Online dictionary learning for sparse coding (https://www.di.ens.fr/sierra/pdfs/icml09.pdf)
方法
方法 | 說明 |
---|---|
fit (self, X[, y]) |
根據X中的數據擬合模型。 |
fit_transform (self, X[, y]) |
擬合數據,然后轉換它。 |
get_params (self[, deep]) |
獲取這個估計器的參數。 |
set_params (self, **params) |
設置這個估計器的參數。 |
transform (self, X) |
將數據編碼為字典原子的稀疏組合。 |
__init__(self, n_components=None, *, alpha=1, max_iter=1000, tol=1e-08, fit_algorithm='lars', transform_algorithm='omp', transform_n_nonzero_coefs=None, transform_alpha=None, n_jobs=None, code_init=None, dict_init=None, verbose=False, split_sign=False, random_state=None, positive_code=False, positive_dict=False, transform_max_iter=1000)
初始化self.請參閱help(type(self))以獲得準確的說明 。
fit(self, X, y=None)
根據X中的數據擬合模型。
參數 | 說明 |
---|---|
X | array-like, shape (n_samples, n_features) 訓練向量,其中樣本數量中的n_samples和n_features為feature的數量。 |
y | Ignored |
返回值 | 說明 |
---|---|
self | object 返回object自身 |
fit_transform(self, X, y=None, fit_params)
擬合數據,然后轉換它。
使用可選參數fit_params
將transformer
與X
和y
匹配,并返回X
的轉換版本。
參數 | 說明 |
---|---|
X | {array-like, sparse matrix, dataframe} of shape (n_samples, n_features) |
Y | ndarray of shape (n_samples,), default=None 目標值 |
fit_params | dict 其他擬合參數。 |
返回值 | 說明 |
---|---|
X_new | ndarray array of shape (n_samples, n_features_new) 轉換過的數組 |
get_params(self, deep=True)
獲取這個估計器的參數。
參數 | 說明 |
---|---|
deep | bool, default=True 如果為真,將返回此估計器的參數以及包含的作為估計器的子對象。 |
返回值 | 說明 |
---|---|
params | mapping of string to any 參數名稱映射到它們的值。 |
set_params(self, **params)
設置這個估計器的參數。
該方法適用于簡單估計器和嵌套對象(如管道)。后者具有形式為
參數 | 說明 |
---|---|
params | dict 估計參數 |
返回值 | 說明 |
---|---|
self | object 估計距離 |
transform(self, X)
將數據編碼為字典原子的稀疏組合。
編碼方法由對象參數transform_algorithm
決定。
參數 | 說明 |
---|---|
X | array of shape (n_samples, n_features) 要轉換的測試數據,必須具有與用于訓練模型的數據相同數量的特征。 |
返回值 | 說明 |
---|---|
X | array, shape (n_samples, n_components) 轉換過的數據 |