cuvs.preprocessing.pca#

5 min read time

Applies to Linux

Submodules#

Classes#

Params

Params(n_components=None, *, copy=None, whiten=None, algorithm=None, tol=None, n_iterations=None)

Functions#

fit(*args[, resources])

fit(Params params, X, resources=None)

fit_transform(*args[, resources])

fit_transform(Params params, X, resources=None)

inverse_transform(*args[, resources])

inverse_transform(Params params, trans_input, components, singular_vals, mu, output=None, resources=None)

transform(*args[, resources])

transform(Params params, X, components, singular_vals, mu, trans_input=None, resources=None)

Package Contents#

class cuvs.preprocessing.pca.Params#

Params(n_components=None, *, copy=None, whiten=None, algorithm=None, tol=None, n_iterations=None)

Parameters for PCA decomposition.

Parameters#

n_componentsint

Number of principal components to keep (default: 1).

copybool

If False, data passed to fit are overwritten and running fit(X) then transform(X) will not yield the expected results; use fit_transform(X) instead (default: True).

whitenbool

When True the component vectors are multiplied by the square root of n_samples and divided by the singular values to ensure uncorrelated outputs with unit component-wise variances (default: False).

algorithmstr

Solver algorithm. One of "cov_eig_dq" (divide-and-conquer) or "cov_eig_jacobi" (Jacobi) (default: "cov_eig_dq").

tolfloat

Tolerance for singular values, used by the Jacobi solver (default: 0.0).

n_iterationsint

Number of iterations for the Jacobi solver (default: 15).

static __reduce__(*args, **kwargs)#

Params.__reduce_cython__(self)

static __setstate__(*args, **kwargs)#

Params.__setstate_cython__(self, __pyx_state)

cuvs.preprocessing.pca.fit(*args, resources=None, **kwargs)#

fit(Params params, X, resources=None)

Compute PCA (fit only).

Computes the principal components, explained variances, singular values, and column means from the input data.

Parameters#

paramsParams

PCA parameters. params.copy should be True if you intend to reuse X after this call.

Xdevice array-like, shape (n_samples, n_features), float32

Input data (will be converted to col-major device memory).

resourcesOptional cuVS Resource handle for reusing CUDA resources.

If Resources aren’t supplied, CUDA resources will be allocated inside this function and synchronized before the function exits. If resources are supplied, you will need to explicitly synchronize yourself by calling resources.sync() before accessing the output.

Returns#

FitOutput

Named tuple with fields: components, explained_var, explained_var_ratio, singular_vals, mu, noise_vars.

Examples#

>>> import cupy as cp
>>> from cuvs.preprocessing import pca
>>> X = cp.random.random_sample((500, 32), dtype=cp.float32)
>>> params = pca.Params(n_components=8, copy=True)
>>> result = pca.fit(params, X)
>>> result.components.shape
(8, 32)
cuvs.preprocessing.pca.fit_transform(*args, resources=None, **kwargs)#

fit_transform(Params params, X, resources=None)

Compute PCA and transform the input data in a single operation.

Parameters#

paramsParams

PCA parameters.

Xdevice array-like, shape (n_samples, n_features), float32

Input data (will be converted to col-major device memory).

resourcesOptional cuVS Resource handle for reusing CUDA resources.

If Resources aren’t supplied, CUDA resources will be allocated inside this function and synchronized before the function exits. If resources are supplied, you will need to explicitly synchronize yourself by calling resources.sync() before accessing the output.

Returns#

FitTransformOutput

Named tuple with fields: trans_input, components, explained_var, explained_var_ratio, singular_vals, mu, noise_vars.

Examples#

>>> import cupy as cp
>>> from cuvs.preprocessing import pca
>>> X = cp.random.random_sample((500, 32), dtype=cp.float32)
>>> params = pca.Params(n_components=8)
>>> result = pca.fit_transform(params, X)
>>> result.trans_input.shape
(500, 8)
cuvs.preprocessing.pca.inverse_transform(*args, resources=None, **kwargs)#

inverse_transform(Params params, trans_input, components, singular_vals, mu, output=None, resources=None)

Transform data from the PCA eigenspace back to the original space.

Parameters#

paramsParams

PCA parameters (must match those used during fit).

trans_inputdevice array-like, shape (n_samples, n_components)

Transformed data from transform() or fit_transform().

componentsdevice array-like, shape (n_components, n_features)

Principal components from a prior fit.

singular_valsdevice array-like, shape (n_components,)

Singular values from a prior fit.

mudevice array-like, shape (n_features,)

Column means from a prior fit.

outputoptional device array, shape (n_samples, n_features)

Pre-allocated output buffer (col-major, float32).

resourcesOptional cuVS Resource handle for reusing CUDA resources.

If Resources aren’t supplied, CUDA resources will be allocated inside this function and synchronized before the function exits. If resources are supplied, you will need to explicitly synchronize yourself by calling resources.sync() before accessing the output.

Returns#

outputdevice array, shape (n_samples, n_features)

Reconstructed data.

Examples#

>>> import cupy as cp
>>> from cuvs.preprocessing import pca
>>> X = cp.random.random_sample((500, 32), dtype=cp.float32)
>>> params = pca.Params(n_components=8)
>>> result = pca.fit_transform(params, X)
>>> reconstructed = pca.inverse_transform(
...     params, result.trans_input, result.components,
...     result.singular_vals, result.mu)
cuvs.preprocessing.pca.transform(*args, resources=None, **kwargs)#

transform(Params params, X, components, singular_vals, mu, trans_input=None, resources=None)

Transform data into the PCA eigenspace.

Uses previously computed principal components from fit() or fit_transform().

Parameters#

paramsParams

PCA parameters (must match those used during fit).

Xdevice array-like, shape (n_samples, n_features), float32

Data to transform.

componentsdevice array-like, shape (n_components, n_features)

Principal components from a prior fit.

singular_valsdevice array-like, shape (n_components,)

Singular values from a prior fit.

mudevice array-like, shape (n_features,)

Column means from a prior fit.

trans_inputoptional device array, shape (n_samples, n_components)

Pre-allocated output buffer (col-major, float32).

resourcesOptional cuVS Resource handle for reusing CUDA resources.

If Resources aren’t supplied, CUDA resources will be allocated inside this function and synchronized before the function exits. If resources are supplied, you will need to explicitly synchronize yourself by calling resources.sync() before accessing the output.

Returns#

trans_input : device array, shape (n_samples, n_components)

Examples#

>>> import cupy as cp
>>> from cuvs.preprocessing import pca
>>> X = cp.random.random_sample((500, 32), dtype=cp.float32)
>>> params = pca.Params(n_components=8, copy=True)
>>> result = pca.fit(params, X)
>>> transformed = pca.transform(params, X, result.components,
...                             result.singular_vals, result.mu)