1
- """ Non-negative matrix factorization
1
+ """ Non-negative matrix factorization.
2
2
"""
3
3
# Author: Vlad Niculae
4
4
# Lars Buitinck
25
25
26
26
27
27
def norm (x ):
28
- """Dot product-based Euclidean norm implementation
28
+ """Dot product-based Euclidean norm implementation.
29
29
30
30
See: http://fseoane.net/blog/2011/computing-the-vector-norm/
31
31
32
32
Parameters
33
33
----------
34
34
x : array-like
35
- Vector for which to compute the norm
35
+ Vector for which to compute the norm.
36
36
"""
37
37
return sqrt (squared_norm (x ))
38
38
@@ -43,9 +43,9 @@ def trace_dot(X, Y):
43
43
Parameters
44
44
----------
45
45
X : array-like
46
- First matrix
46
+ First matrix.
47
47
Y : array-like
48
- Second matrix
48
+ Second matrix.
49
49
"""
50
50
return np .dot (X .ravel (), Y .ravel ())
51
51
@@ -85,7 +85,7 @@ def _beta_divergence(X, W, H, beta, square_root=False):
85
85
Returns
86
86
-------
87
87
res : float
88
- Beta divergence of X and np.dot(X, H)
88
+ Beta divergence of X and np.dot(X, H).
89
89
"""
90
90
beta = _beta_loss_to_float (beta )
91
91
@@ -187,7 +187,7 @@ def _special_sparse_dot(W, H, X):
187
187
188
188
189
189
def _compute_regularization (alpha , l1_ratio , regularization ):
190
- """Compute L1 and L2 regularization coefficients for W and H"""
190
+ """Compute L1 and L2 regularization coefficients for W and H. """
191
191
alpha_H = 0.
192
192
alpha_W = 0.
193
193
if regularization in ('both' , 'components' ):
@@ -233,7 +233,7 @@ def _check_string_param(solver, regularization, beta_loss, init):
233
233
234
234
235
235
def _beta_loss_to_float (beta_loss ):
236
- """Convert string beta_loss to float"""
236
+ """Convert string beta_loss to float. """
237
237
allowed_beta_loss = {'frobenius' : 2 ,
238
238
'kullback-leibler' : 1 ,
239
239
'itakura-saito' : 0 }
@@ -252,7 +252,7 @@ def _initialize_nmf(X, n_components, init=None, eps=1e-6,
252
252
"""Algorithms for NMF initialization.
253
253
254
254
Computes an initial guess for the non-negative
255
- rank k matrix approximation for X: X = WH
255
+ rank k matrix approximation for X: X = WH.
256
256
257
257
Parameters
258
258
----------
@@ -288,18 +288,18 @@ def _initialize_nmf(X, n_components, init=None, eps=1e-6,
288
288
eps : float, default=1e-6
289
289
Truncate all values less then this in output to zero.
290
290
291
- random_state : int or RandomState instance, default=None
291
+ random_state : int, RandomState instance or None , default=None
292
292
Used when ``init`` == 'nndsvdar' or 'random'. Pass an int for
293
293
reproducible results across multiple function calls.
294
294
See :term:`Glossary <random_state>`.
295
295
296
296
Returns
297
297
-------
298
298
W : array-like of shape (n_samples, n_components)
299
- Initial guesses for solving X ~= WH
299
+ Initial guesses for solving X ~= WH.
300
300
301
301
H : array-like of shape (n_components, n_features)
302
- Initial guesses for solving X ~= WH
302
+ Initial guesses for solving X ~= WH.
303
303
304
304
References
305
305
----------
@@ -395,11 +395,11 @@ def _initialize_nmf(X, n_components, init=None, eps=1e-6,
395
395
396
396
def _update_coordinate_descent (X , W , Ht , l1_reg , l2_reg , shuffle ,
397
397
random_state ):
398
- """Helper function for _fit_coordinate_descent
398
+ """Helper function for _fit_coordinate_descent.
399
399
400
400
Update W to minimize the objective function, iterating once over all
401
401
coordinates. By symmetry, to update H, one can call
402
- _update_coordinate_descent(X.T, Ht, W, ...)
402
+ _update_coordinate_descent(X.T, Ht, W, ...).
403
403
404
404
"""
405
405
n_components = Ht .shape [1 ]
@@ -472,7 +472,7 @@ def _fit_coordinate_descent(X, W, H, tol=1e-4, max_iter=200, l1_reg_W=0,
472
472
shuffle : bool, default=False
473
473
If true, randomize the order of coordinates in the CD solver.
474
474
475
- random_state : int or RandomState instance, default=None
475
+ random_state : int, RandomState instance or None , default=None
476
476
Used to randomize the coordinates in the CD solver, when
477
477
``shuffle`` is set to ``True``. Pass an int for reproducible
478
478
results across multiple function calls.
@@ -532,7 +532,7 @@ def _fit_coordinate_descent(X, W, H, tol=1e-4, max_iter=200, l1_reg_W=0,
532
532
533
533
def _multiplicative_update_w (X , W , H , beta_loss , l1_reg_W , l2_reg_W , gamma ,
534
534
H_sum = None , HHt = None , XHt = None , update_H = True ):
535
- """update W in Multiplicative Update NMF"""
535
+ """Update W in Multiplicative Update NMF. """
536
536
if beta_loss == 2 :
537
537
# Numerator
538
538
if XHt is None :
@@ -626,7 +626,7 @@ def _multiplicative_update_w(X, W, H, beta_loss, l1_reg_W, l2_reg_W, gamma,
626
626
627
627
628
628
def _multiplicative_update_h (X , W , H , beta_loss , l1_reg_H , l2_reg_H , gamma ):
629
- """update H in Multiplicative Update NMF"""
629
+ """Update H in Multiplicative Update NMF. """
630
630
if beta_loss == 2 :
631
631
numerator = safe_sparse_dot (W .T , X )
632
632
denominator = np .linalg .multi_dot ([W .T , W , H ])
@@ -711,7 +711,7 @@ def _fit_multiplicative_update(X, W, H, beta_loss='frobenius',
711
711
max_iter = 200 , tol = 1e-4 ,
712
712
l1_reg_W = 0 , l1_reg_H = 0 , l2_reg_W = 0 , l2_reg_H = 0 ,
713
713
update_H = True , verbose = 0 ):
714
- """Compute Non-negative Matrix Factorization with Multiplicative Update
714
+ """Compute Non-negative Matrix Factorization with Multiplicative Update.
715
715
716
716
The objective function is _beta_divergence(X, WH) and is minimized with an
717
717
alternating minimization of W and H. Each minimization is done with a
@@ -849,7 +849,7 @@ def non_negative_factorization(X, W=None, H=None, n_components=None, *,
849
849
max_iter = 200 , alpha = 0. , l1_ratio = 0. ,
850
850
regularization = None , random_state = None ,
851
851
verbose = 0 , shuffle = False ):
852
- """Compute Non-negative Matrix Factorization (NMF)
852
+ """Compute Non-negative Matrix Factorization (NMF).
853
853
854
854
Find two non-negative matrices (W, H) whose product approximates the non-
855
855
negative matrix X. This factorization can be used for example for
@@ -970,7 +970,7 @@ def non_negative_factorization(X, W=None, H=None, n_components=None, *,
970
970
Select whether the regularization affects the components (H), the
971
971
transformation (W), both or none of them.
972
972
973
- random_state : int or RandomState instance, default=None
973
+ random_state : int, RandomState instance or None , default=None
974
974
Used for NMF initialisation (when ``init`` == 'nndsvdar' or
975
975
'random'), and in Coordinate Descent. Pass an int for reproducible
976
976
results across multiple function calls.
@@ -1086,7 +1086,7 @@ def non_negative_factorization(X, W=None, H=None, n_components=None, *,
1086
1086
1087
1087
1088
1088
class NMF (TransformerMixin , BaseEstimator ):
1089
- """Non-Negative Matrix Factorization (NMF)
1089
+ """Non-Negative Matrix Factorization (NMF).
1090
1090
1091
1091
Find two non-negative matrices (W, H) whose product approximates the non-
1092
1092
negative matrix X. This factorization can be used for example for
@@ -1175,7 +1175,7 @@ class NMF(TransformerMixin, BaseEstimator):
1175
1175
max_iter : int, default=200
1176
1176
Maximum number of iterations before timing out.
1177
1177
1178
- random_state : int or RandomState instance, default=None
1178
+ random_state : int, RandomState instance or None , default=None
1179
1179
Used for initialisation (when ``init`` == 'nndsvdar' or
1180
1180
'random'), and in Coordinate Descent. Pass an int for reproducible
1181
1181
results across multiple function calls.
@@ -1334,17 +1334,17 @@ def fit(self, X, y=None, **params):
1334
1334
return self
1335
1335
1336
1336
def transform (self , X ):
1337
- """Transform the data X according to the fitted NMF model
1337
+ """Transform the data X according to the fitted NMF model.
1338
1338
1339
1339
Parameters
1340
1340
----------
1341
1341
X : {array-like, sparse matrix} of shape (n_samples, n_features)
1342
- Data matrix to be transformed by the model
1342
+ Data matrix to be transformed by the model.
1343
1343
1344
1344
Returns
1345
1345
-------
1346
1346
W : ndarray of shape (n_samples, n_components)
1347
- Transformed data
1347
+ Transformed data.
1348
1348
"""
1349
1349
check_is_fitted (self )
1350
1350
@@ -1365,12 +1365,12 @@ def inverse_transform(self, W):
1365
1365
Parameters
1366
1366
----------
1367
1367
W : {ndarray, sparse matrix} of shape (n_samples, n_components)
1368
- Transformed data matrix
1368
+ Transformed data matrix.
1369
1369
1370
1370
Returns
1371
1371
-------
1372
1372
X : {ndarray, sparse matrix} of shape (n_samples, n_features)
1373
- Data matrix of original shape
1373
+ Data matrix of original shape.
1374
1374
1375
1375
.. versionadded:: 0.18
1376
1376
"""
0 commit comments