Factor Loadings using sklearn

Riyaz picture Riyaz · Jan 19, 2014 · Viewed 25k times · Source

I want the correlations between individual variables and principal components in python. I am using PCA in sklearn. I don't understand how can I achieve the loading matrix after I have decomposed my data? My code is here.

iris = load_iris()
data, y = iris.data, iris.target
pca = PCA(n_components=2)
transformed_data = pca.fit(data).transform(data)
eigenValues = pca.explained_variance_ratio_

http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html doesn't mention how this can be achieved.

Answer

BigPanda picture BigPanda · Feb 5, 2017

Multiply each component by the square root of its corresponding eigenvalue:

pca.components_.T * np.sqrt(pca.explained_variance_)

This should produce your loading matrix.