v2026.6.1
All Bundles
Bundle Core machine learning types: the seedable Random generator, Matrix2D linear algebra and activations, matrix reference holders, the NeuralNetwork and CSV-backed MatrixReader. Compile with -lib ml.

PCA

Principal component analysis via power iteration with deflation on the sample covariance matrix. Deterministic (fixed initialization vector). Components are unit-length rows of a k x d matrix; sign is arbitrary.

Example

pca := PCA->New(1);
pca->Fit(X);
reduced := pca->Transform(X);          # n x 1
ratios := pca->GetExplainedVarianceRatio();

Operations

Fit #

Fits the decomposition: centers the data, builds the sample covariance matrix and extracts the top components by power iteration + deflation.

method : public : Fit(X:Float[,]) ~ Bool

Parameters

NameTypeDescription
XFloatdata matrix (rows=samples, cols=features)

Return

TypeDescription
Booltrue if fitting succeeded

GetComponents #

Gets the fitted components (num_components x features; unit rows).

method : public : GetComponents() ~ Float[,]

Return

TypeDescription
Floatcomponent matrix, or Nil if not fitted

GetExplainedVariance #

Gets the variance explained by each component (eigenvalues).

method : public : GetExplainedVariance() ~ Float[]

Return

TypeDescription
Floatexplained variance array, or Nil if not fitted

GetExplainedVarianceRatio #

Gets the fraction of total variance explained by each component.

method : public : GetExplainedVarianceRatio() ~ Float[]

Return

TypeDescription
Floatratio array, or Nil if not fitted

GetMean #

Gets the per-feature means used for centering.

method : public : GetMean() ~ Float[]

Return

TypeDescription
Floatmean array, or Nil if not fitted

InverseTransform #

Maps projected data back into the original feature space.

method : public : InverseTransform(Z:Float[,]) ~ Float[,]

Parameters

NameTypeDescription
ZFloatprojected matrix (rows x num_components)

Return

TypeDescription
Floatreconstruction (rows x features), or Nil if not fitted

IsFitted #

Whether the decomposition has been fitted.

method : public : IsFitted() ~ Bool

Return

TypeDescription
Booltrue if fitted

Load # function

Loads a fitted decomposition from a file.

function : Load(filename:String) ~ PCA

Parameters

NameTypeDescription
filenameStringfile to load from

Return

TypeDescription
PCAfitted decomposition, or Nil on failure

New # constructor

Constructor

New(num_components:Int)

Parameters

NameTypeDescription
num_componentsIntnumber of principal components to keep

Store #

Saves the fitted decomposition to a file.

method : public : Store(filename:String) ~ Bool

Parameters

NameTypeDescription
filenameStringfile to store to

Return

TypeDescription
Booltrue if successful, false otherwise

Transform #

Projects data onto the fitted components.

method : public : Transform(X:Float[,]) ~ Float[,]

Parameters

NameTypeDescription
XFloatdata matrix (rows=samples, cols=features)

Return

TypeDescription
Floatprojected matrix (rows x num_components), or Nil if not fitted