NMF
- class sklearn.decomposition.NMF(n_components='auto', *, init=None, solver='cd', beta_loss='frobenius', tol=0.0001, max_iter=, , , , , ,
Non-Negative Matrix Factorization (NMF).
Find two non-negative matrices, i.e. matrices with all non-negative elements, (W, H) whose product approximates the non-negative matrix X. This factorization can be used for example for dimensionality reduction, source separation or topic extraction.
The objective function is:
where (Frobenius norm) and (Elementwise L1 norm).
The generic norm may represent
the Frobenius norm or another supported beta-divergence loss.
The choice between options is controlled by the beta_loss parameter.
The regularization terms are scaled by n_features for W and by n_samples for
H to keep their impact balanced with respect to one another and to the data fit
term as independent as possible of the size n_samples of the training set.
The objective function is minimized with an alternating minimization of W and H.
Note that the transformed data is named W and the components matrix is named H. In the NMF literature, the naming convention is usually the opposite since the data matrix X is transposed.
Read more in the User Guide.
Number of components. If None, all features are kept.
If n_components='auto', the number of components is automatically inferred
from W or H shapes.
“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.
“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],
Learn a NMF model for the data X.
Training vector, where n_samples is the number of samples
and n_features is the number of features.
Learn a NMF model for the data X and returns the transformed data.
This is more efficient than calling fit followed by transform.
Training vector, where n_samples is the number of samples
and is the number of features.
Get output feature names for transformation.
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"].
Only used to validate feature names with the names seen in fit.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRequest encapsulating
routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Transform data back to its original space.
Transformed data matrix.
Set output container.
Refer to the user guide for more details and Introducing the set_output API for an example on how to use the API.
Configure output of transform and .
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as Pipeline). The latter have
parameters of the form <component>__<parameter> so that it’s
possible to update each component of a nested object.
Estimator parameters.
Transform the data X according to the fitted NMF model.
Training vector, where n_samples is the number of samples
and n_features is the number of features.
Transformed data.
Topic extraction with Non-negative Matrix Factorization and Latent Dirichlet Allocation
Selecting dimensionality reduction with Pipeline and GridSearchCV
'auto'Method used to initialize the procedure. Valid options:
None: ‘nndsvda’ if n_components <= min(n_samples, n_features),
otherwise random.
'random': non-negative random matrices, scaled with:
sqrt(X.mean() / n_components)
'nndsvd': Nonnegative Double Singular Value Decomposition (NNDSVD)
initialization (better for sparseness)
'nndsvda': NNDSVD with zeros filled with the average of X
(better when sparsity is not desired)
'nndsvdar' NNDSVD with zeros filled with small random values
(generally faster, less accurate alternative to NNDSVDa
for when sparsity is not desired)
'custom': Use custom matrices W and H which must both be provided.
Numerical solver to use:
‘cd’ is a Coordinate Descent solver.
‘mu’ is a Multiplicative Update solver.
Beta divergence to be minimized, measuring the distance between X and the dot product WH. Note that values different from ‘frobenius’ (or 2) and ‘kullback-leibler’ (or 1) lead to significantly slower fits. Note that for beta_loss <= 0 (or ‘itakura-saito’), the input matrix X cannot contain zeros. Used only in ‘mu’ solver.
Tolerance of the stopping condition.
Maximum number of iterations before timing out.
Used for initialisation (when init == ‘nndsvdar’ or
‘random’), and in Coordinate Descent. Pass an int for reproducible
results across multiple function calls.
See Glossary.
Constant that multiplies the regularization terms of W. Set it to zero
(default) to have no regularization on W.
Constant that multiplies the regularization terms of H. Set it to zero to
have no regularization on H. If “same” (default), it takes the same value as
alpha_W.
The regularization mixing parameter, with 0 <= l1_ratio <= 1. For l1_ratio = 0 the penalty is an elementwise L2 penalty (aka Frobenius Norm). For l1_ratio = 1 it is an elementwise L1 penalty. For 0 < l1_ratio < 1, the penalty is a combination of L1 and L2.
Whether to be verbose.
If true, randomize the order of coordinates in the CD solver.
Factorization matrix, sometimes called ‘dictionary’.
The number of components. It is same as the n_components parameter
if it was given. Otherwise, it will be same as the number of
features.
Frobenius norm of the matrix difference, or beta-divergence, between
the training data X and the reconstructed data WH from
the fitted model.
Actual number of iterations.
Number of features seen during fit.
n_features_in_,)Names of features seen during fit. Defined only when X
has feature names that are all strings.
SparseCoderFind a sparse representation of data from a fixed, precomputed dictionary.
Sparse Principal Components Analysis.
Dimensionality reduction using truncated SVD.
Not used, present for API consistency by convention.
Parameters (keyword arguments) and values passed to the fit_transform instance.
Returns the instance itself.
Not used, present for API consistency by convention.
If init='custom', it is used as initial guess for the solution.
If None, uses the initialisation method specified in init.
If init='custom', it is used as initial guess for the solution.
If None, uses the initialisation method specified in init.
Transformed data.
Transformed feature names.
Returns a data matrix of the original shape.
"default": Default output format of a transformer
"pandas": DataFrame output
"polars": Polars output
None: Transform configuration is unchanged
Estimator instance.
Estimator instance.