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.

GaussianMixture

Gaussian mixture model with diagonal covariance, fitted by expectation- maximization. Component means are seeded k-means++-style with the seedable System.ML.Random for reproducible fits; the E-step works in the log domain for numerical stability and variances are floored. Training stops when the average log-likelihood improves by less than the tolerance.

Example

gmm := GaussianMixture->New(2, 7);
gmm->Fit(X);
components := gmm->PredictClass(X);     # hard assignments
resp := gmm->Predict(X);                # soft responsibilities

Operations

Fit #

Fits the mixture by EM.

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

Parameters

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

Return

TypeDescription
Booltrue if fitting succeeded

GetIterationsRun #

Gets the number of EM iterations actually run.

method : public : GetIterationsRun() ~ Int

Return

TypeDescription
Intiterations run

GetLogLikelihood #

Gets the final average log-likelihood of the training data.

method : public : GetLogLikelihood() ~ Float

Return

TypeDescription
Floataverage log-likelihood

GetMeans #

Gets the fitted component means (components x features).

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

Return

TypeDescription
Floatmean matrix, or Nil if not fitted

GetWeights #

Gets the fitted mixture weights.

method : public : GetWeights() ~ Float[]

Return

TypeDescription
Floatweight array, or Nil if not fitted

IsFitted #

Whether the mixture has been fitted.

method : public : IsFitted() ~ Bool

Return

TypeDescription
Booltrue if fitted

Load # function

Loads a fitted mixture from a file.

function : Load(filename:String) ~ GaussianMixture

Parameters

NameTypeDescription
filenameStringfile to load from

Return

TypeDescription
GaussianMixturefitted mixture, or Nil on failure

New # constructor

Constructor with default optimization settings (100 iterations, tolerance 0.000001).

New(num_components:Int, seed:Int)

Parameters

NameTypeDescription
num_componentsIntnumber of mixture components
seedIntPRNG seed for reproducible initialization

New # constructor

Constructor with explicit optimization settings.

New(num_components:Int, max_iterations:Int, tolerance:Float, seed:Int)

Parameters

NameTypeDescription
num_componentsIntnumber of mixture components
max_iterationsIntEM iteration cap
toleranceFloatstop when average log-likelihood improves less than this
seedIntPRNG seed for reproducible initialization

Predict #

Computes soft component responsibilities for every row.

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

Parameters

NameTypeDescription
XFloatdata matrix

Return

TypeDescription
Floatrows x components responsibility matrix, or Nil if not fitted

PredictClass #

Predicts the most likely component for every row.

method : public : PredictClass(X:Float[,]) ~ Int[]

Parameters

NameTypeDescription
XFloatdata matrix

Return

TypeDescription
Intper-row component ids, or Nil if not fitted

Store #

Saves the fitted mixture to a file.

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

Parameters

NameTypeDescription
filenameStringfile to store to

Return

TypeDescription
Booltrue if successful, false otherwise