Qu

pca_explained_variance

Builtin index · Statistics & ML

result = pca_explained_variance(X, k)

Input: X, k · Output: result

What it is

Fits the same PCA as pca on X and k, returning a length-k vector giving the fraction of the total variance in X that each kept component carries, in decreasing order.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = pca_explained_variance(X, k)
X vectornot documented yet
k vectornot documented yet
result Vec
Returns a length-k Vec.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

G = vstack(randn(40, 2, seed = 31) + 4,
           randn(40, 2, seed = 32) - 4,
           randn(40, 2, seed = 33))
d = dbscan(G, 1.5, 4)
print("dbscan labels: {distinct(d)}")

proj  = pca(G, 2)                    # the (n, 2) projected coordinates

comps = pca_components(G, 2)         # the (2, 2) loading matrix

ev    = pca_explained_variance(G, 2)
print("pca projected shape: {rows(proj)} x {cols(proj)}")
print("pca components:\n{round(comps, 3)}")
print("explained variance: {round(ev, 3)}")

small = vstack(randn(6, 2, seed = 41) + 3, randn(6, 2, seed = 42) - 3)
low = tsne(small, n_components = 2, perplexity = 3, seed = 1, max_iter = 100)
print("tsne output rows: {rows(low)}")
output
dbscan labels: (0, 1, 2)
pca projected shape: 120 x 2
pca components:
[0.694, 0.72; 0.72, -0.694]
explained variance: [0.969, 0.031]
tsne output rows: 12

from statistics-ml

How it works

pca_explained_variance(X, k) — the fraction of total variance each of the top k components accounts for (sigma_i^2 / sum(sigma^2) over ALL singular values, not just the retained k — the usual denominator for "variance explained").

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.