"Demonstration of several covariances types for Gaussian mixture models (GMM).\n",
"\n",
"Demonstration of several covariances types for Gaussian mixture models.\n",
"See the [sklearn guide on GMM](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture) for more information on the estimator.\n",
"\n",
"See https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture for more information on the estimator.\n",
"We apply GMM on the iris dataset. This dataset is four-dimensional, but only the first two\n",
"dimensions are shown here, and thus some points are separated in other\n",
"dimensions.\n",
"We plot the shape of the estimated clusters and the AIC/BIC criteria to estimate the optimal number of clusters.\n",
"Note that we initialize the means of the Gaussians with the means of the true classes from the training set to make\n",
"this comparison valid for several covariances types for GMM.\n",
"\n",
"Although GMM are often used for clustering, we can compare the obtained\n",
"<!--Although GMM are often used for clustering, we can compare the obtained\n",
"clusters with the actual classes from the dataset. We initialize the means\n",
"of the Gaussians with the means of the classes from the training set to make\n",
"this comparison valid.\n",
...
...
@@ -35,7 +40,8 @@
"On the plots, train data is shown as dots, while test data is shown as\n",
"crosses. The iris dataset is four-dimensional. Only the first two\n",
"dimensions are shown here, and thus some points are separated in other\n",
"dimensions.\n",
"dimensions.-->\n",
"\n",
"\n"
]
},
...
...
@@ -43,8 +49,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This is a simplified version of the code that can be found here:\n",
"This is a simplified version of the code of this [sklearn example](https://scikit-learn.org/stable/auto_examples/mixture/plot_gmm_covariances.html#sphx-glr-auto-examples-mixture-plot-gmm-covariances-py)."
]
},
{
...
...
@@ -101,8 +106,13 @@
"outputs": [],
"source": [
"# Try GMMs using full covariance (no constraints imposed on cov)\n",
"- Comment on the number of clusters estimated with the BIC and AIC criteria respectively.\n",
"- Change the shape of the clusters by setting some constraints on the GMM covariances (`tied` for a covariance common to all clusters, `diagonal`, or `spherical` for a covariance proportional to the identity matrix, see cell 3) and re-run the estimation/visualization/model selection cells. Comment on the cluster shapes and the AIC/BIC criterion obtained."
]
}
],
"metadata": {
...
...
%% Cell type:markdown id: tags:
This notebook can be run on mybinder: [](https://mybinder.org/v2/git/https%3A%2F%2Fgricad-gitlab.univ-grenoble-alpes.fr%2Fchatelaf%2Fml-sicom3a/master?urlpath=lab/tree/notebooks/7_EM_iris_data_example/)
%% Cell type:markdown id: tags:
# GMM covariances
Demonstration of several covariances types for Gaussian mixture models (GMM).
Demonstration of several covariances types for Gaussian mixture models.
See the [sklearn guide on GMM](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture) for more information on the estimator.
See https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture for more information on the estimator.
We apply GMM on the iris dataset. This dataset is four-dimensional, but only the first two
dimensions are shown here, and thus some points are separated in other
dimensions.
We plot the shape of the estimated clusters and the AIC/BIC criteria to estimate the optimal number of clusters.
Note that we initialize the means of the Gaussians with the means of the true classes from the training set to make
this comparison valid for several covariances types for GMM.
Although GMM are often used for clustering, we can compare the obtained
<!--Although GMM are often used for clustering, we can compare the obtained
clusters with the actual classes from the dataset. We initialize the means
of the Gaussians with the means of the classes from the training set to make
this comparison valid.
We plot predicted labels on both training and held out test data using a
variety of GMM covariance types on the iris dataset.
We compare GMMs with spherical, diagonal, full, and tied covariance
matrices in increasing order of performance. Although one would
expect full covariance to perform best in general, it is prone to
overfitting on small datasets and does not generalize well to held out
test data.
On the plots, train data is shown as dots, while test data is shown as
crosses. The iris dataset is four-dimensional. Only the first two
dimensions are shown here, and thus some points are separated in other
dimensions.
dimensions.-->
%% Cell type:markdown id: tags:
This is a simplified version of the code that can be found here:
This is a simplified version of the code of this [sklearn example](https://scikit-learn.org/stable/auto_examples/mixture/plot_gmm_covariances.html#sphx-glr-auto-examples-mixture-plot-gmm-covariances-py).
%% Cell type:code id: tags:
``` python
importmatplotlibasmpl
importmatplotlib.pyplotasplt
importnumpyasnp
fromsklearnimportdatasets
fromsklearn.mixtureimportGaussianMixture
fromsklearn.model_selectionimportStratifiedKFold
%matplotlibinline
```
%% Cell type:markdown id: tags:
# Iris data - EM clustering
Note that while labels are available for this data set, they will not be used in GMM identification.
%% Cell type:code id: tags:
``` python
iris=datasets.load_iris()
X_train=iris.data
y_train=iris.target
n_classes=len(
np.unique(y_train)
)# list the unique elements in y_train : nb of different labels
```
%% Cell type:markdown id: tags:
## GMM model estimation
%% Cell type:code id: tags:
``` python
# Try GMMs using full covariance (no constraints imposed on cov)
- Comment on the number of clusters estimated with the BIC and AIC criteria respectively.
- Change the shape of the clusters by setting some constraints on the GMM covariances (`tied` for a covariance common to all clusters, `diagonal`, or `spherical` for a covariance proportional to the identity matrix, see cell 3) and re-run the estimation/visualization/model selection cells. Comment on the cluster shapes and the AIC/BIC criterion obtained.