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参数控制。正则化项会分别根据
W的n_features和H的n_samples进行缩放,以保持它们在彼此之间以及与数据拟合项之间的平衡,并尽可能使它们独立于训练集规模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',分量数量会自动从W或H的形状中推断出来。版本 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的默认值在 0.23 版本中从 ‘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’ 时)以及在坐标下降中使用。传入 int 以在多次函数调用中获得可重现的结果。参见 术语表。- 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)