Kernel principal component analysis - Kernel principal component analysis - 5.3 English - 68552

AOCL API Guide (68552)

Document_ID
68552
Release_Date
2026-05-13
Version
5.3 English
class aoclda.factorization.KernelPCA(n_components=0, kernel='linear', gamma=-1.0, degree=3, coef0=1.0, fit_inverse_transform=False, alpha=1.0, remove_zero_eig=False, check_data=False)#

Kernel principal component analysis (kernel PCA).

Performs nonlinear dimensionality reduction using kernels.

Parameters:
  • n_components (int, optional) – Number of components to keep. If 0, all non-zero components are kept. Default = 0.

  • kernel (str, optional) – Kernel function to use. Possible values: 'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'. Default = 'linear'.

  • eigen_solver (str, optional) – Method to use for eigendecomposition of the kernel matrix. Possible values: 'auto', 'syevd'. Default = 'auto'.

  • gamma (float, optional) – Kernel coefficient for 'rbf', 'poly', and 'sigmoid' kernels. If -1.0, it is set to 1 / n_features. Default = -1.0.

  • degree (int, optional) – Degree for the polynomial kernel. Default = 3.

  • coef0 (float, optional) – Independent term for 'poly' and 'sigmoid' kernels. Default = 1.0.

  • fit_inverse_transform (bool, optional) – Whether to learn the approximate inverse transform during fit, enabling inverse_transform. Cannot be used with a precomputed kernel. Default = False.

  • alpha (float, optional) – Ridge regularization parameter for the inverse transform. Must be positive. Default = 1.0.

  • remove_zero_eig (bool, optional) – Whether to remove components whose eigenvalue is zero. Default = False.

  • check_data (bool, optional) – Whether to check the data for NaNs prior to computation. Default = False.

  • copy_X (bool, optional) – Whether to store a copy of the user data. Default = True.

property X_fit_#

The training data used to fit the kernel PCA, tight-packed in the fitted storage order.

Type:

numpy.ndarray of shape (n_samples, n_features)

property dual_coef#

Dual coefficients from the kernel ridge regression inverse transform. Only available when fit_inverse_transform=True.

Type:

numpy.ndarray of shape (n_samples, n_features)

property eigenvalues#

Eigenvalues \(\lambda_1 \ge \cdots \ge \lambda_{n_\mathrm{components}}\) of the centered kernel matrix.

Type:

numpy.ndarray of shape (n_components, )

property eigenvectors#

Eigenvectors \(V\) of the centered kernel matrix.

Type:

numpy.ndarray of shape (n_samples, n_components)

fit(A)#

Computes the kernel principal component analysis on the supplied data matrix.

Parameters:

A (array-like) – The data matrix with which to compute the kernel PCA. It has shape (n_samples, n_features).

Returns:

Returns the instance itself.

Return type:

self (object)

property gamma_#

The resolved gamma parameter used in the computation. When gamma is set to a value less than 0, this returns 1 / n_features.

Type:

float

inverse_transform(Y)#

Transform data from the kernel PCA feature space back to the original space.

Requires fit_inverse_transform=True to have been set before calling fit.

Parameters:

Y (array-like) – The data matrix to be transformed. It has shape (k_samples, k_features). Note that k_features must match n_components, the number of principal components computed by fit.

Returns:

The reconstructed

matrix.

Return type:

numpy.ndarray of shape (k_samples, n_features)

property n_components#

The number of components found in the kernel PCA.

Type:

int

property n_features#

The number of features in the data matrix used to compute the kernel PCA.

Type:

int

property n_samples#

The number of samples in the data matrix used to compute the kernel PCA.

Type:

int

property scores#

The kernel PCA scores, \(Z = V \cdot \mathrm{diag}(\sqrt{\lambda_i})\)

Type:

numpy.ndarray of shape (n_samples, n_components)

transform(X)#

Transform a data matrix into the kernel PCA feature space.

Parameters:

X (array-like) – The data matrix to be transformed. It has shape (m_samples, m_features). Note that m_features must match n_features, the number of features in the data matrix originally supplied to fit.

Returns:

The transformed

matrix.

Return type:

numpy.ndarray of shape (m_samples, n_components)

da_status da_kernel_pca_set_data_s(da_handle handle, da_int n_samples, da_int n_features, const float *A, da_int lda)#
da_status da_kernel_pca_set_data_d(da_handle handle, da_int n_samples, da_int n_features, const double *A, da_int lda)#

Pass a data matrix to the da_handle object in preparation for computing the kernel PCA.

When kernel is set to precomputed, the matrix A is the precomputed kernel matrix (n_samples \(\times\) n_samples), and n_features must equal n_samples.

Parameters:
  • handle – [inout] a da_handle object, initialized using da_handle_init_? with type da_handle_kernel_pca.

  • n_samples – [in] the number of rows of the data matrix, A. Constraint: n_samples \(\ge\) 1.

  • n_features – [in] the number of columns of the data matrix, A. Constraint: n_features \(\ge\) 1.

  • A – [in] the n_samples \(\times\) n_features data matrix. By default, it should be stored in column-major order, unless you have set the storage order option to row-major.

  • lda – [in] the leading dimension of the data matrix.

Returns:

da_status. The function returns:

da_status da_kernel_pca_compute_s(da_handle handle)#
da_status da_kernel_pca_compute_d(da_handle handle)#

Compute kernel PCA.

Builds and centers the kernel matrix, performs the eigendecomposition, and computes the kernel PCA scores. Optionally fits the inverse transform via kernel ridge regression when the fit inverse transform option is set to 1.

In addition da_handle_get_result_int can be queried with the following enums:

  • da_kernel_pca_n_components - return an array of size 1 containing n_components, the number of principal components found.

Parameters:

handle – [inout] a da_handle object, initialized with type da_handle_kernel_pca and with data passed in via da_kernel_pca_set_data_?.

Returns:

da_status. The function returns:

Post:

After successful execution, da_handle_get_result_? can be queried with the following enums:

  • da_rinfo - return an array of size 2 containing n_samples, n_features.

  • da_kernel_pca_gamma - return an array of size 1 containing the resolved gamma value.

  • da_kernel_pca_eigenvalues - return an array of size n_components containing the eigenvalues \(\lambda_1 \ge \cdots \ge \lambda_{n_\mathrm{components}}\) of the centered kernel matrix.

  • da_kernel_pca_eigenvectors - return an array of size n_samples \(\times\) n_components containing the eigenvectors \(V\) of the centered kernel matrix.

  • da_kernel_pca_scores - return an array of size n_samples \(\times\) n_components containing the scores \(Z = V \cdot \mathrm{diag}(\sqrt{\lambda_i})\).

  • da_kernel_pca_dual_coef - return an array of size n_samples \(\times\) n_features containing the dual coefficients from the kernel ridge regression. Only available when fit inverse transform is set to 1.

da_status da_kernel_pca_transform_s(da_handle handle, da_int m_samples, da_int m_features, const float *X, da_int ldx, float *X_transform, da_int ldx_transform)#
da_status da_kernel_pca_transform_d(da_handle handle, da_int m_samples, da_int m_features, const double *X, da_int ldx, double *X_transform, da_int ldx_transform)#

Transform a data matrix into the kernel PCA feature space.

Projects m_samples new points into the reduced kernel PCA space previously computed via da_kernel_pca_compute_?. A cross-kernel matrix is built between the new data and the stored training data, centered using training statistics, and projected onto the kernel principal components.

When kernel is set to precomputed, X is the (m_samples \(\times\) n_samples_train) cross-kernel matrix and m_features must equal n_samples from the training call.

Parameters:
  • handle – [inout] a da_handle object, with a kernel PCA previously computed via da_kernel_pca_compute_?.

  • m_samples – [in] the number of rows of the data matrix, X. Constraint: m_samples \(\ge\) 1.

  • m_features – [in] the number of columns of the data matrix, X.

  • X – [in] the m_samples \(\times\) m_features data matrix.

  • ldx – [in] the leading dimension of the data matrix.

  • X_transform – [out] an array in which the transformed data will be stored.

  • ldx_transform – [in] the leading dimension of X_transform.

Returns:

da_status. The function returns:

da_status da_kernel_pca_inverse_transform_s(da_handle handle, da_int k_samples, da_int k_components, const float *Y, da_int ldy, float *Y_inv_transform, da_int ldy_inv_transform)#
da_status da_kernel_pca_inverse_transform_d(da_handle handle, da_int k_samples, da_int k_components, const double *Y, da_int ldy, double *Y_inv_transform, da_int ldy_inv_transform)#

Map points from the kernel PCA space back to the original feature space.

Applies the approximate pre-image mapping learned during da_kernel_pca_compute_? (when the fit inverse transform option was set to 1) to reconstruct data in the original feature space. This operation is incompatible with kernel set to precomputed.

Parameters:
  • handle – [inout] a da_handle object, with a kernel PCA previously computed with fit inverse transform = 1.

  • k_samples – [in] the number of rows of the data matrix, Y. Constraint: k_samples \(\ge\) 1.

  • k_components – [in] the number of columns of the data matrix, Y. Constraint: k_components \(=\) n_components.

  • Y – [in] the k_samples \(\times\) k_components data matrix in the reduced kernel PCA space.

  • ldy – [in] the leading dimension of Y.

  • Y_inv_transform – [out] an array in which the reconstructed data will be stored.

  • ldy_inv_transform – [in] the leading dimension of Y_inv_transform.

Returns:

da_status. The function returns: