Skip to Content
OchreML
Clustering, Neighbors & NB

GaussianMixture

Probabilistic Gaussian Mixture Model estimated via Expectation-Maximization with Cholesky precision factors.

Overview & Backend Support

GaussianMixture models a data distribution as a weighted mixture of n_components multivariate Gaussian distributions. Parameters are estimated via Expectation-Maximization (EM) with Cholesky precision factors, supporting "full", "tied", "diag", and "spherical" covariance structures, density evaluation (score_samples, score), model selection (aic, bic), and generative sampling (sample).

  • Backend primitives: Dense linear algebra (E1-E5) and N3/N4 for kmeans initialization across CPU (Rayon), CUDA, wgpu, and Google TPU.
  • Incremental learning (partial_fit): Not supported.
  • Persistence: Supports binary .dit serialization via save(path), GaussianMixture.load(path, device=...), ochreml.save(), and ochreml.load().

Import & Signature

from ochreml.mixture import GaussianMixture
# or lazily from top-level:
from ochreml import GaussianMixture

GaussianMixture(
    n_components: int = 1,
    covariance_type: str = "full",
    tol: float = 1e-3,
    reg_covar: float = 1e-6,
    max_iter: int = 100,
    n_init: int = 1,
    init_params: str = "kmeans",
    random_state: int | None = None,
    device: str | None = None,
)

Parameters

ParameterTypeDefaultDescription
n_componentsint1Number of Gaussian mixture components.
covariance_type{"full", "tied", "diag", "spherical"}"full"Covariance parameterization shared or per-component.
tolfloat1e-3Convergence threshold on the average lower-bound log-likelihood gain.
reg_covarfloat1e-6Non-negative regularization added to the diagonal of covariance matrices for numerical stability.
max_iterint100Maximum number of EM iterations per initialization.
n_initint1Number of initializations to perform; the fit with the highest lower bound is kept.
init_params{"kmeans", "random"}"kmeans"Strategy for initializing component weights, means, and precisions.
random_stateint | NoneNoneRandom seed controlling initialization and sample().
devicestr | NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
weights_ndarray of shape (n_components,) | NoneMixing weights of each Gaussian component.
means_ndarray of shape (n_components, n_features) | NoneMean vector of each component.
covariances_ndarray | NoneComponent covariances; shape depends on covariance_type: (k, p, p) for "full", (p, p) for "tied", (k, p) for "diag", (k,) for "spherical".
precisions_cholesky_ndarray | NoneCholesky decomposition of the precision matrices, with the same shape as covariances_.
converged_boolWhether the EM algorithm converged within max_iter.
n_iter_intNumber of EM iterations performed in the best fit.
lower_bound_float | NoneBest per-sample average log-likelihood lower bound reached during EM.
n_features_in_int | NoneNumber of features seen during fit.
device_str | NoneResolved hardware device string used by the underlying Rust model.

Methods

MethodSignatureDescription
fitfit(X, y=None) -> GaussianMixtureEstimate mixture parameters on X using EM.
fit_predictfit_predict(X, y=None) -> ndarrayFit the model and return int64 component labels for X.
predictpredict(X) -> ndarrayPredict the most likely component label (int64) for each sample in X.
predict_probapredict_proba(X) -> ndarrayReturn posterior component responsibilities of shape (n_samples, n_components).
score_samplesscore_samples(X) -> ndarrayReturn per-sample log-likelihoods of shape (n_samples,).
scorescore(X, y=None) -> floatReturn the average per-sample log-likelihood of X.
aicaic(X) -> floatCompute Akaike Information Criterion on X (lower is better).
bicbic(X) -> floatCompute Bayesian Information Criterion on X (lower is better).
samplesample(n_samples=1) -> tuple[ndarray, ndarray]Draw (X_samples, y_components) randomly from the fitted mixture.
savesave(path: str) -> NoneSave the fitted model to a binary .dit file.
loadGaussianMixture.load(path: str, device: str | None = None) -> GaussianMixtureLoad a fitted model from a binary .dit file.
toto(device: str) -> GaussianMixtureMove estimator compute target to another hardware device.

Example

import numpy as np
from ochreml.mixture import GaussianMixture

rng = np.random.default_rng(7)
X = np.vstack([
    rng.normal(loc=[-2.0, -2.0], scale=0.4, size=(50, 2)),
    rng.normal(loc=[3.0, 3.0], scale=0.5, size=(50, 2)),
])

gmm = GaussianMixture(n_components=2, covariance_type="full", random_state=7)
gmm.fit(X)

print("Converged:", gmm.converged_, "Iterations:", gmm.n_iter_)
print("Means:\n", gmm.means_)
print("AIC:", gmm.aic(X), "BIC:", gmm.bic(X))
X_gen, y_gen = gmm.sample(n_samples=4)
print("Generated samples shape:", X_gen.shape, "Components:", y_gen)

On this page