sparse_encode#

sklearn.decomposition.sparse_encode(X, dictionary, *, gram=None, cov=None, algorithm='lasso_lars', n_nonzero_coefs=None, alpha=None, copy_cov=True, init=None, max_iter=1000, n_jobs=None, check_input=True, verbose=0, positive=False)[源代码]#

稀疏编码。

结果的每一行都是稀疏编码问题的解决方案。目标是找到稀疏数组 code 这样::

X ~= code * dictionary

阅读更多的 User Guide .

参数:
X形状类似阵列(n_samples,n_features)

数据矩阵。

dictionary形状类似阵列(n_组件,n_特征)

用于解决数据稀疏编码问题的字典矩阵。一些算法假设规范化行以获得有意义的输出。

gram形状类似数组(n_components,n_components),默认=无

预先计算的革兰矩阵, dictionary * dictionary' .

cov形状类似阵列(n_components,n_samples),默认=无

预先计算的协方差, dictionary' * X .

algorithm'lasso_lars ','lasso_cd ',' lars ',' omp ','threshold'}, default='lasso_lars'

使用的算法:

  • 'lars' :使用最小角度回归法 (linear_model.lars_path );

  • 'lasso_lars' :使用Lars计算Lasso解决方案;

  • 'lasso_cd' :使用坐标下降法计算Lasso解 (linear_model.Lasso ).如果估计的成分稀疏,lasso_lars会更快;

  • 'omp' :使用垂直匹配追求来估计稀疏解;

  • 'threshold' :将投影中小于正规化的所有系数压缩为零 dictionary * data' .

n_nonzero_coefsint,默认=无

解决方案每列中要目标的非零系数数。这仅用于 algorithm='lars'algorithm='omp' 并被覆盖 alphaomp 案子如果 None 那么 n_nonzero_coefs=int(n_features / 10) .

alphafloat,默认=无

如果 algorithm='lasso_lars'algorithm='lasso_cd' , alpha 是适用于L1规范的惩罚。如果 algorithm='threshold' , alpha 是阈值的绝对值,低于该阈值的系数将被压缩为零。如果 algorithm='omp' , alpha 是容差参数:目标重建误差的值。在这种情况下,它覆盖 n_nonzero_coefs .如果 None ,默认为1。

copy_cov布尔,默认=True

是否复制预先计算的协方差矩阵;如果 False ,它可能会被覆盖。

init形状的nd数组(n_samples,n_components),默认=无

稀疏码的指定值。仅在以下情况下使用 algorithm='lasso_cd' .

max_iterint,默认=1000

如果出现以下情况,需要执行的最大迭代次数 algorithm='lasso_cd''lasso_lars' .

n_jobsint,默认=无

要运行的并行作业数。 None 意思是1,除非在a中 joblib.parallel_backend 上下文 -1 意味着使用所有处理器。看到 Glossary 了解更多详细信息。

check_input布尔,默认=True

如果 False ,不会检查输入数组X和字典。

verboseint,默认=0

控制冗长;越高,消息越多。

positive布尔,默认=假

在查找编码时是否强制执行积极性。

Added in version 0.20.

返回:
code形状的nd数组(n_samples,n_components)

稀疏代码。

参见

sklearn.linear_model.lars_path

使用LARS算法计算最小角度回归或Lasso路径。

sklearn.linear_model.orthogonal_mp

解决垂直匹配追求问题。

sklearn.linear_model.Lasso

以L1之前作为正规化器训练线性模型。

SparseCoder

从固定的预计算字典中查找数据的稀疏表示。

示例

>>> import numpy as np
>>> from sklearn.decomposition import sparse_encode
>>> X = np.array([[-1, -1, -1], [0, 0, 3]])
>>> dictionary = np.array(
...     [[0, 1, 0],
...      [-1, -1, 2],
...      [1, 1, 1],
...      [0, 1, 1],
...      [0, 2, 1]],
...    dtype=np.float64
... )
>>> sparse_encode(X, dictionary, alpha=1e-10)
array([[ 0.,  0., -1.,  0.,  0.],
       [ 0.,  1.,  1.,  0.,  0.]])