TruncatedSVD
- class sklearn.decomposition.TruncatedSVD(n_components=2, *, algorithm='randomized', n_iter=5, n_oversamples=10, power_iteration_normalizer='auto', random_state,
Dimensionality reduction using truncated SVD (aka LSA).
This transformer performs linear dimensionality reduction by means of truncated singular value decomposition (SVD). Contrary to PCA, this estimator does not center the data before computing the singular value decomposition. This means it can work with sparse matrices efficiently.
In particular, truncated SVD works on term count/tf-idf matrices as
returned by the vectorizers in sklearn.feature_extraction.text. In
that context, it is known as latent semantic analysis (LSA).
This estimator supports two algorithms: a fast randomized SVD solver, and
a “naive” algorithm that uses ARPACK as an eigensolver on X * X.T or
X.T * X, whichever is more efficient.
Read more in the User Guide.
Desired dimensionality of output data. If algorithm=’arpack’, must be strictly less than the number of features. If algorithm=’randomized’, must be less than or equal to the number of features. The default value is useful for visualisation. For LSA, a value of 100 is recommended.
SVD solver to use. Either “arpack” for the ARPACK wrapper in SciPy (scipy.sparse.linalg.svds), or “randomized” for the randomized algorithm due to Halko (2009).
Number of iterations for randomized SVD solver. Not used by ARPACK. The
default is larger than the default in
randomized_svd to handle sparse
matrices that may have large slowly decaying spectrum.
Number of oversamples for randomized SVD solver. Not used by ARPACK.
See randomized_svd for a complete
description.
Power iteration normalizer for randomized SVD solver.
Not used by ARPACK. See randomized_svd
for more details.
Used during randomized svd. Pass an int for reproducible results across multiple function calls. See Glossary.
Tolerance for ARPACK. 0 means machine precision. Ignored by randomized SVD solver.
The right singular vectors of the input data.
The variance of the training samples transformed by a projection to each component.
Percentage of variance explained by each of the selected components.
The singular values corresponding to each of the selected components.
The singular values are equal to the 2-norms of the n_components
variables in the lower-dimensional space.
Number of features seen during fit.
SVD suffers from a problem called “sign indeterminacy”, which means the
sign of the components_ and the output from transform depend on the
algorithm and random state. To work around this, fit instances of this
class to data once, then keep the instance around to do transformations.
>>> from sklearn.decomposition import TruncatedSVD
>>> from scipy.sparse import csr_array
>>> import numpy as np
>>> np.random.seed(0)
>>> X_dense = np.random.rand
Fit model on training data X.
Training data.
Not used, present here for API consistency by convention.
Fit model to X and perform dimensionality reduction on X.
Training data.
Not used, present here for API consistency by convention.
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 X back to its original space.
Returns an array X_original whose transform would be X.
New data.
Note that this is always a dense array.
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.
Perform dimensionality reduction on X.
New data.
Reduced version of X. This will always be a dense array.
Hashing feature transformation using Totally Random Trees
Manifold learning on handwritten digits: Locally Linear Embedding, Isomap…
n_features_in_,)Names of features seen during fit. Defined only when X
has feature names that are all strings.
KernelPCAKernel Principal component analysis.
Non-Negative Matrix Factorization.
Principal component analysis.
Returns the transformer object.
Reduced version of X. This will always be a dense array.
Transformed feature names.
"default": Default output format of a transformer
"pandas": DataFrame output
"polars": Polars output
None: Transform configuration is unchanged
Estimator instance.
Estimator instance.