TruncatedSVD#
- class cuml.decomposition.TruncatedSVD(
- *,
- algorithm='full',
- n_components=1,
- n_iter=15,
- random_state=None,
- tol=1e-07,
- verbose=False,
- output_type=None,
TruncatedSVD is used to compute the top K singular values and vectors of a large matrix X. It is much faster when n_components is small, such as in the use of PCA when 3 components is used for 3D visualization.
cuML’s TruncatedSVD an array-like object or cuDF DataFrame, and provides 2 algorithms Full and Jacobi. Full (default) uses a full eigendecomposition then selects the top K singular vectors. The Jacobi algorithm is much faster as it iteratively tries to correct the top K singular vectors, but might be less accurate.
- Parameters:
- algorithm‘full’ or ‘jacobi’ or ‘auto’ (default = ‘full’)
Full uses a eigendecomposition of the covariance matrix then discards components. Jacobi is much faster as it iteratively corrects, but is less accurate.
- n_componentsint (default = 1)
The number of top K singular vectors / values you want. Must be <= number(columns).
- n_iterint (default = 15)
Used in Jacobi solver. The more iterations, the more accurate, but slower.
- random_stateint / None (default = None)
If you want results to be the same when you restart Python, select a state.
- tolfloat (default = 1e-7)
Used if algorithm = “jacobi”. Smaller tolerance can increase accuracy, but but will slow down the algorithm’s convergence.
- verboseint or boolean, default=False
Sets logging level. It must be one of
cuml.common.logger.level_*. See Verbosity Levels for more info.- output_type{None, ‘input’, ‘cupy’, ‘numpy’, ‘cudf’, ‘pandas’}, default=None
Return results and set estimator attributes to the indicated output type. If None, the output type set at the module level (
cuml.global_settings.output_type) will be used. See Output Data Type Configuration for more info.
- Attributes:
- components_array
The top K components (VT.T[:,:n_components]) in U, S, VT = svd(X)
- explained_variance_array
How much each component explains the variance in the data given by S**2
- explained_variance_ratio_array
How much in % the variance is explained given by S**2/sum(S**2)
- singular_values_array
The top K singular values. Remember all singular values >= 0
Methods
fit(self, X[, y])Fit model on training cudf DataFrame X.
fit_transform(self, X[, y])Fit model to X and perform dimensionality reduction on X.
inverse_transform(self, X)Transform X back to its original space.
transform(self, X)Perform dimensionality reduction on X.
Notes
TruncatedSVD (the randomized version [Jacobi]) is fantastic when the number of components you want is much smaller than the number of features. The approximation to the largest singular values and vectors is very robust, however, this method loses a lot of accuracy when you want many, many components.
Applications of TruncatedSVD
TruncatedSVD is also known as Latent Semantic Indexing (LSI) which tries to find topics of a word count matrix. If X previously was centered with mean removal, TruncatedSVD is the same as TruncatedPCA. TruncatedSVD is also used in information retrieval tasks, recommendation systems and data compression.
For additional documentation, see scikitlearn’s TruncatedSVD docs.
Examples
>>> # Both import methods supported >>> from cuml import TruncatedSVD >>> from cuml.decomposition import TruncatedSVD >>> import cudf >>> import cupy as cp >>> gdf_float = cudf.DataFrame() >>> gdf_float['0'] = cp.asarray([1.0,2.0,5.0], dtype=cp.float32) >>> gdf_float['1'] = cp.asarray([4.0,2.0,1.0], dtype=cp.float32) >>> gdf_float['2'] = cp.asarray([4.0,2.0,1.0], dtype=cp.float32) >>> tsvd_float = TruncatedSVD(n_components = 2, algorithm = "jacobi", ... n_iter = 20, tol = 1e-9) >>> tsvd_float.fit(gdf_float) TruncatedSVD(algorithm='jacobi', n_components=2, n_iter=20, tol=1e-09) >>> print(f'components: {tsvd_float.components_}') components: 0 1 2 0 0.587259 0.572331 0.572331 1 0.809399 -0.415255 -0.415255 >>> exp_var = tsvd_float.explained_variance_ >>> print(f'explained variance: {exp_var}') explained variance: 0 0.494... 1 5.505... dtype: float32 >>> exp_var_ratio = tsvd_float.explained_variance_ratio_ >>> print(f'explained variance ratio: {exp_var_ratio}') explained variance ratio: 0 0.082... 1 0.917... dtype: float32 >>> sing_values = tsvd_float.singular_values_ >>> print(f'singular values: {sing_values}') singular values: 0 7.439... 1 4.081... dtype: float32 >>> trans_gdf_float = tsvd_float.transform(gdf_float) >>> print(f'Transformed matrix: {trans_gdf_float}') Transformed matrix: 0 1 0 5.165910 -2.512643 1 3.463844 -0.042223 2 4.080960 3.216484 >>> input_gdf_float = tsvd_float.inverse_transform(trans_gdf_float) >>> print(f'Input matrix: {input_gdf_float}') Input matrix: 0 1 2 0 1.0 4.0 4.0 1 2.0 2.0 2.0 2 5.0 1.0 1.0
- as_sklearn()[source]#
Convert this estimator into an equivalent scikit-learn (or scikit-learn extension) estimator.
- Returns:
- sklearn.base.BaseEstimator
A scikit-learn compatible estimator instance that mirrors the trained state of the current estimator.
- fit(self, X, y=None) 'TruncatedSVD'[source]#
Fit model on training cudf DataFrame X. y is currently ignored.
- Parameters:
- Xarray-like (device or host) shape = (n_samples, n_features)
Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.
- yarray-like (device or host) shape = (n_samples, 1)
Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.
- fit_transform(self, X, y=None)[source]#
Fit model to X and perform dimensionality reduction on X. y is currently ignored.
- Parameters:
- Xarray-like (device or host) shape = (n_samples, n_features)
Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.
- yarray-like (device or host) shape = (n_samples, 1)
Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.
- Returns:
- transcuDF, CuPy or NumPy object depending on cuML’s output type configuration, shape = (n_samples, n_components)
Reduced version of X
For more information on how to configure cuML’s output type, refer to: Output Data Type Configuration.
- classmethod from_sklearn(model)[source]#
Create a cuml estimator from a scikit-learn estimator.
- Parameters:
- modelsklearn.base.BaseEstimator
A compatible scikit-learn (or scikit-learn extension) estimator.
- Returns:
- cls
A new instance of this cuml estimator class that mirrors the state of the input estimator.
Notes
output_typeof the estimator is set to “numpy” by default, as these cannot be inferred from training arguments. If something different is required, then please use cuml’s output_type configuration utilities.
- get_feature_names_out(input_features=None)[source]#
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"].- Parameters:
- input_featuresarray-like of str or None, default=None
Only used to validate feature names with the names seen in
fit.
- Returns:
- feature_names_outndarray of str objects
Transformed feature names.
- get_params(deep=True)[source]#
Returns a dict of all params owned by this class. If the child class has appropriately overridden the
_get_param_namesmethod and does not need anything other than what is there in this method, then it doesn’t have to override this method
- inverse_transform(self, X)[source]#
Transform X back to its original space. Returns X_original whose transform would be X.
- Parameters:
- Xarray-like (device or host) shape = (n_samples, n_features)
Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.
- Returns:
- X_originalcuDF, CuPy or NumPy object depending on cuML’s output type configuration, shape = (n_samples, n_features)
X in original space
For more information on how to configure cuML’s output type, refer to: Output Data Type Configuration.
- set_params(**params)[source]#
Accepts a dict of params and updates the corresponding ones owned by this class. If the child class has appropriately overridden the
_get_param_namesmethod and does not need anything other than what is, there in this method, then it doesn’t have to override this method
- transform(self, X)[source]#
Perform dimensionality reduction on X.
- Parameters:
- Xarray-like (device or host) shape = (n_samples, n_features)
Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.
- Returns:
- X_newcuDF, CuPy or NumPy object depending on cuML’s output type configuration, shape = (n_samples, n_components)
Reduced version of X
For more information on how to configure cuML’s output type, refer to: Output Data Type Configuration.