Documentation of VIPurPCA
VIPurPCA offers a visualization of uncertainty propagated through the dimensionality reduction technique Principal Component Analysis (PCA) by automatic differentiation.
Code is available here.
Installation
VIPurPCA requires Python 3.7.3 or later and can be installed via:
pip install vipurpca
Usage
Propagating uncertainty through PCA and visualize output uncertainty as animated scatter plot
In order to propagate uncertainty through PCA the class PCA
can be used, which has the following parameters, attributes, and methods:
Parameters | |
---|---|
matrix : array_like Array of size [n, p] containing mean numbers to which VIPurPCA should be applied. |
|
n_components : int or float, default=None, optional Number of components to keep. |
|
axis : {0, 1} , default=0, optional The default expects samples in rows and features in columns. |
|
cov_data : array_like of shape [n*p] or [n*p, n*p] , default=None, optional Uncertainties attached to the numbers in matrix. If cov_data is one-dimensional it is assumend to be the diagonal of a diagonal matrix. If None |
|
compute_jacobian : Boolean, default=False, optional Whether or whether not to propagate uncertainty through PCA. |
Attributes | |
---|---|
size : [n, p] Dimension of matrix (n: number of samples, p: number of dimensions) |
|
covariance : ndarray of size [p, p] Features’ covariance matrix. |
|
eigenvalues : ndarray of size [n_components] Eigenvalues obtained from eigenvalue decomposition of the covariance matrix. |
|
eigenvectors : ndarray of size [n_components*p, n*p] Eigenvectors obtained from eigenvalue decomposition of the covariance matrix. |
|
jacobian : ndarray of size [n_components*p, n*p] Jacobian containing derivatives of eigenvectors w.r.t. input matrix. |
|
jacobian_eigenvalues : ndarray of size [n_components*p, n*p] Jacobian containing derivatives of eigenvalues w.r.t. input matrix. |
|
cov_eigenvectors : ndarray of size [n_components*p, n_components*p] Propagated uncertainties of eigenvectors. |
|
cov_eigenvalues : ndarray of size [n_components*n_components] Propagaged uncertainties of eigenvalues. |
|
transformed data : ndarray of size [n, n_components] Low dimensional representation of data after applying PCA. |
Methods | |
---|---|
pca_value() | Apply PCA to the matrix. |
pca_grad(center=True) | Apply PCA to the matrix and compute the jacobian and jacobian_eigenvalues using automatic differentiation. |
transform_data() | Transform matrix according to eigenvectors and reduce dimensionality according to n_components. |
compute_cov_eigenvectors() | Compute uncertainties of eigenvectors. |
compute_cov_eigenvalues() | Compute uncertainties of eigenvalues. |
animate(n_frames, labels, outfile) | Generate animation with n_frames number of frames with plotly. labels (list, 1d array) indicate labelling of individual samples. Save animation (as html) at outfile. |
Example
from vipurpca import load_data
from vipurpca import PCA
# load mean (Y), uncertainty estimates (cov_Y) and labels (y)
Y, cov_Y, y = load_data.load_mice_dataset()
pca_student_grades = PCA(matrix=Y, cov_data=cov_Y, n_components=2, axis=0, compute_jacobian=True)
# compute PCA with backprop
pca_student_grades.pca_grad()
# Bayesian inference
pca_student_grades.compute_cov_eigenvectors()
pca_student_grades.compute_cov_eigenvalues()
# Transform data
pca_student_grades.transform_data()
pca_student_grades.animate(n_frames=10, labels=y, outfile='animation.html')
The resulting animation can be found here here.