non_negative_factorization#

sklearn.decomposition.non_negative_factorization(X, W=None, H=None, n_components='auto', *, init=None, update_H=True, solver='cd', beta_loss='frobenius', tol=0.0001, max_iter=200, alpha_W=0.0, alpha_H='same', l1_ratio=0.0, random_state=None, verbose=0, shuffle=False)[source]#

计算非负矩阵分解 (NMF)。

寻找两个非负矩阵(W,H),其乘积近似于非负矩阵X。该分解可用于降维、源分离或主题提取等。

目标函数为

\[ \begin{align}\begin{aligned}L(W, H) &= 0.5 * ||X - WH||_{loss}^2\\ &+ alpha\_W * l1\_ratio * n\_features * ||vec(W)||_1\\ &+ alpha\_H * l1\_ratio * n\_samples * ||vec(H)||_1\\ &+ 0.5 * alpha\_W * (1 - l1\_ratio) * n\_features * ||W||_{Fro}^2\\ &+ 0.5 * alpha\_H * (1 - l1\_ratio) * n\_samples * ||H||_{Fro}^2,\end{aligned}\end{align} \]

其中 \(||A||_{Fro}^2 = \sum_{i,j} A_{ij}^2\)(Frobenius范数)和 \(||vec(A)||_1 = \sum_{i,j} abs(A_{ij})\)(元素L1范数)

通用范数 \(||X - WH||_{loss}^2\) 可以表示Frobenius范数或另一个受支持的beta散度损失。选项之间的选择由 beta_loss 参数控制。

正则化项按 n_featuresW 进行缩放,按 n_samplesH 进行缩放,以使它们彼此之间的影响保持平衡,并使其与数据拟合项尽可能独立于训练集的大小 n_samples

目标函数通过交替最小化W和H来最小化。如果H给定且update_H=False,则只求解W。

请注意,转换后的数据被命名为W,分量矩阵被命名为H。在NMF文献中,命名约定通常是相反的,因为数据矩阵X被转置了。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

常数矩阵。

Warray-like of shape (n_samples, n_components), default=None

如果 init='custom',则将其用作解的初始猜测。如果 update_H=False,则将其初始化为零数组,除非 solver='mu',则它会填充由 np.sqrt(X.mean() / self._n_components) 计算的值。如果为 None,则使用 init 中指定的初始化方法。

Harray-like of shape (n_components, n_features), default=None

如果 init='custom',则将其用作解的初始猜测。如果 update_H=False,则将其用作常数,仅求解W。如果为 None,则使用 init 中指定的初始化方法。

n_componentsint or {‘auto’} or None, default=’auto’

组件数量。如果为 None,则保留所有特征。如果 n_components='auto',则组件数量将根据 WH 的形状自动推断。

版本 1.4 中更改: 添加了 'auto' 值。

版本 1.6 中更改: 默认值从 None 更改为 'auto'

init{‘random’, ‘nndsvd’, ‘nndsvda’, ‘nndsvdar’, ‘custom’}, default=None

用于初始化过程的方法。

有效选项

  • None: 如果 n_components < n_features,则为 ‘nndsvda’,否则为 ‘random’。

  • ‘random’: 非负随机矩阵,按以下方式缩放: sqrt(X.mean() / n_components)

  • ‘nndsvd’: 非负双奇异值分解(NNDSVD)初始化(更适合稀疏性)

  • ‘nndsvda’: NNDSVD,其中零值填充为X的平均值(在不需要稀疏性时更好)

  • ‘nndsvdar’: NNDSVD,其中零值填充为小的随机值(通常更快,在不需要稀疏性时是NNDSVDa的替代方案,但精度较低)

  • ‘custom’: 如果 update_H=True,则使用必须同时提供的自定义矩阵W和H。如果 update_H=False,则仅使用自定义矩阵H。

版本 0.23 中的更改: init 的默认值从 ‘random’ 更改为 None。

版本 1.1 中的更改: init=None 且 n_components 小于 n_samples 和 n_features 时,默认为 nndsvda 而不是 nndsvd

update_Hbool, default=True

设置为True,W和H都将从初始猜测中估计。设置为False,只估计W。

solver{‘cd’, ‘mu’}, default=’cd’

使用的数值求解器

  • ‘cd’ 是一个坐标下降求解器,使用快速分层交替最小二乘法(Fast HALS)。

  • ‘mu’ 是一个乘法更新求解器。

版本 0.17 中新增: 坐标下降求解器。

版本 0.19 中新增: 乘法更新求解器。

beta_lossfloat or {‘frobenius’, ‘kullback-leibler’, ‘itakura-saito’}, default=’frobenius’

要最小化的beta散度,用于衡量X与点积WH之间的距离。请注意,不同于‘frobenius’(或2)和‘kullback-leibler’(或1)的值会导致拟合速度明显变慢。请注意,对于beta_loss <= 0(或‘itakura-saito’),输入矩阵X不能包含零。仅在‘mu’求解器中使用。

Added in version 0.19.

tolfloat, default=1e-4

停止条件的容差。

max_iter整型, 默认为 200

超时前的最大迭代次数。

alpha_Wfloat, default=0.0

乘以 W 的正则化项的常数。将其设置为零(默认值)表示不对 W 进行正则化。

1.0 版本新增。

alpha_Hfloat or “same”, default=”same”

乘以 H 的正则化项的常数。将其设置为零表示不对 H 进行正则化。如果为 “same”(默认值),则取与 alpha_W 相同的值。

1.0 版本新增。

l1_ratiofloat, default=0.0

正则化混合参数,0 <= l1_ratio <= 1。当 l1_ratio = 0 时,惩罚项是元素级的 L2 惩罚(又称 Frobenius 范数)。当 l1_ratio = 1 时,惩罚项是元素级的 L1 惩罚。当 0 < l1_ratio < 1 时,惩罚项是 L1 和 L2 的组合。

random_stateint, RandomState instance or None, default=None

用于NMF初始化(当 init == ‘nndsvdar’ 或 ‘random’)以及坐标下降。传入一个整数以在多次函数调用中获得可重现的结果。参见 Glossary

verboseint, default=0

详细程度。

shufflebool, default=False

如果为true,则在CD求解器中随机化坐标的顺序。

返回:
Wndarray of shape (n_samples, n_components)

非负最小二乘问题的解。

Hndarray of shape (n_components, n_features)

非负最小二乘问题的解。

n_iter整型

实际迭代次数。

References

[1]

“Fast local algorithms for large scale nonnegative matrix and tensor factorizations” Cichocki, Andrzej, and P. H. A. N. Anh-Huy. IEICE transactions on fundamentals of electronics, communications and computer sciences 92.3: 708-721, 2009.

[2]

“Algorithms for nonnegative matrix factorization with the beta-divergence” Fevotte, C., & Idier, J. (2011). Neural Computation, 23(9).

示例

>>> import numpy as np
>>> X = np.array([[1,1], [2, 1], [3, 1.2], [4, 1], [5, 0.8], [6, 1]])
>>> from sklearn.decomposition import non_negative_factorization
>>> W, H, n_iter = non_negative_factorization(
...     X, n_components=2, init='random', random_state=0)