Docs · Machine learning
pca: principal component analysis.
Principal component analysis, the equivalent of Spark MLlib's PCA. One paged pass accumulates the feature sums and the sum of outer products; the host forms the population covariance in double precision and diagonalises it with cyclic Jacobi.
- Module
- ml_pca
- Header
- clio_ml/pca.h
- Rows
- features only
What it does
The pass is deterministic in block order, so the covariance and the components are reproducible across cache sizes. Components are sorted by decreasing variance and sign-normalised so that each component's largest-magnitude entry is positive. All nf components are returned; select the leading ones yourself.
Rows
Every column is a feature. clio_ml.COLUMNS["pca"] is 0.
Python
python
import numpy as np, clio_ml as cm
X = np.random.default_rng(0).normal(size=(4096, 8)).astype(np.float32)
r = cm.fit_pca(X)
components = np.asarray(r.components).reshape(r.nf, r.nf) # row k = k-th component
top3 = components[:3]
print(r.explained_variance[:3])
projected = (X - np.asarray(r.mean)) @ top3.Tfit_pca(X, **options).- Options (
PcaOptions):threads(256). - Result
PcaResult:nf,mean(nf),components(nf*nf, row k is the k-th component),explained_variance(nf, descending),stats. - No one-row predictor; project rows with NumPy as above.
C++
cpp
struct PcaOptions {
u32 threads = 256;
ClusterExchange cluster; // global_rows used as n when active
};
struct PcaResult {
u32 nf = 0;
std::vector<float> mean; // nf
std::vector<float> components; // nf*nf
std::vector<float> explained_variance; // nf, descending
VecStats stats;
std::vector<float> Flatten() const;
};
PcaResult FitPca(Dataset &ds, const PcaOptions &opt);
PcaResult FitPcaPlain(const float *data, u64 n_rows, u32 nf, u64 elems_per_page,
u32 nblocks, const PcaOptions &opt);
PcaResult FitPcaReference(/* same arguments */);The ChiMod is module ml_pca.