GaussianMixture
- class sklearn.mixture.GaussianMixture(n_components=1, *, covariance_type='full', tol=0.001, reg_covar=1e-06, max_iter=100, n_init=, , , , , , , ,
Gaussian Mixture.
Representation of a Gaussian mixture model probability distribution. This class allows to estimate the parameters of a Gaussian mixture distribution.
Read more in the User Guide.
The number of mixture components.
String describing the type of covariance parameters to use. Must be one of:
‘full’: each component has its own general covariance matrix.
‘tied’: all components share the same general covariance matrix.
‘diag’: each component has its own diagonal covariance matrix.
‘spherical’: each component has its own single variance.
For an example of using covariance_type, refer to
Gaussian Mixture Model Selection.
The convergence threshold. EM iterations will stop when the lower bound average gain is below this threshold.
Non-negative regularization added to the diagonal of covariance. Allows to assure that the covariance matrices are all positive.
The number of EM iterations to perform.
The number of initializations to perform. The best results are kept.
The method used to initialize the weights, the means and the precisions. String must be one of:
‘kmeans’ : responsibilities are initialized using kmeans.
‘k-means++’ : use the k-means++ method to initialize.
‘random’ : responsibilities are initialized randomly.
‘random_from_data’ : initial means are randomly selected data points.
The user-provided initial weights.
If it is None, weights are initialized using the init_params method.
The user-provided initial means,
If it is None, means are initialized using the init_params method.
The user-provided initial precisions (inverse of the covariance matrices). If it is None, precisions are initialized using the ‘init_params’ method. The shape depends on ‘covariance_type’:
(n_components,) if 'spherical',
(n_features, n_features) if 'tied',
(n_components, n_features) if 'diag',
(n_components, n_features, n_features
Controls the random seed given to the method chosen to initialize the
parameters (see init_params).
In addition, it controls the generation of random samples from the
fitted distribution (see the method sample).
Pass an int for reproducible output across multiple function calls.
See Glossary.
If ‘warm_start’ is True, the solution of the last fitting is used as initialization for the next call of fit(). This can speed up convergence when fit is called several times on similar problems. In that case, ‘n_init’ is ignored and only a single initialization occurs upon the first call. See the Glossary.
Enable verbose output. If 1 then it prints the current initialization and each iteration step. If greater than 1 then it prints also the log probability and the time needed for each step.
Number of iteration done before the next print.
The weights of each mixture components.
The mean of each mixture component.
The covariance of each mixture component.
The shape depends on covariance_type:
(n_components,) if 'spherical',
(n_features, n_features) if
>>> import numpy as np
>>> from sklearn.mixture import GaussianMixture
>>> X = np.array([[1, 2], [1, 4], [1, 0], [10,
For a comparison of Gaussian Mixture with other clustering algorithms, see Comparing different clustering algorithms on toy datasets.
For an illustration of the negative log-likelihood surface of a
GaussianMixture Model,
see Density Estimation for a Gaussian mixture.
Akaike information criterion for the current model on the input X.
You can refer to this mathematical section for more details regarding the formulation of the AIC used.
The input samples.
The lower the better.
Bayesian information criterion for the current model on the input X.
You can refer to this mathematical section for more details regarding the formulation of the BIC used.
For an example of GMM selection using bic information criterion,
refer to Gaussian Mixture Model Selection.
The input samples.
Estimate model parameters with the EM algorithm.
The method fits the model n_init times and sets the parameters with
which the model has the largest likelihood or lower bound. Within each
trial, the method iterates between E-step and M-step for max_iter
times until the change of likelihood or lower bound is less than
tol, otherwise, a ConvergenceWarning is raised.
If warm_start is True, then is ignored and a single
initialization is performed upon the first call. Upon consecutive
calls, training starts where it left off.
Estimate model parameters using X and predict the labels for X.
The method fits the model n_init times and sets the parameters with
which the model has the largest likelihood or lower bound. Within each
trial, the method iterates between E-step and M-step for max_iter
times until the change of likelihood or lower bound is less than
tol, otherwise, a ConvergenceWarning is
raised. After fitting, it predicts the most probable label for the
input data points.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRequest encapsulating
routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Predict the labels for the data samples in X using trained model.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Component labels.
Evaluate the components’ density for each sample.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Density of each Gaussian component for each sample in X.
Generate random samples from the fitted Gaussian distribution.
Number of samples to generate.
Randomly generated sample.
Compute the per-sample average log-likelihood of the given data X.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Not used, present for API consistency by convention.
Compute the log-likelihood of each sample.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Log-likelihood of each sample in X under the current model.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as Pipeline). The latter have
parameters of the form <component>__<parameter> so that it’s
possible to update each component of a nested object.
Estimator parameters.
Comparing different clustering algorithms on toy datasets
For an example of using covariances, refer to GMM covariances.
The precision matrices for each component in the mixture. A precision
matrix is the inverse of a covariance matrix. A covariance matrix is
symmetric positive definite so the mixture of Gaussian can be
equivalently parameterized by the precision matrices. Storing the
precision matrices instead of the covariance matrices makes it more
efficient to compute the log-likelihood of new samples at test time.
The shape depends on covariance_type:
(n_components,) if 'spherical',
(n_features, n_features) if 'tied',
(n_components, n_features) if 'diag',
(n_components, n_features, n_features) if 'full'
The Cholesky decomposition of the precision matrices of each mixture
component. A precision matrix is the inverse of a covariance matrix.
A covariance matrix is symmetric positive definite so the mixture of
Gaussian can be equivalently parameterized by the precision matrices.
Storing the precision matrices instead of the covariance matrices makes
it more efficient to compute the log-likelihood of new samples at test
time. The shape depends on covariance_type:
(n_components,) if 'spherical',
(n_features, n_features) if 'tied',
(n_components, n_features) if 'diag',
(n_components, n_features, n_features) if 'full'
True when convergence of the best fit of EM was reached, False otherwise.
Number of step used by the best fit of EM to reach the convergence.
Lower bound value on the log-likelihood (of the training data with respect to the model) of the best fit of EM.
n_iter_,)The list of lower bound values on the log-likelihood from each iteration of the best fit of EM.
Number of features seen during fit.
n_features_in_,)Names of features seen during fit. Defined only when X
has feature names that are all strings.
The lower the better.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Not used, present for API consistency by convention.
The fitted mixture.
Added in version 0.20.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Not used, present for API consistency by convention.
Component labels.
Component labels.
Log-likelihood of X under the Gaussian mixture model.
Estimator instance.