mojolearn.BayesianGaussianMixture#
- class mojolearn.BayesianGaussianMixture(*, n_components=1, covariance_type='full', tol=0.001, reg_covar=1e-06, max_iter=100, n_init=1, init_params='kmeans', weight_concentration_prior_type='dirichlet_process', weight_concentration_prior=None, mean_precision_prior=None, mean_prior=None, degrees_of_freedom_prior=None, covariance_prior=None, random_state=None, warm_start=False, verbose=0, verbose_interval=10)[source]#
Bases:
_XClusterVariational Bayesian Gaussian mixture.
Follows scikit-learn’s
BayesianGaussianMixture: variational inference with a Dirichlet process or Dirichlet distribution prior on the weights, for every covariance type. The per-row work runs in float32 and the parameter updates and lower bound in higher precision. Initialization draws from this library’s own seeded random stream, so results agree with scikit-learn’s within a tolerance. Input is converted to float32.- Parameters:
n_components (int, default 1) – Maximum number of mixture components. Must not exceed the number of samples.
covariance_type ({‘full’, ‘tied’, ‘diag’, ‘spherical’}, default ‘full’) – Form of the component covariances.
tol (float, default 1e-3) – Convergence threshold on the lower bound gain.
reg_covar (float, default 1e-6) – Non-negative value added to the covariance diagonals.
max_iter (int, default 100) – Maximum EM iterations per initialization; must be at least 1.
n_init (int, default 1) – Number of initializations; the one with the best lower bound is kept. Must be at least 1.
init_params ({‘kmeans’, ‘random’, ‘k-means++’, ‘random_from_data’}, default ‘kmeans’) – How the responsibilities start. ‘kmeans’ uses this library’s
KMeans.weight_concentration_prior_type ({‘dirichlet_process’, ‘dirichlet_distribution’}, default ‘dirichlet_process’) – Prior on the weights.
weight_concentration_prior (float or None, default None) – Weight concentration prior; None means 1 / n_components.
mean_precision_prior (float or None, default None) – Precision prior on the means; None means 1.
mean_prior (array-like of shape (n_features,) or None, default None) – Prior on the means; None means the mean of
X.degrees_of_freedom_prior (float or None, default None) – Degrees-of-freedom prior; None means n_features.
covariance_prior (float, array-like or None, default None) – Covariance prior: shape (n_features, n_features) for ‘full’ and ‘tied’, (n_features,) for ‘diag’, a float for ‘spherical’. None uses the covariance of
X, as in scikit-learn.random_state (int or None, default None) – Non-negative seed. None means 0, so repeated fits give the same result. A NumPy
RandomStateis not accepted.warm_start (bool, default False) – If True, a refit with the same
n_components, number of features andcovariance_typeresumes from the previous fit’s parameters (one run, the lower bound continuing).verbose (int, default 0) – Accepted for scikit-learn compatibility; nothing is printed.
verbose_interval (int, default 10) – Accepted for scikit-learn compatibility; ignored.
numeric_mode (str or None, default None) – Numeric mode for this estimator. None uses the process default (see
mojolearn.set_numeric_mode()).
- Variables:
weights_ (Array of shape (n_components,), float32) – Component weights.
means_ (Array of shape (n_components, n_features), float32) – Component means.
covariances_ (Array, float32) – Component covariances, shaped by
covariance_typeas in scikit-learn.precisions_cholesky_ (Array, float32) – Cholesky factors of the precision matrices, shaped by
covariance_type.weight_concentration_ (Array or tuple of two Arrays, float32) – Dirichlet parameters of the weights (a pair for the Dirichlet process).
weight_concentration_prior_ (float) – Weight concentration prior used.
mean_precision_ (Array of shape (n_components,), float32) – Precision of each mean.
mean_precision_prior_ (float) – Mean precision prior used.
mean_prior_ (Array of shape (n_features,), float32) – Prior on the means used.
degrees_of_freedom_ (Array of shape (n_components,) or float) – Degrees of freedom of each component (a float for ‘tied’).
degrees_of_freedom_prior_ (float) – Degrees-of-freedom prior used.
covariance_prior_ (Array or float) – Covariance prior used, shaped by
covariance_type.lower_bound_ (float) – Lower bound of the best fit.
n_iter_ (int) – EM iterations of the best fit.
converged_ (bool) – Whether the best fit converged.
n_features_in_ (int) – Number of features seen in
fit.
Examples
>>> import numpy as np, mojolearn >>> X = np.random.default_rng(0).random((1000, 4), dtype=np.float32) >>> model = mojolearn.BayesianGaussianMixture(n_components=5, random_state=0).fit(X) >>> proba = model.predict_proba(X)
- __init__(*, n_components=1, covariance_type='full', tol=0.001, reg_covar=1e-06, max_iter=100, n_init=1, init_params='kmeans', weight_concentration_prior_type='dirichlet_process', weight_concentration_prior=None, mean_precision_prior=None, mean_prior=None, degrees_of_freedom_prior=None, covariance_prior=None, random_state=None, warm_start=False, verbose=0, verbose_interval=10)[source]#
Methods
fit(X[, y])Fit the mixture on
X.fit_predict(X[, y])Fit the mixture on
Xand return the component of each row.get_params([deep])numeric_mode_used()The numeric mode this estimator runs in.
predict(X)Most likely component of each row of
X.Responsibility of each component for each row of
X.score(X[, y])Mean log-likelihood of the rows of
X.Log-likelihood of each row of
Xunder the mixture.set_params(**params)Set constructor parameters.
vendor_used()The GPU backend this estimator runs on.
- fit(X, y=None)[source]#
Fit the mixture on
X.- Parameters:
X (array-like of shape (n_samples, n_features)) – Training rows, converted to float32. NaN or infinity raises ValueError.
y (ignored) – Present for scikit-learn compatibility.
- Returns:
self (BayesianGaussianMixture) – The fitted estimator.
- Raises:
ValueError – If
covariance_type,init_paramsorweight_concentration_prior_typeis unknown,n_componentsexceeds the number of samples,n_initormax_iteris below 1, or a prior has the wrong shape.
- fit_predict(X, y=None)[source]#
Fit the mixture on
Xand return the component of each row.- Parameters:
X (array-like of shape (n_samples, n_features)) – Training rows, converted to float32.
y (ignored) – Present for scikit-learn compatibility.
- Returns:
labels (Array of shape (n_samples,), int32) – Most likely component of each training row.
- predict(X)[source]#
Most likely component of each row of
X.A tie goes to the lowest component index.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Rows to label, converted to float32.
- Returns:
labels (Array of shape (n_samples,), int32) – Component with the largest responsibility for each row.
- Raises:
RuntimeError – If the model is not fitted.
- predict_proba(X)[source]#
Responsibility of each component for each row of
X.- Parameters:
X (array-like of shape (n_samples, n_features)) – Rows to evaluate, converted to float32.
- Returns:
proba (Array of shape (n_samples, n_components), float32) – Posterior probability of each component.
- Raises:
RuntimeError – If the model is not fitted.
- score_samples(X)[source]#
Log-likelihood of each row of
Xunder the mixture.- Parameters:
X (array-like of shape (n_samples, n_features)) – Rows to evaluate, converted to float32.
- Returns:
log_prob (Array of shape (n_samples,), float32) – Weighted log probability of each row.
- Raises:
RuntimeError – If the model is not fitted.
- score(X, y=None)[source]#
Mean log-likelihood of the rows of
X.- Parameters:
X (array-like of shape (n_samples, n_features)) – Rows to evaluate, converted to float32.
y (ignored) – Present for scikit-learn compatibility.
- Returns:
score (float) – Average of
score_samplesover the rows.- Raises:
RuntimeError – If the model is not fitted.