(n_components,) if 'spherical',
(n_features, n_features) if 'tied',
(n_components, n_features) if 'diag',
(n_components, n_features, n_features) if 'full'
random_stateint, RandomState instance or None, default=NoneControls 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.
warm_startbool, default=FalseIf ‘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.
verboseint, default=0Enable 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.
verbose_intervalint, default=10Number of iteration done before the next print.
Attributes:
weights_array-like of shape (n_components,)The weights of each mixture components.
means_array-like of shape (n_components, n_features)The mean of each mixture component.
covariances_array-likeThe covariance of each mixture component.
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'
For an example of using covariances, refer to
GMM covariances.
precisions_array-likeThe 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'
precisions_cholesky_array-likeThe 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'
converged_boolTrue when convergence of the best fit of EM was reached, False otherwise.
n_iter_intNumber of step used by the best fit of EM to reach the convergence.
lower_bound_floatLower bound value on the log-likelihood (of the training data with
respect to the model) of the best fit of EM.
lower_bounds_array-like of shape (n_iter_
,)The list of lower bound values on the log-likelihood from each
iteration of the best fit of EM.
n_features_in_intNumber of features seen during fit.
Added in version 0.24.
feature_names_in_ndarray of shape (n_features_in_
,)Names of features seen during fit. Defined only when X
has feature names that are all strings.
Added in version 1.0.
>>> import numpy as np
>>> from sklearn.mixture import GaussianMixture
>>> X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
>>> gm = GaussianMixture(n_components=2, random_state=0).fit(X)
>>> gm.means_
array([[10., 2.],
[ 1., 2.]])
>>> gm.predict([[0, 0], [12, 3]])
array([1, 0])
For a comparison of Gaussian Mixture with other clustering algorithms, see
Comparing different clustering algorithms on toy datasets
aic(X)[source]
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.
Parameters:
Xarray of shape (n_samples, n_dimensions)The input samples.
Returns:
aicfloatThe lower the better.
bic(X)[source]
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.
Parameters:
Xarray of shape (n_samples, n_dimensions)The input samples.
Returns:
bicfloatThe lower the better.
fit(X, y=None)[source]
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 n_init
is ignored and a single
initialization is performed upon the first call. Upon consecutive
calls, training starts where it left off.
Parameters:
Xarray-like of shape (n_samples, n_features)List of n_features-dimensional data points. Each row
corresponds to a single data point.
yIgnoredNot used, present for API consistency by convention.
Returns:
selfobjectThe fitted mixture.
fit_predict(X, y=None)[source]
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.
Added in version 0.20.
Parameters:
Xarray-like of shape (n_samples, n_features)List of n_features-dimensional data points. Each row
corresponds to a single data point.
yIgnoredNot used, present for API consistency by convention.
Returns:
labelsarray, shape (n_samples,)Component labels.
get_metadata_routing()[source]
Get metadata routing of this object.
Please check User Guide on how the routing
mechanism works.
Returns:
routingMetadataRequestA MetadataRequest
encapsulating
routing information.
Parameters:
deepbool, default=TrueIf True, will return the parameters for this estimator and
contained subobjects that are estimators.
Returns:
paramsdictParameter names mapped to their values.
predict(X)[source]
Predict the labels for the data samples in X using trained model.
Parameters:
Xarray-like of shape (n_samples, n_features)List of n_features-dimensional data points. Each row
corresponds to a single data point.
Returns:
labelsarray, shape (n_samples,)Component labels.
Parameters:
Xarray-like of shape (n_samples, n_features)List of n_features-dimensional data points. Each row
corresponds to a single data point.
Returns:
resparray, shape (n_samples, n_components)Density of each Gaussian component for each sample in X.
sample(n_samples=1)[source]
Generate random samples from the fitted Gaussian distribution.
Parameters:
n_samplesint, default=1Number of samples to generate.
Returns:
Xarray, shape (n_samples, n_features)Randomly generated sample.
yarray, shape (nsamples,)Component labels.
score(X, y=None)[source]
Compute the per-sample average log-likelihood of the given data X.
Parameters:
Xarray-like of shape (n_samples, n_dimensions)List of n_features-dimensional data points. Each row
corresponds to a single data point.
yIgnoredNot used, present for API consistency by convention.
Returns:
log_likelihoodfloatLog-likelihood of X
under the Gaussian mixture model.
Parameters:
Xarray-like of shape (n_samples, n_features)List of n_features-dimensional data points. Each row
corresponds to a single data point.
Returns:
log_probarray, shape (n_samples,)Log-likelihood of each sample in X
under the current model.
set_params(**params)[source]
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.
Parameters:
**paramsdictEstimator parameters.
Returns:
selfestimator instanceEstimator instance.
Comparing different clustering algorithms on toy datasets
Comparing different clustering algorithms on toy datasets
Demonstration of k-means assumptions
Demonstration of k-means assumptions
Gaussian Mixture Model Ellipsoids
Gaussian Mixture Model Ellipsoids
GMM covariances
GMM covariances
GMM Initialization Methods
GMM Initialization Methods
Density Estimation for a Gaussian mixture
Density Estimation for a Gaussian mixture
Gaussian Mixture Model Selection
Gaussian Mixture Model Selection
Gaussian Mixture Model Sine Curve
Gaussian Mixture Model Sine Curve