GaussianMixtureModel<T>
Models & Types · AiDotNet.Clustering.Probabilistic
Gaussian Mixture Model clustering using Expectation-Maximization.
Inheritance: ClusteringBase<T> → GaussianMixtureModel<T>
Implements: IGeneratedParameterRegistrar<T>
For Beginners
GMM is like soft K-Means.
Instead of saying “this point belongs to cluster 2”, GMM says “this point has 70% chance of cluster 2, 25% chance of cluster 1, 5% chance of cluster 3”.
This is useful when:
- Clusters overlap (points could belong to multiple groups)
- Clusters have different shapes (some wide, some narrow)
- You need uncertainty estimates for cluster assignments
The EM algorithm works by:
- E-step: For each point, estimate probability of belonging to each cluster
- M-step: Update cluster parameters based on these probabilities
- Repeat until stable
How It Works
GMM represents data as a mixture of Gaussian distributions. Each cluster is characterized by its mean, covariance, and mixing weight. The EM algorithm iteratively refines these parameters.
Algorithm steps (EM):
- Initialize means, covariances, and weights
- E-step: Compute responsibility of each component for each point
- M-step: Update parameters to maximize expected log-likelihood
- Repeat until convergence
Example
using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.Clustering.Probabilistic;
using AiDotNet.Tensors.LinearAlgebra;
var data = new Matrix<double>(6, 2);
double[][] rows = { new[] { 1.0, 1.0 }, new[] { 1.2, 0.9 }, new[] { 1.1, 1.1 },
new[] { 8.0, 8.0 }, new[] { 8.2, 7.9 }, new[] { 7.9, 8.1 } };
for (int i = 0; i < 6; i++) { data[i, 0] = rows[i][0]; data[i, 1] = rows[i][1]; }
var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
.ConfigureModel(new GaussianMixtureModel<double>())
.ConfigureDataLoader(DataLoaders.FromMatrix(data))
.BuildAsync();
var labels = result.Predict(data);
Console.WriteLine($"GaussianMixtureModel: clustered {labels.Length} points.");
Constructors
| Constructor | Summary |
|---|---|
GaussianMixtureModel(GMMOptions<T> options) |
Initializes a new GaussianMixtureModel instance. |
Properties
| Property | Summary |
|---|---|
Matrix<T> Means { get; } |
Gets the component means. |
T LowerBound { get; } |
Gets the lower bound (ELBO) from the last training. |
Tensor<T> Covariances { get; } |
Gets the component covariances. |
Vector<T> Weights { get; } |
Gets the mixture weights. |
Methods
| Method | Summary |
|---|---|
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters) |
|
Matrix<T> PredictProba(Matrix<T> x) |
Predicts probability of each component for each sample. |
Matrix<T> Sample(int numSamples) |
Samples from the fitted mixture model. |
ModelOptions GetOptions() |
|
T Score(Matrix<T> x) |
Computes log-likelihood of data under the model. |
Vector<T> FitPredict(Matrix<T> x) |
|
Vector<T> Predict(Matrix<T> x) |
|
void Train(Matrix<T> x, Vector<T> y) |