Overview
Principal Components is a dimensionality reduction technique that utilizes the covariance.
Centering
Most often in principal components analysis, the data is preprocessed to be centered at the origin. This can easily be accomplised by using the center function of the moments library.
let mt = await import('/lib/statistics/moments/v1.0.0/moments.mjs');
let data = [
[1,2,3],
[2,5,3],
[6,3,4],
[3,3,3],
[1,4,2],
[7,8,4],
];
let centered = mt.center(data);
Covariance
The next step of the principal components analysis is to compute the covariance matrix of the dataset.
let mt = await import('/lib/statistics/moments/v1.0.0/moments.mjs');
let data = [
[1,2,3],
[2,5,3],
[6,3,4],
[3,3,3],
[1,4,2],
[7,8,4],
];
let centered = mt.center(data);
let covariance = mt.covariance(centered);
Eigen Values and Eigen Vectors
let la = await import('/lib/linear-algebra/v1.0.0/linear-algebra.mjs');
let mt = await import('/lib/statistics/moments/v1.0.0/moments.mjs');
let data = [
[1,2,3],
[2,5,3],
[6,3,4],
[3,3,3],
[1,4,2],
[7,8,4],
];
let centered = mt.center(data);
let covariance = mt.covariance(centered);
let eig = la.eigenvectors(covariance);
let eigenvalues = eig.map(p=>p.eigenvalue);
let vectors = eig.map(p=>p.eigenvector);
Try it!
Visualization
A sample dataset.
The principal component analysis effectively rotates the axes.