Qu

pca

Builtin index · Statistics & ML

result = pca(X, k)

Input: X, k · Output: result

What it is

Projects an N-row, D-column matrix X onto its top k principal components (columns mean-centered first).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = pca(X, k)
X matrixnot documented yet
k matrixnot documented yet
result matrix
Returns the (N, k) projected coordinate matrix.

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(X, k) — projects X (n, d) onto its top k principal components, returning the (n, k) projected data. X's columns are mean-centered first (PCA is defined on centered data); the "hard part" (the actual SVD) is qu_core::linalg ::svd, the same decomposition pinv/ridge already build on. Deterministic — no seed needed, unlike kmeans.

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