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
在 用户指南 中阅读更多内容。
- 参数:
- Xshape 为 (n_samples, n_features) 的 array-like
数据矩阵。
- dictionary形状为 (n_components, n_features) 的类数组对象
用于解决数据稀疏编码的字典矩阵。某些算法假定行已标准化以获得有意义的输出。
- gram形状为 (n_components, n_components) 的类数组对象, default=None
预计算的 Gram 矩阵,
dictionary * dictionary'。- cov形状为 (n_components, n_samples) 的类数组对象, default=None
预计算的协方差,
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, default=None
目标解中每列的非零系数数量。这仅用于
algorithm='lars'和algorithm='omp',并且在omp情况下被alpha覆盖。如果为None,则n_nonzero_coefs=int(n_features / 10)。- alphafloat, default=None
如果
algorithm='lasso_lars'或algorithm='lasso_cd',alpha是应用于 L1 范数的惩罚项。如果algorithm='threshold',alpha是阈值的绝对值,低于该阈值的系数将被归零。如果algorithm='omp',alpha是容差参数:目标重建误差的值。在这种情况下,它会覆盖n_nonzero_coefs。如果为None,则默认为 1。- copy_covbool, default=True
是否复制预计算的协方差矩阵;如果为
False,它可能会被覆盖。- init形状为 (n_samples, n_components) 的 ndarray, default=None
稀疏代码的初始化值。仅在
algorithm='lasso_cd'时使用。- max_iterint, default=1000
如果
algorithm='lasso_cd'或'lasso_lars',要执行的最大迭代次数。- n_jobsint, default=None
要并行运行的作业数量。
None表示 1,除非在joblib.parallel_backend上下文中。-1表示使用所有处理器。有关更多详细信息,请参阅词汇表。- check_inputbool, default=True
如果为
False,将不会检查输入数组 X 和 dictionary。- verboseint, default=0
控制冗长程度;值越高,消息越多。
- positivebool, default=False
在寻找编码时是否强制非负性。
0.20 版本新增。
- 返回:
- codendarray of shape (n_samples, n_components)
稀疏代码。
另请参阅
sklearn.linear_model.lars_pathCompute Least Angle Regression or Lasso path using LARS algorithm.
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.]])