Qu

pca_components

Builtin index · Statistics & ML

result = pca_components(X, k)

Input: X, k · Output: result

What it is

Fits the same PCA as pca on X and k, returning the (k, D) loading matrix instead — each row is a unit-length direction in the original D-dimensional feature space.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = pca_components(X, k)
X matrixnot documented yet
k matrixnot documented yet
result Mat
Returns a (k, D) Mat.

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_components(X, k) — the (k, d) principal-direction matrix (rows are unit loading vectors, scikit-learn's own components_ convention) — a separate call rather than a second return value, same "no tuples, cheaply recompute" reasoning as kmeans_centers, except here it's not just cheap but exactly reproducible with zero extra cost (PCA has no randomness to keep in sync via a seed).

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