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 responsibilitiesOperations
Fit #
Fits the mixture by EM.
method : public : Fit(X:Float[,]) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| X | Float | data matrix (rows=samples, cols=features) |
Return
| Type | Description |
|---|---|
| Bool | true if fitting succeeded |
GetIterationsRun #
Gets the number of EM iterations actually run.
method : public : GetIterationsRun() ~ IntReturn
| Type | Description |
|---|---|
| Int | iterations run |
GetLogLikelihood #
Gets the final average log-likelihood of the training data.
method : public : GetLogLikelihood() ~ FloatReturn
| Type | Description |
|---|---|
| Float | average log-likelihood |
GetMeans #
Gets the fitted component means (components x features).
method : public : GetMeans() ~ Float[,]Return
| Type | Description |
|---|---|
| Float | mean matrix, or Nil if not fitted |
GetWeights #
Gets the fitted mixture weights.
method : public : GetWeights() ~ Float[]Return
| Type | Description |
|---|---|
| Float | weight array, or Nil if not fitted |
IsFitted #
Whether the mixture has been fitted.
method : public : IsFitted() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if fitted |
Load # function
Loads a fitted mixture from a file.
function : Load(filename:String) ~ GaussianMixtureParameters
| Name | Type | Description |
|---|---|---|
| filename | String | file to load from |
Return
| Type | Description |
|---|---|
| GaussianMixture | fitted mixture, or Nil on failure |
New # constructor
Constructor with default optimization settings (100 iterations, tolerance 0.000001).
New(num_components:Int, seed:Int)Parameters
| Name | Type | Description |
|---|---|---|
| num_components | Int | number of mixture components |
| seed | Int | PRNG seed for reproducible initialization |
New # constructor
Constructor with explicit optimization settings.
New(num_components:Int, max_iterations:Int, tolerance:Float, seed:Int)Parameters
| Name | Type | Description |
|---|---|---|
| num_components | Int | number of mixture components |
| max_iterations | Int | EM iteration cap |
| tolerance | Float | stop when average log-likelihood improves less than this |
| seed | Int | PRNG seed for reproducible initialization |
Predict #
Computes soft component responsibilities for every row.
method : public : Predict(X:Float[,]) ~ Float[,]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | data matrix |
Return
| Type | Description |
|---|---|
| Float | rows x components responsibility matrix, or Nil if not fitted |