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 is the full normalized mixture log likelihood from the latest expectation step, including when the fit reaches max_iterations.

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