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)[source]#
稀疏编码。
结果的每一行都是稀疏编码问题的解。目标是找到一个稀疏数组
code
,使得X ~= code * dictionary
更多信息请参阅用户指南。
- 参数:
- X形状为 (n_samples, n_features) 的类数组
数据矩阵。
- dictionary形状为 (n_components, n_features) 的类数组
用于解决数据稀疏编码问题的字典矩阵。某些算法假定行已标准化以获得有意义的输出。
- gram形状为 (n_components, n_components) 的类数组,默认为 None
预计算的格拉姆矩阵,
dictionary * dictionary'
。- cov形状为 (n_components, n_samples) 的类数组,默认为 None
预计算的协方差,
dictionary' * X
。- algorithm{‘lasso_lars’, ‘lasso_cd’, ‘lars’, ‘omp’, ‘threshold’},默认为 ‘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_coefs整型,默认为 None
解的每一列中目标非零系数的数量。这仅用于
algorithm='lars'
和algorithm='omp'
,在omp
的情况下会被alpha
覆盖。如果为None
,则n_nonzero_coefs=int(n_features / 10)
。- alpha浮点型,默认为 None
如果
algorithm='lasso_lars'
或algorithm='lasso_cd'
,alpha
是应用于 L1 范数的惩罚项。如果algorithm='threshold'
,alpha
是阈值的绝对值,低于该阈值的系数将被归零。如果algorithm='omp'
,alpha
是容差参数:目标重构误差的值。在这种情况下,它会覆盖n_nonzero_coefs
。如果为None
,则默认为 1。- copy_cov布尔型,默认为 True
是否复制预计算的协方差矩阵;如果为
False
,它可能会被覆盖。- init形状为 (n_samples, n_components) 的 ndarray,默认为 None
稀疏编码的初始化值。仅在
algorithm='lasso_cd'
时使用。- max_iter整型,默认为 1000
如果
algorithm='lasso_cd'
或'lasso_lars'
,要执行的最大迭代次数。- n_jobs整型,默认为 None
要运行的并行作业数。
None
表示 1,除非在joblib.parallel_backend
上下文中。-1
表示使用所有处理器。更多详细信息请参阅词汇表。- check_input布尔型,默认为 True
如果为
False
,则不会检查输入数组 X 和 dictionary。- verbose整型,默认为 0
控制详细程度;值越高,消息越多。
- positive布尔型,默认为 False
在寻找编码时是否强制为正。
0.20 版本新增。
- 返回:
- code形状为 (n_samples, n_components) 的 ndarray
稀疏编码。
另请参阅
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.]])