Implementation:Scikit learn Scikit learn BayesianGaussianMixture
| Knowledge Sources | |
|---|---|
| Domains | Mixture Models, Bayesian Inference |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for variational Bayesian estimation of a Gaussian mixture model provided by scikit-learn.
Description
BayesianGaussianMixture infers an approximate posterior distribution over the parameters of a Gaussian mixture distribution using variational inference. Unlike GaussianMixture, it can automatically determine the effective number of components from the data by using a Dirichlet process or Dirichlet distribution prior on the mixture weights. Components with near-zero weight are effectively pruned. It extends BaseMixture and uses Wishart priors on precision matrices and normal priors on means.
Usage
Use BayesianGaussianMixture when the number of mixture components is unknown and should be inferred from the data, or when you want to incorporate prior knowledge about the mixture distribution. It is well-suited for density estimation, soft clustering, and situations where a fully Bayesian treatment of uncertainty is desirable.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/mixture/_bayesian_mixture.py
Signature
class BayesianGaussianMixture(BaseMixture):
def __init__(
self,
*,
n_components=1,
covariance_type="full",
tol=1e-3,
reg_covar=1e-6,
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,
):
Import
from sklearn.mixture import BayesianGaussianMixture
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| n_components | int | No | Maximum number of mixture components. Effective number may be smaller. Default is 1. |
| covariance_type | str | No | Covariance parameter type: "full", "tied", "diag", or "spherical". Default is "full". |
| tol | float | No | Convergence threshold. Default is 1e-3. |
| reg_covar | float | No | Regularization added to covariance diagonal. Default is 1e-6. |
| max_iter | int | No | Maximum number of variational inference iterations. Default is 100. |
| n_init | int | No | Number of initializations. Default is 1. |
| init_params | str | No | Initialization method: "kmeans", "random", "random_from_data", or "k-means++". Default is "kmeans". |
| weight_concentration_prior_type | str | No | Prior type: "dirichlet_process" or "dirichlet_distribution". Default is "dirichlet_process". |
| weight_concentration_prior | float or None | No | Concentration parameter for the weight distribution prior. Default is None. |
| mean_precision_prior | float or None | No | Precision prior on the mean distribution. Default is None. |
| mean_prior | array-like or None | No | Prior on the mean distribution. Default is None. |
| degrees_of_freedom_prior | float or None | No | Prior degrees of freedom on the covariance Wishart distribution. Default is None. |
| covariance_prior | float, array-like, or None | No | Prior on the covariance distribution. Default is None. |
| random_state | int or RandomState | No | Random state for reproducibility. Default is None. |
| warm_start | bool | No | Whether to use solution of last fit as initialization. Default is False. |
| verbose | int | No | Verbosity level. Default is 0. |
| verbose_interval | int | No | Interval between verbose log messages. Default is 10. |
Outputs
| Name | Type | Description |
|---|---|---|
| weights_ | ndarray of shape (n_components,) | Weight of each mixture component. |
| means_ | ndarray of shape (n_components, n_features) | Mean of each mixture component. |
| covariances_ | ndarray | Covariance of each mixture component (shape depends on covariance_type). |
| precisions_ | ndarray | Precision matrices for each component. |
| precisions_cholesky_ | ndarray | Cholesky decomposition of the precision matrices. |
| converged_ | bool | Whether variational inference converged. |
| n_iter_ | int | Number of iterations performed. |
| lower_bound_ | float | Lower bound value on the model evidence (ELBO). |
| weight_concentration_ | ndarray or tuple | Dirichlet concentration parameters of the weight distribution. |
| mean_precision_ | ndarray of shape (n_components,) | Precision parameter for each component's mean. |
| degrees_of_freedom_ | ndarray of shape (n_components,) | Degrees of freedom of each component's Wishart distribution. |
Usage Examples
Basic Usage
from sklearn.mixture import BayesianGaussianMixture
import numpy as np
X = np.array([[1, 2], [1, 4], [1, 0],
[10, 2], [10, 4], [10, 0]])
bgm = BayesianGaussianMixture(n_components=5, random_state=42).fit(X)
print(bgm.predict(X))
print(bgm.weights_)