ml_gaussian_mixture
ml_gaussian_mixture(
x,
k,
seed=None,
kmeans_plus_plus=True,
max_iterations=1000,
tolerance=1e-08,
)Gaussian mixture model.
Mirrors the C# GaussianMixtureModel class: fits a mixture of k multivariate normals by expectation-maximization, initialized from a k-means fit. It generalizes :func:ml_kmeans by carrying a full covariance per component rather than only a centre.
labels are 0-BASED, as in :func:ml_kmeans. log_likelihood follows the library’s own definition, which OMITS the multivariate-normal normalizing constant -0.5 * x.shape[1] * log(2 * pi) per observation – so it is short of the true mixture log-likelihood by x.shape[0] * x.shape[1] / 2 * log(2 * pi). That constant cancels when comparing two fits of the same data (which is what the library uses it for), but do not feed this value to an information criterion without adding it back. A run that hits max_iterations without converging reports log_likelihood as 0.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| x | array_like | A 2-D array with one row per observation, or a 1-D array. | required |
| k | int | The number of mixture components. | required |
| seed | int | PRNG seed; None uses the computer clock. |
None |
| kmeans_plus_plus | bool | Use k-means++ initialization for the starting fit. | True |
| max_iterations | int | The EM iteration cap. | 1000 |
| tolerance | float | The relative convergence tolerance on the log-likelihood. | 1e-8 |
Returns
| Name | Type | Description |
|---|---|---|
| dict | means (k by p), sigmas (a length-k list of p-by-p covariance arrays), weights, labels (0-based), log_likelihood, and iterations. |
Examples
>>> from corehydropy import ml_gaussian_mixture
>>> x = [1, 1.2, 0.9, 1.1, 1.05, 8, 8.3, 7.9, 8.1, 8.2, 1.0, 8.0]
>>> fit = ml_gaussian_mixture(x, k=2, seed=12345)
>>> len(fit["sigmas"])
2