r""" Factor a data matrix into two nonnegative low rank factor matrices via fast HALS. Notes ----- This method implements Algorithm 2 from [*]_. In contrast to vanilla ALS, HALS proceeds by minimizing a *set* of local cost functions with the s
(self, X, W=None, H=None, n_initializations=10, verbose=False)
| 275 | return W |
| 276 | |
| 277 | def fit(self, X, W=None, H=None, n_initializations=10, verbose=False): |
| 278 | r""" |
| 279 | Factor a data matrix into two nonnegative low rank factor matrices via |
| 280 | fast HALS. |
| 281 | |
| 282 | Notes |
| 283 | ----- |
| 284 | This method implements Algorithm 2 from [*]_. In contrast to vanilla |
| 285 | ALS, HALS proceeds by minimizing a *set* of local cost functions with |
| 286 | the same global minima. Each cost function is defined on a "residue" of |
| 287 | the factor matrices **W** and **H**: |
| 288 | |
| 289 | .. math:: |
| 290 | |
| 291 | \mathbf{X}^{(j)} := |
| 292 | \mathbf{X} - \mathbf{WH}^\top + \mathbf{w}_j \mathbf{h}_j^\top |
| 293 | |
| 294 | where :math:`\mathbf{X}^{(j)}` is the :math:`j^{th}` residue, **X** is |
| 295 | the input data matrix, and :math:`\mathbf{w}_j` and |
| 296 | :math:`\mathbf{h}_j` are the :math:`j^{th}` columns of the current |
| 297 | factor matrices **W** and **H**. HALS proceeds by minimizing the cost |
| 298 | for each residue, first with respect to :math:`\mathbf{w}_j`, and then |
| 299 | with respect to :math:`\mathbf{h}_j`. In either case, the cost for |
| 300 | residue `j`, :math:`\mathcal{L}^{(j)}` is simply: |
| 301 | |
| 302 | .. math:: |
| 303 | |
| 304 | \mathcal{L}^{(j)} := |
| 305 | || \mathbf{X}^{(j)} - \mathbf{w}_j \mathbf{h}_j^\top || |
| 306 | |
| 307 | where :math:`||\cdot||` denotes the Frobenius norm. For NMF, |
| 308 | minimization is performed under the constraint that all elements of |
| 309 | both **W** and **H** are nonnegative. |
| 310 | |
| 311 | References |
| 312 | ---------- |
| 313 | .. [*] Cichocki, A., & Phan, A. (2009). Fast local algorithms for |
| 314 | large scale nonnegative matrix and tensor factorizations. *IEICE |
| 315 | Transactions on Fundamentals of Electronics, Communications and |
| 316 | Computer Sciences, 92(3)*, 708-721. |
| 317 | |
| 318 | Parameters |
| 319 | ---------- |
| 320 | X : numpy array of shape `(N, M)` |
| 321 | The data matrix to factor. |
| 322 | W : numpy array of shape `(N, K)` or None |
| 323 | An initial value for the `W` factor matrix. If None, initialize |
| 324 | **W** using vanilla ALS. Default is None. |
| 325 | H : numpy array of shape `(K, M)` or None |
| 326 | An initial value for the `H` factor matrix. If None, initialize |
| 327 | **H** using vanilla ALS. Default is None. |
| 328 | n_initializations : int |
| 329 | Number of re-initializations of the algorithm to perform before |
| 330 | taking the answer with the lowest reconstruction error. This value |
| 331 | is ignored and set to 1 if both `W` and `H` are not None. Default |
| 332 | is 10. |
| 333 | verbose : bool |
| 334 | Whether to print the loss at each iteration. Default is False. |