PCA

View as Markdown

Python module: cuvs.preprocessing.pca

Params

1cdef class Params

Parameters for PCA decomposition.

Parameters

NameTypeDescription
n_componentsintNumber of principal components to keep (default: 1).
copyboolIf 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).
whitenboolWhen 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).
algorithmstrSolver algorithm. One of "cov_eig_dq" (divide-and-conquer) or "cov_eig_jacobi" (Jacobi) (default: "cov_eig_dq").
tolfloatTolerance for singular values, used by the Jacobi solver (default: 0.0).
n_iterationsintNumber of iterations for the Jacobi solver (default: 15).

Constructor

1def __init__(self, *, n_components=None, copy=None, whiten=None, algorithm=None, tol=None, n_iterations=None)

Members

NameKind
n_componentsproperty
copyproperty
whitenproperty
algorithmproperty
tolproperty
n_iterationsproperty

n_components

1def n_components(self)

copy

1def copy(self)

whiten

1def whiten(self)

algorithm

1def algorithm(self)

tol

1def tol(self)

n_iterations

1def n_iterations(self)

fit

@auto_sync_resources

1def 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

NameTypeDescription
paramsParamsPCA parameters. params.copy should be True if you intend to reuse X after this call.
Xdevice array-like, shape (n_samples, n_features), float32Input data (will be converted to col-major device memory).
resourcescuvs.common.Resources, optional

Returns

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

Examples

1>>> import cupy as cp
2>>> from cuvs.preprocessing import pca
3>>> X = cp.random.random_sample((500, 32), dtype=cp.float32)
4>>> params = pca.Params(n_components=8, copy=True)
5>>> result = pca.fit(params, X)
6>>> result.components.shape
7(8, 32)

fit_transform

@auto_sync_resources

1def fit_transform(Params params, X, resources=None)

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

Parameters

NameTypeDescription
paramsParamsPCA parameters.
Xdevice array-like, shape (n_samples, n_features), float32Input data (will be converted to col-major device memory).
resourcescuvs.common.Resources, optional

Returns

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

Examples

1>>> import cupy as cp
2>>> from cuvs.preprocessing import pca
3>>> X = cp.random.random_sample((500, 32), dtype=cp.float32)
4>>> params = pca.Params(n_components=8)
5>>> result = pca.fit_transform(params, X)
6>>> result.trans_input.shape
7(500, 8)

inverse_transform

@auto_sync_resources @auto_convert_output

1def 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

NameTypeDescription
paramsParamsPCA 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).
resourcescuvs.common.Resources, optional

Returns

NameTypeDescription
outputdevice array, shape (n_samples, n_features)Reconstructed data.

Examples

1>>> import cupy as cp
2>>> from cuvs.preprocessing import pca
3>>> X = cp.random.random_sample((500, 32), dtype=cp.float32)
4>>> params = pca.Params(n_components=8)
5>>> result = pca.fit_transform(params, X)
6>>> reconstructed = pca.inverse_transform(
7... params, result.trans_input, result.components,
8... result.singular_vals, result.mu)

transform

@auto_sync_resources @auto_convert_output

1def 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

NameTypeDescription
paramsParamsPCA parameters (must match those used during fit).
Xdevice array-like, shape (n_samples, n_features), float32Data 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).
resourcescuvs.common.Resources, optional

Returns

NameTypeDescription
trans_inputdevice array, shape (n_samples, n_components)

Examples

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