Skip to navigation

PCA

View as Markdown

Python module: cuvs.preprocessing.pca

Params

cdef 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

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

def n_components(self)

copy

def copy(self)

whiten

def whiten(self)

algorithm

def algorithm(self)

tol

def tol(self)

n_iterations

def n_iterations(self)

fit

@auto_sync_resources

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

The input layout (C-contiguous / row-major or F-contiguous / col-major) is preserved natively; no internal copy/transpose is performed. Output arrays use the same layout as the input.

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. Must be contiguous in either C- or F-order.
resourcescuvs.common.Resources, optional

Returns

FitOutput Named tuple with fields: components, explained_var, explained_var_ratio, singular_vals, mu, noise_vars. components matches the layout of X.

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)

fit_transform

@auto_sync_resources

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

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

The input layout (C- or F-contiguous) is preserved natively; output arrays use the same layout.

Parameters

NameTypeDescription
paramsParamsPCA parameters.
Xdevice array-like, shape (n_samples, n_features), float32Input data. Must be contiguous in either C- or F-order.
resourcescuvs.common.Resources, optional

Returns

FitTransformOutput Named tuple with fields: trans_input, components, explained_var, explained_var_ratio, singular_vals, mu, noise_vars. trans_input and components match the layout of X.

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)

inverse_transform

@auto_sync_resources @auto_convert_output

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

The layout (C- or F-contiguous) of trans_input is preserved; components and output are aligned to that layout.

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 (float32). Layout is matched to trans_input.
resourcescuvs.common.Resources, optional

Returns

NameTypeDescription
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)

transform

@auto_sync_resources @auto_convert_output

def 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. The input layout (C- or F-contiguous) of X determines the layout used internally; components and trans_input are aligned to that layout.

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 (float32). Layout is matched to X.
resourcescuvs.common.Resources, optional

Returns

NameTypeDescription
trans_inputdevice 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)