- 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 to1 / 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, enablinginverse_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=Trueto have been set before callingfit.- 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
kernelis set to precomputed, the matrixAis the precomputed kernel matrix (n_samples \(\times\) n_samples), andn_featuresmust equaln_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_featuresdata 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_success - the operation was successfully completed.
da_status_wrong_type - the handle may have been initialized using the wrong precision.
da_status_invalid_handle_type - the handle was not initialized with type da_handle_kernel_pca.
da_status_handle_not_initialized - the handle has not been initialized.
da_status_invalid_pointer -
Ais null.da_status_invalid_array_dimension -
n_samplesorn_featureswas less than 1.da_status_invalid_leading_dimension - the constraint on
ldawas violated.da_status_invalid_input - one of the arguments had an invalid value.
da_status_memory_error - a memory allocation failed.
da_status_incompatible_options -
n_componentsexceeds the maximum possible and has been clamped.
-
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 size1containingn_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:
da_status_success - the operation was successfully completed.
da_status_wrong_type - the handle may have been initialized using the wrong precision.
da_status_handle_not_initialized - the handle has not been initialized.
da_status_no_data - da_kernel_pca_set_data_? has not been called prior to this function call.
da_status_invalid_input - one of the arguments had an invalid value. You can obtain further information using da_handle_print_error_message.
da_status_numerical_difficulties - the eigenvalue decomposition or inverse transform factorization failed numerically.
- Post:
After successful execution, da_handle_get_result_? can be queried with the following enums:
da_rinfo- return an array of size 2 containingn_samples,n_features.da_kernel_pca_gamma- return an array of size1containing the resolvedgammavalue.da_kernel_pca_eigenvalues- return an array of sizen_componentscontaining the eigenvalues \(\lambda_1 \ge \cdots \ge \lambda_{n_\mathrm{components}}\) of the centered kernel matrix.da_kernel_pca_eigenvectors- return an array of sizen_samples\(\times\)n_componentscontaining the eigenvectors \(V\) of the centered kernel matrix.da_kernel_pca_scores- return an array of sizen_samples\(\times\)n_componentscontaining the scores \(Z = V \cdot \mathrm{diag}(\sqrt{\lambda_i})\).da_kernel_pca_dual_coef- return an array of sizen_samples\(\times\)n_featurescontaining 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_samplesnew 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
kernelis set to precomputed,Xis the (m_samples \(\times\) n_samples_train) cross-kernel matrix andm_featuresmust equaln_samplesfrom 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_featuresdata 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_success - the operation was successfully completed.
da_status_wrong_type - the handle may have been initialized using the wrong precision.
da_status_invalid_handle_type - the handle was not initialized with type da_handle_kernel_pca.
da_status_handle_not_initialized - the handle has not been initialized.
da_status_invalid_pointer - one of the arrays is null.
da_status_no_data - da_kernel_pca_compute_? has not been called prior to this function call.
da_status_invalid_input - one of the arguments had an invalid value. You can obtain further information using da_handle_print_error_message.
da_status_invalid_array_dimension -
m_sampleswas less than 1.da_status_invalid_leading_dimension - the constraint on
ldxorldx_transformwas violated.da_status_incompatible_options - kernel options have been modified since the model was fitted.
da_status_memory_error - a memory allocation failed.
da_status_internal_error - an unexpected internal error occurred.
-
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
kernelset 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_componentsdata 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:
da_status_success - the operation was successfully completed.
da_status_wrong_type - the handle may have been initialized using the wrong precision.
da_status_invalid_handle_type - the handle was not initialized with type da_handle_kernel_pca.
da_status_handle_not_initialized - the handle has not been initialized.
da_status_invalid_pointer - one of the arrays is null.
da_status_no_data - da_kernel_pca_compute_? has not been called, or fit inverse transform was not set to 1.
da_status_invalid_input - one of the arguments had an invalid value. You can obtain further information using da_handle_print_error_message.
da_status_invalid_array_dimension -
k_sampleswas less than 1.da_status_invalid_leading_dimension - the constraint on
ldyorldy_inv_transformwas violated.da_status_incompatible_options - kernel options have been modified since the model was fitted.
da_status_memory_error - a memory allocation failed.
da_status_internal_error - an unexpected internal error occurred.