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) andN3/N4forkmeansinitialization across CPU (Rayon), CUDA,wgpu, and Google TPU. - Incremental learning (
partial_fit): Not supported. - Persistence: Supports binary
.ditserialization viasave(path),GaussianMixture.load(path, device=...),ochreml.save(), andochreml.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
| Parameter | Type | Default | Description |
|---|---|---|---|
n_components | int | 1 | Number of Gaussian mixture components. |
covariance_type | {"full", "tied", "diag", "spherical"} | "full" | Covariance parameterization shared or per-component. |
tol | float | 1e-3 | Convergence threshold on the average lower-bound log-likelihood gain. |
reg_covar | float | 1e-6 | Non-negative regularization added to the diagonal of covariance matrices for numerical stability. |
max_iter | int | 100 | Maximum number of EM iterations per initialization. |
n_init | int | 1 | Number 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_state | int | None | None | Random seed controlling initialization and sample(). |
device | str | None | None | Target compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type | Description |
|---|---|---|
weights_ | ndarray of shape (n_components,) | None | Mixing weights of each Gaussian component. |
means_ | ndarray of shape (n_components, n_features) | None | Mean vector of each component. |
covariances_ | ndarray | None | Component 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 | None | Cholesky decomposition of the precision matrices, with the same shape as covariances_. |
converged_ | bool | Whether the EM algorithm converged within max_iter. |
n_iter_ | int | Number of EM iterations performed in the best fit. |
lower_bound_ | float | None | Best per-sample average log-likelihood lower bound reached during EM. |
n_features_in_ | int | None | Number of features seen during fit. |
device_ | str | None | Resolved hardware device string used by the underlying Rust model. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y=None) -> GaussianMixture | Estimate mixture parameters on X using EM. |
fit_predict | fit_predict(X, y=None) -> ndarray | Fit the model and return int64 component labels for X. |
predict | predict(X) -> ndarray | Predict the most likely component label (int64) for each sample in X. |
predict_proba | predict_proba(X) -> ndarray | Return posterior component responsibilities of shape (n_samples, n_components). |
score_samples | score_samples(X) -> ndarray | Return per-sample log-likelihoods of shape (n_samples,). |
score | score(X, y=None) -> float | Return the average per-sample log-likelihood of X. |
aic | aic(X) -> float | Compute Akaike Information Criterion on X (lower is better). |
bic | bic(X) -> float | Compute Bayesian Information Criterion on X (lower is better). |
sample | sample(n_samples=1) -> tuple[ndarray, ndarray] | Draw (X_samples, y_components) randomly from the fitted mixture. |
save | save(path: str) -> None | Save the fitted model to a binary .dit file. |
load | GaussianMixture.load(path: str, device: str | None = None) -> GaussianMixture | Load a fitted model from a binary .dit file. |
to | to(device: str) -> GaussianMixture | Move 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)