- class aoclda.factorization.PCA(n_components=1, bias='unbiased', method='covariance', solver='auto', store_U=False, whiten=False, check_data=False)#
Principal component analysis (PCA).
Find all or some of the principal components of a data matrix.
- Parameters:
n_components (int, optional) – Number of components to keep. Default=1.
bias (str, optional) – Whether to use unbiased or biased estimators for standard deviations and variances. It can take the values ‘unbiased’ or ‘biased’. Default: ‘unbiased’.
method (str, optional) –
The method used to compute the PCA. Default = ‘covariance’.
If
method = 'covariance'then the columns are mean-centered so the PCA is based on the covariance matrix.If
method = 'correlation'then the columns are mean-centered and scaled by the standard deviation so the PCA is based on the correlation matrix.If
method = 'svd'then no normalization occurs so the PCA reduces to the singular value decomposition.
solver (str, optional) – Which LAPACK solver to use to compute the underlying singular value decomposition, allowed values: ‘auto’, ‘gesdd’, ‘gesvd’, ‘gesvdx’, ‘syevd’. If
solver = 'syevd'then then the SVD will be found by explicitly forming the covariance or correlation matrix and performing an eigendecomposition. This is very fast for tall, thin data matrices but for wider matrices it requires a lot of memory. The method is also more susceptible to ill-conditioning so must be used with care. It is incompatible with the ‘store_U’ option. Ifsolver = 'auto'then ‘gesdd’ will be used unless internal heuristics determine that eigendecomposition using syevd is quicker. Default=’auto’.store_U (bool, optional) – Controls whether to store the matrix
Ufrom the singular value decomposition. This allows deterministic results regarding the sign of the principal components, at the expense of some extra computation. This option cannot be used ifsolver = 'syevd'. Default = False.check_data (bool, optional) – Whether to check the data for NaNs. Default = False.
whiten (bool, optional) – Whether to whiten the data upon transformation. This divides each principal component by its corresponding singular value and multiplies the component by a dimensional factor so the transformed data, specifically that data used to fit the PCA, has a unit diagonal covariance matrix. Default = False.
- property column_means#
The column means of the data matrix. These are computed if
method = 'covariance'ormethod = 'correlation'.- Type:
numpy.ndarray of shape (n_features, )
- property column_sdevs#
The column standard deviations of the data matrix. These are only computed if
method = 'correlation'- Type:
numpy.ndarray of shape (n_features, )
- fit(A)#
Computes the principal component analysis on the supplied data matrix.
- Parameters:
A (array-like) – The data matrix with which to compute the PCA. It has shape (n_samples, n_features).
- Returns:
Returns the instance itself.
- Return type:
self (object)
- inverse_transform(Y)#
Transform a data matrix into the original coordinate space.
Transforms a data matrix
Yin the new feature space back into the original coordinate space used by the matrix which was supplied topca.fit. The transformation is computed by projectingYinto the original coordinate space, then inverting any standardization used on the original data matrix.- 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
pca.fit.- Returns:
The transformed matrix.
- Return type:
numpy.ndarray of shape (k_samples, n_features)
- property n_components#
The number of components found in the PCA.
- Type:
int
- property n_features#
The number of features in the data matrix used to compute the PCA.
- Type:
int
- property n_samples#
The number of samples in the data matrix used to compute the PCA.
- Type:
int
- property principal_components#
Principal axes in feature space, representing the directions of maximum variance in the data. Equivalently, the right singular vectors of the normalized input data, parallel to its eigenvectors. The components are sorted by decreasing
variance.- Type:
numpy.ndarray of shape (n_components, n_features)
- property scores#
The principal component scores, \(U\Sigma\).
- Type:
numpy.ndarray of shape (n_samples, n_components)
- property sigma#
The diagonal values of \(\Sigma\) from the SVD.
- Type:
numpy.ndarray of shape (n_components,)
- property total_variance#
The total amount of variance within the dataset.
- Type:
numpy.ndarray of shape (1, )
- transform(X)#
Transform a data matrix into new feature space.
Transforms a data matrix
Xfrom the original coordinate system into the new coordinates previously computed bypca.fit. The transformation is computed by applying any standardization used on the original data matrix toX, then projectingXinto the previously computed principal components.- 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
pca.fit.- Returns:
The transformed matrix.
- Return type:
numpy.ndarray of shape (m_samples, n_components)
- property u#
The matrix \(U\) from the SVD.
- Type:
numpy.ndarray of shape (n_samples, n_samples)
- property variance#
The amount of variance explained by each of the selected components.
- Type:
numpy.ndarray of shape (n_components, )
- property vt#
The matrix \(V^T\) from the SVD.
- Type:
numpy.ndarray of shape (n_components, n_features)
-
da_status da_pca_set_data_s(da_handle handle, da_int n_samples, da_int n_features, const float *A, da_int lda)#
-
da_status da_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 PCA.
A copy of the data matrix is stored internally, to avoid overwriting the user’s data during the computation.
After calling this function you may use the option setting APIs to set options.
- Parameters:
handle – [inout] a da_handle object, initialized using da_handle_init_? with type da_handle_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. Constraint:
lda\(\ge\)n_samplesifAis stored in column-major order, orlda\(\ge\)n_featuresifAis stored in row-major order.
- 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_pointer - the handle has not been initialized, or
Ais null.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_leading_dimension - the constraint on
ldawas violated.
-
da_status da_pca_compute_d(da_handle handle)#
Compute PCA.
Computes a principal component analysis on the data matrix previously passed into the handle using da_pca_set_data_?.
- Parameters:
handle – [inout] a da_handle object, initialized using da_handle_init_? with type da_handle_pca and with data passed in via da_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_invalid_pointer - the handle has not been initialized.
da_status_no_data - da_pca_set_data_? has not been called prior to this function call.
da_status_internal_error - this can occur if your data contains undefined values.
- Post:
After successful execution, da_handle_get_result_? can be queried with the following enums:
da_rinfo- return an array of size 3 containingn_samples,n_featuresandn_components, the number of principal components found.da_pca_principal_components- return an array of sizen_components\(\times\)n_featurescontaining the principal components, in the same storage order as the input data, in decreasing order of explained variance.da_pca_scores- return an array of sizen_samples\(\times\)n_componentscontaining the scores in the same storage order as the input data.da_pca_variance- return an array of sizen_componentscontaining the amount of variance explained by each principal component.da_pca_total_variance- return an array of size1containing the total of variance within the dataset.da_pca_u- return an array of sizen_samples\(\times\)n_componentscontaining the matrix \( U \) from the SVD of the standardized data matrix in the same storage order as the input data.da_pca_sigma- return an array of sizen_componentscontaining the singular values of the standardized data matrix.da_pca_vt- return an array of sizen_components\(\times\)n_featurescontaining the matrix \( V^T \) from the SVD of the standardized data matrix in the same storage order as the input data.da_pca_column_means- return an array of sizen_featurescontaining the column means of the data matrix (note this is only available if the option PCA method was set to covariance or correlation).da_pca_column_sdevs- return an array of sizen_featurescontaining the column standard deviations (withn_samples\(\ - 1 \) degrees of freedom) of the data matrix (note this is only available if the option PCA method was set to correlation).
-
da_status da_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_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 new feature space.
Transforms a data matrix
Xfrom the original coordinate system into the new coordinates previously computed in da_pca_compute_?. The transformation is computed by applying any standardization used on the original data matrix toX, then projectingXinto the previously computed principal components.- Parameters:
handle – [inout] a da_handle object, with a PCA previously computed via da_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. Constraint:m_features\(=\)n_features, the number of features in the data matrix originally supplied to da_pca_set_data_?.X – [in] the
m_samples\(\times\)m_featuresdata matrix, in the same storage format used to fit the model.ldx – [in] the leading dimension of the data matrix. Constraint:
ldx\(\ge\)m_samplesifXis stored in column-major order, orldx\(\ge\)m_featuresifXis stored in row-major order.X_transform – [out] an array of size at least
m_samples\(\times\)n_components, in which the transformed data will be stored (in the same storage format used to fit the model).ldx_transform – [in] the leading dimension of
X_transform. Constraint:ldx_transform\(\ge\)m_samplesifXis stored in column-major order, orldx_transform\(\ge\)n_componentsifXis stored in row-major order.
- 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_pointer - the handle has not been initialized, or one of the arrays is null.
da_status_no_data - the PCA has not been computed 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_leading_dimension - one of the constraints on
ldxorldx_transformwas violated.
-
da_status da_pca_inverse_transform_s(da_handle handle, da_int k_samples, da_int k_features, const float *Y, da_int ldy, float *Y_inv_transform, da_int ldy_inv_transform)#
-
da_status da_pca_inverse_transform_d(da_handle handle, da_int k_samples, da_int k_features, const double *Y, da_int ldy, double *Y_inv_transform, da_int ldy_inv_transform)#
Transform a data matrix into the original coordinate space.
Transforms a data matrix
Yin the new feature space back into the original coordinate space used by the matrix which was supplied to da_pca_set_data_?. The transformation is computed by projectingYinto the original coordinate space, then inverting any standardization used on the original data matrix.- Parameters:
handle – [inout] a da_handle object, with a PCA previously computed via da_pca_compute_?.
k_samples – [in] the number of rows of the data matrix,
Y. Constraint:k_samples\(\ge\) 1.k_features – [in] the number of columns of the data matrix,
Y. Constraint:k_features\(=\)n_components, the number of PCA components computed by da_pca_compute_?.Y – [in] the
k_samples\(\times\)k_featuresdata matrix, in the same storage format used to fit the model.ldy – [in] the leading dimension of the data matrix. Constraint:
ldy\(\ge\)k_samplesifYis stored in column-major order, orldy\(\ge\)k_featuresifYis stored in row-major order.Y_inv_transform – [out] an array of size at least
k_samples\(\times\)n_features, in which the transformed data will be stored (in the same storage format used to fit the model).ldy_inv_transform – [in] the leading dimension of
Y_inv_transform. Constraint:ldy_inv_transform\(\ge\)k_samplesifYis stored in column-major order, orldy_inv_transform\(\ge\)n_featuresifYis stored in row-major order.
- 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_pointer - the handle has not been initialized, or one of the arrays is null.
da_status_no_data - the PCA has not been computed 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_leading_dimension - one of the constraints on
ldyorldy_inv_transformwas violated.