MiniBatchDictionaryLearning#

class sklearn.decomposition.MiniBatchDictionaryLearning(n_components=None, *, alpha=1, max_iter=1000, fit_algorithm='lars', n_jobs=None, batch_size=256, shuffle=True, dict_init=None, transform_algorithm='omp', transform_n_nonzero_coefs=None, transform_alpha=None, verbose=False, split_sign=False, random_state=None, positive_code=False, positive_dict=False, transform_max_iter=1000, callback=None, tol=0.001, max_no_improvement=10)[source]#

Mini-batch 字典学习。

查找一个字典(一组原子),该字典在稀疏编码拟合数据方面表现良好。

解决优化问题

(U^*,V^*) = argmin 0.5 || X - U V ||_Fro^2 + alpha * || U ||_1,1
             (U,V)
             with || V_k ||_2 <= 1 for all  0 <= k < n_components

||.||_Fro 代表 Frobenius 范数,||.||_1,1 代表按元素计算的矩阵范数,它是矩阵中所有元素的绝对值之和。

用户指南中了解更多信息。

参数:
n_componentsint, default=None

要提取的字典元素的数量。

alphafloat, default=1

稀疏性控制参数。

max_iterint, default=1_000

Maximum number of iterations over the complete dataset before stopping independently of any early stopping criterion heuristics.

版本 1.1 中新增。

fit_algorithm{‘lars’, ‘cd’}, default=’lars’

使用的算法

  • 'lars': 使用最小角回归方法解决 lasso 问题(linear_model.lars_path

  • 'cd': 使用坐标下降方法计算 Lasso 解(linear_model.Lasso)。如果估计的组件是稀疏的,Lars 会更快。

n_jobsint, default=None

要并行运行的作业数量。None 表示 1,除非在 joblib.parallel_backend 上下文中。-1 表示使用所有处理器。有关更多详细信息,请参阅词汇表

batch_sizeint, default=256

每个 mini-batch 中的样本数量。

版本 1.3 中已更改: batch_size 的默认值从版本 1.3 中的 3 更改为 256。

shufflebool, default=True

在形成批次之前是否打乱样本。

dict_initndarray of shape (n_components, n_features), default=None

字典的初始值,用于热启动场景。

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': 使用正交匹配追踪来估计稀疏解。

  • 'threshold': 将投影 dictionary * X' 中小于 alpha 的所有系数压缩为零。

transform_n_nonzero_coefsint, default=None

解中每列要定位的非零系数数量。这仅用于 algorithm='lars'algorithm='omp'。如果为 None,则 transform_n_nonzero_coefs=int(n_features / 10)

transform_alphafloat, default=None

如果 algorithm='lasso_lars'algorithm='lasso_cd'alpha 是应用于 L1 范数的惩罚项。如果 algorithm='threshold'alpha 是阈值的绝对值,低于该阈值的系数将被压缩为零。如果为 None,则默认为 alpha

版本 1.2 中已更改: 当为 None 时,默认值从 1.0 更改为 alpha

verbosebool or int, default=False

控制过程的详细程度。

split_signbool, default=False

是否将稀疏特征向量拆分为其负部分和正部分的连接。这可以提高下游分类器的性能。

random_stateint, RandomState instance or None, default=None

用于在未指定 dict_init 时初始化字典,在 shuffle 设置为 True 时随机打乱数据,以及更新字典。传入一个整数可以在多次函数调用中获得可重现的结果。参阅词汇表

positive_codebool, default=False

查找代码时是否强制执行非负性。

0.20 版本新增。

positive_dictbool, default=False

查找字典时是否强制执行非负性。

0.20 版本新增。

transform_max_iterint, default=1000

如果 algorithm='lasso_cd''lasso_lars',要执行的最大迭代次数。

版本 0.22 新增。

callbackcallable, default=None

在每次迭代结束时调用的可调用对象。

版本 1.1 中新增。

tolfloat, default=1e-3

根据两次迭代之间字典差异的范数控制提前停止。

要禁用基于字典更改的提前停止,请将 tol 设置为 0.0。

版本 1.1 中新增。

max_no_improvementint, default=10

根据连续的 mini batch 数量控制提前停止,这些 mini batch 对平滑成本函数没有改进。

要禁用基于成本函数的收敛检测,请将 max_no_improvement 设置为 None。

版本 1.1 中新增。

属性:
components_ndarray of shape (n_components, n_features)

从数据中提取的组件。

n_features_in_int

拟合 期间看到的特征数。

0.24 版本新增。

feature_names_in_shape 为 (n_features_in_,) 的 ndarray

fit 期间看到的特征名称。仅当 X 具有全部为字符串的特征名称时才定义。

1.0 版本新增。

n_iter_int

在完整数据集上的迭代次数。

n_steps_int

处理的 mini-batch 数量。

版本 1.1 中新增。

另请参阅

DictionaryLearning

查找一个稀疏编码数据的字典。

MiniBatchSparsePCA

Mini-batch 稀疏主成分分析。

SparseCoder

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

SparsePCA

稀疏主成分分析。

References

J. Mairal, F. Bach, J. Ponce, G. Sapiro, 2009: Online dictionary learning for sparse coding (https://www.di.ens.fr/~fbach/mairal_icml09.pdf)

示例

>>> import numpy as np
>>> from sklearn.datasets import make_sparse_coded_signal
>>> from sklearn.decomposition import MiniBatchDictionaryLearning
>>> X, dictionary, code = make_sparse_coded_signal(
...     n_samples=30, n_components=15, n_features=20, n_nonzero_coefs=10,
...     random_state=42)
>>> dict_learner = MiniBatchDictionaryLearning(
...     n_components=15, batch_size=3, transform_algorithm='lasso_lars',
...     transform_alpha=0.1, max_iter=20, random_state=42)
>>> X_transformed = dict_learner.fit_transform(X)

我们可以检查 X_transformed 的稀疏度水平

>>> np.mean(X_transformed == 0) > 0.5
np.True_

我们可以比较稀疏编码信号的重构误差的平均平方欧几里得范数与原始信号的平方欧几里得范数

>>> X_hat = X_transformed @ dict_learner.components_
>>> np.mean(np.sum((X_hat - X) ** 2, axis=1) / np.sum(X ** 2, axis=1))
np.float64(0.052)

有关更详细的示例,请参阅 使用字典学习的图像去噪

fit(X, y=None)[source]#

根据 X 中的数据拟合模型。

参数:
Xshape 为 (n_samples, n_features) 的 array-like

训练向量,其中 n_samples 是样本数,n_features 是特征数。

y被忽略

未使用,按照惯例为保持 API 一致性而存在。

返回:
selfobject

返回实例本身。

fit_transform(X, y=None, **fit_params)[source]#

拟合数据,然后对其进行转换。

使用可选参数 fit_params 将转换器拟合到 Xy,并返回 X 的转换版本。

参数:
Xshape 为 (n_samples, n_features) 的 array-like

输入样本。

y形状为 (n_samples,) 或 (n_samples, n_outputs) 的类数组对象,默认=None

目标值(对于无监督转换,为 None)。

**fit_paramsdict

额外的拟合参数。仅当估计器在其 fit 方法中接受额外的参数时才传递。

返回:
X_newndarray array of shape (n_samples, n_features_new)

转换后的数组。

get_feature_names_out(input_features=None)[source]#

获取转换的输出特征名称。

The feature names out will prefixed by the lowercased class name. For example, if the transformer outputs 3 features, then the feature names out are: ["class_name0", "class_name1", "class_name2"].

参数:
input_featuresarray-like of str or None, default=None

Only used to validate feature names with the names seen in fit.

返回:
feature_names_outstr 对象的 ndarray

转换后的特征名称。

get_metadata_routing()[source]#

获取此对象的元数据路由。

请查阅 用户指南,了解路由机制如何工作。

返回:
routingMetadataRequest

封装路由信息的 MetadataRequest

get_params(deep=True)[source]#

获取此估计器的参数。

参数:
deepbool, default=True

如果为 True,将返回此估计器以及包含的子对象(如果它们是估计器)的参数。

返回:
paramsdict

参数名称映射到其值。

inverse_transform(X)[source]#

将数据转换回其原始空间。

参数:
Xarray-like of shape (n_samples, n_components)

要转换回的数据。必须具有与用于训练模型的数据相同的组件数。

返回:
X_original形状为 (n_samples, n_features) 的 ndarray

转换后的数据。

partial_fit(X, y=None)[source]#

使用 X 中的数据作为 mini-batch 更新模型。

参数:
Xshape 为 (n_samples, n_features) 的 array-like

训练向量,其中 n_samples 是样本数,n_features 是特征数。

y被忽略

未使用,按照惯例为保持 API 一致性而存在。

返回:
selfobject

返回实例本身。

set_output(*, transform=None)[source]#

设置输出容器。

有关如何使用 API 的示例,请参阅引入 set_output API

参数:
transform{“default”, “pandas”, “polars”}, default=None

配置 transformfit_transform 的输出。

  • "default": 转换器的默认输出格式

  • "pandas": DataFrame 输出

  • "polars": Polars 输出

  • None: 转换配置保持不变

1.4 版本新增: 添加了 "polars" 选项。

返回:
selfestimator instance

估计器实例。

set_params(**params)[source]#

设置此估计器的参数。

此方法适用于简单的估计器以及嵌套对象(如 Pipeline)。后者具有 <component>__<parameter> 形式的参数,以便可以更新嵌套对象的每个组件。

参数:
**paramsdict

估计器参数。

返回:
selfestimator instance

估计器实例。

transform(X)[source]#

将数据编码为字典原子的稀疏组合。

编码方法由对象参数 transform_algorithm 确定。

参数:
Xndarray of shape (n_samples, n_features)

要转换的测试数据,必须具有与用于训练模型的数据相同的特征数。

返回:
X_newndarray of shape (n_samples, n_components)

转换后的数据。