mojolearn.GaussianMixture#
- class mojolearn.GaussianMixture(n_components=1, covariance_type='full', tol=0.001, reg_covar=1e-06, max_iter=100, init_params='kmeans', random_state=0, n_init=1, warm_start=False, means_init=None, weights_init=None, precisions_init=None)[source]#
Bases:
NumericModeMixinGaussian mixture model fitted by expectation-maximization.
Follows scikit-learn’s
GaussianMixture: every parameter has the same name, meaning and default. Iterates until the change in the lower bound is belowtolormax_iteris reached.Xis read as float32 and the fitted arrays are float32.The default configuration (
covariance_type='full',init_params='kmeans'or'random',n_init=1, no warm start and no initial arrays) runs on mojolearn’s main mixture solver. Any other option runs on a second solver; a model fitted that way can score and predict, butsaveandsampleraiseNotImplementedError.- Parameters:
n_components (int, default 1) – Number of mixture components.
covariance_type ({‘full’, ‘tied’, ‘diag’, ‘spherical’}, default ‘full’) – Form of each component’s covariance, as in scikit-learn.
tol (float, default 1e-3) – Convergence threshold on the change in the lower bound.
reg_covar (float, default 1e-6) – Added to the diagonal of each covariance.
max_iter (int, default 100) – Maximum number of EM iterations.
init_params ({‘kmeans’, ‘random’, ‘k-means++’, ‘random_from_data’}, default ‘kmeans’) – How the responsibilities are initialized.
random_state (int, default 0) – Seed for initialization and for
sample.n_init (int, default 1) – Number of initializations; the best one is kept.
warm_start (bool, default False) – Start from the previous fit’s solution when the shape and covariance type match.
means_init (array-like of shape (n_components, n_features), default None) – Initial means.
weights_init (array-like of shape (n_components,), default None) – Initial weights.
precisions_init (array-like, default None) – Initial precisions, shaped by
covariance_typeas in scikit-learn.
- Variables:
weights_ (Array of shape (n_components,), float32) – Weight of each component.
means_ (Array of shape (n_components, n_features), float32) – Mean of each component.
covariances_ (Array, float32) – Covariance of each component, shaped by
covariance_type; for ‘full’,(n_components, n_features, n_features). Includesreg_covaron the diagonal, as scikit-learn’s does.precisions_cholesky_ (Array, float32) – Cholesky factors of the precision matrices, shaped like
covariances_.log_det_chol_ (Array of shape (n_components,), float32) – Log-determinant of each precision Cholesky factor.
n_iter_ (int) – Number of EM iterations run.
converged_ (bool) – False when the fit stopped at
max_iterbefore reachingtol. scikit-learn also warns in that case; mojolearn only sets this flag.lower_bound_ (float) – Lower bound on the log likelihood at the end of the fit.
n_features_in_ (int) – Number of features seen during
fit.
Examples
>>> import numpy as np, mojolearn >>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32) >>> gmm = mojolearn.GaussianMixture(n_components=3).fit(X) >>> labels = gmm.predict(X) >>> proba = gmm.predict_proba(X) >>> X_new, y_new = gmm.sample(100)
- __init__(n_components=1, covariance_type='full', tol=0.001, reg_covar=1e-06, max_iter=100, init_params='kmeans', random_state=0, n_init=1, warm_start=False, means_init=None, weights_init=None, precisions_init=None)[source]#
Methods
aic(X)Return the Akaike information criterion of the model on
X.bic(X)Return the Bayesian information criterion of the model on
X.fit(X[, y])Fit the mixture to
Xby expectation-maximization.fit_predict(X[, y])Fit the model, then return
predict(X).get_params([deep])load(path)Load a model written by
save.numeric_mode_used()The numeric mode this estimator runs in.
predict(X)Return the most likely component for each row of
X.Return the posterior probability of each component for each row.
sample([n_samples])Draw random rows from the fitted mixture.
save(path)Save the fitted model to
pathas an.npzfile.score(X[, y])Return the mean log likelihood of the rows of
X.Return the log likelihood of each row of
Xunder the model.set_params(**params)Set constructor parameters.
vendor_used()The GPU backend this estimator runs on.
- fit(X, y=None)[source]#
Fit the mixture to
Xby expectation-maximization.Runs until
abs(lower_bound - previous) < tolormax_iteriterations.- Parameters:
X (array-like of shape (n_samples, n_features)) – Training data, read as float32.
y (ignored) – Present for scikit-learn API compatibility.
- Returns:
self (GaussianMixture) – The fitted estimator.
An unknown
covariance_typeorinit_params, or a component thatcollapses during the fit, raises an error naming the problem; the fit
never returns a model with a reset component.
- Raises:
ValueError – For a non-positive
n_componentsor initial arrays of the wrong shape.TypeError – If
n_componentsis not an int ortol/reg_covaris not a number.
- score_samples(X)[source]#
Return the log likelihood of each row of
Xunder the model.- Parameters:
X (array-like of shape (n_samples, n_features))
- Returns:
log_likelihood (Array of shape (n_samples,), float32)
- predict_proba(X)[source]#
Return the posterior probability of each component for each row.
- Parameters:
X (array-like of shape (n_samples, n_features))
- Returns:
proba (Array of shape (n_samples, n_components), float32)
- predict(X)[source]#
Return the most likely component for each row of
X.A tie goes to the lowest component index.
- Parameters:
X (array-like of shape (n_samples, n_features))
- Returns:
labels (Array of shape (n_samples,), int32)
- fit_predict(X, y=None)[source]#
Fit the model, then return
predict(X).- Returns:
labels (Array of shape (n_samples,), int32)
- save(path)[source]#
Save the fitted model to
pathas an.npzfile.The file holds every fitted array plus
n_iter_,converged_andlower_bound_. Load it withload, or withmojolearn.host_model(path)to score on a machine with no GPU.- Parameters:
path (str or path-like) – Destination file.
- Raises:
RuntimeError – If the model is not fitted.
NotImplementedError – If the model was fitted with options outside the default configuration (see the class description).
- classmethod load(path)[source]#
Load a model written by
save.Arrays keep their saved dtypes. The result supports
score_samples,predict_proba,predict,score,bicandaic.- Parameters:
path (str or path-like) – File written by
GaussianMixture.save.- Returns:
model (GaussianMixture)
- Raises:
ValueError – If the file is of another format or estimator, or is truncated or inconsistent.
- score(X, y=None)[source]#
Return the mean log likelihood of the rows of
X.- Parameters:
X (array-like of shape (n_samples, n_features))
y (ignored) – Present for scikit-learn API compatibility.
- Returns:
score (float)
- bic(X)[source]#
Return the Bayesian information criterion of the model on
X.Lower is better.
- Parameters:
X (array-like of shape (n_samples, n_features))
- Returns:
bic (float)
- aic(X)[source]#
Return the Akaike information criterion of the model on
X.Lower is better.
- Parameters:
X (array-like of shape (n_samples, n_features))
- Returns:
aic (float)
- sample(n_samples=1)[source]#
Draw random rows from the fitted mixture.
As in scikit-learn, the number of rows per component is a multinomial draw over
weights_, and the rows come out grouped by component in ascending order. The draws are keyed byrandom_state, so the same model andrandom_stategive the same rows on every call and every GPU vendor; they differ from scikit-learn’s draws.Xis float32 andyint32, where scikit-learn returns float64 and int64.- Parameters:
n_samples (int, default 1) – Number of rows to draw. Must be at least 1.
- Returns:
X (Array of shape (n_samples, n_features), float32) – The drawn rows.
y (Array of shape (n_samples,), int32) – The component each row was drawn from.
- Raises:
ValueError – If the model is not fitted or
random_stateis not an int in[0, 2**64).TypeError – If
n_samplesis not an int.NotImplementedError – If the model was fitted with options outside the default configuration.