Pca question

I was wondering if there is a command or a way to produce the result of the “explained_variance_ratio_” command from scikitlearn in order to plot it.

Thanks.

Hi @Kevin_25,

Since this is your first post, please read Please read: make it easier to help you.

Unless I’m misunderstanding your question, you’ve stumbled onto a Julia forum instead of a Python/scikit-learn forum. I suggest you consult the scikit-learn documentation or ask on their forums.

@Kevin_25, if you are talking about PCA as implemented in MultivariateStats.jl, you can get the proportion of total variance explained by each component like so:

using MultivariateStats
X = randn(10, 1000)
M = fit(PCA, X)
principalvars(M) / tvar(M)

See the documentation here: Principal Component Analysis — MultivariateStats 0.1.0 documentation

If you’re doing it “by hand,” you can get the same info like this:

using LinearAlgebra, Statistics
λ, Φ = eigen(cov(X, dims=2))
λ / sum(λ)

(Note that eigen returns the eigenvalues in ascending order.)

2 Likes