Principal component analysis in Python

Months later, here’s a small class PCA, and a picture: #!/usr/bin/env python “”” a small class for Principal Component Analysis Usage: p = PCA( A, fraction=0.90 ) In: A: an array of e.g. 1000 observations x 20 variables, 1000 rows x 20 columns fraction: use principal components that account for e.g. 90 % of the … Read more

Recovering features names of explained_variance_ratio_ in PCA with sklearn

This information is included in the pca attribute: components_. As described in the documentation, pca.components_ outputs an array of [n_components, n_features], so to get how components are linearly related with the different features you have to: Note: each coefficient represents the correlation between a particular pair of component and feature import pandas as pd import … Read more

Matlab – PCA analysis and reconstruction of multi dimensional data

Here’s a quick walkthrough. First we create a matrix of your hidden variables (or “factors”). It has 100 observations and there are two independent factors. >> factors = randn(100, 2); Now create a loadings matrix. This is going to map the hidden variables onto your observed variables. Say your observed variables have four features. Then … Read more

Feature/Variable importance after a PCA analysis

First of all, I assume that you call features the variables and not the samples/observations. In this case, you could do something like the following by creating a biplot function that shows everything in one plot. In this example, I am using the iris data. Before the example, please note that the basic idea when … Read more

Principal Component Analysis (PCA) in Python

I posted my answer even though another answer has already been accepted; the accepted answer relies on a deprecated function; additionally, this deprecated function is based on Singular Value Decomposition (SVD), which (although perfectly valid) is the much more memory- and processor-intensive of the two general techniques for calculating PCA. This is particularly relevant here … Read more