2010-06-21 16:43:43 +08:00
|
|
|
"""
|
2010-06-25 22:57:40 +08:00
|
|
|
=================================
|
|
|
|
|
Gaussian Mixture Model Ellipsoids
|
|
|
|
|
=================================
|
2010-06-21 16:43:43 +08:00
|
|
|
|
2011-04-15 20:14:24 +08:00
|
|
|
Plot the confidence ellipsoids of a mixture of two gaussians with EM
|
|
|
|
|
and variational dirichlet process.
|
|
|
|
|
|
|
|
|
|
Both models have access to five components with which to fit the
|
|
|
|
|
data. Note that the EM model will necessarily use all five components
|
|
|
|
|
while the DP model will effectively only use as many as are needed for
|
2011-04-24 23:09:40 +08:00
|
|
|
a good fit. This is a property of the Dirichlet Process prior. Here we
|
|
|
|
|
can see that the EM model splits some components arbitrarily, because it
|
|
|
|
|
is trying to fit too many components, while the Dirichlet Process model
|
|
|
|
|
adapts it number of state automatically.
|
2011-04-15 20:14:24 +08:00
|
|
|
|
|
|
|
|
This example doesn't show it, as we're in a low-dimensional space, but
|
|
|
|
|
another advantage of the dirichlet process model is that it can fit
|
|
|
|
|
full covariance matrices effectively even when there are less examples
|
|
|
|
|
per cluster than there are dimensions in the data, due to
|
|
|
|
|
regularization properties of the inference algorithm.
|
2010-06-21 16:43:43 +08:00
|
|
|
"""
|
2010-06-21 20:53:53 +08:00
|
|
|
import itertools
|
|
|
|
|
|
2011-04-24 07:27:08 +08:00
|
|
|
import numpy as np
|
|
|
|
|
from scipy import linalg
|
2010-06-21 20:53:53 +08:00
|
|
|
import pylab as pl
|
|
|
|
|
import matplotlib as mpl
|
2010-06-21 16:43:43 +08:00
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn import mixture
|
2011-04-24 07:27:08 +08:00
|
|
|
|
2011-04-24 23:09:40 +08:00
|
|
|
# Number of samples per component
|
|
|
|
|
n_samples = 500
|
2010-06-21 16:43:43 +08:00
|
|
|
|
2011-04-24 23:09:40 +08:00
|
|
|
# Generate random sample, two components
|
2010-06-21 16:43:43 +08:00
|
|
|
np.random.seed(0)
|
2011-04-24 23:09:40 +08:00
|
|
|
C = np.array([[0., -0.1], [1.7, .4]])
|
|
|
|
|
X = np.r_[np.dot(np.random.randn(n_samples, 2), C),
|
2011-12-24 02:12:26 +08:00
|
|
|
.7 * np.random.randn(n_samples, 2) + np.array([-6, 3])]
|
2010-06-21 16:43:43 +08:00
|
|
|
|
2011-04-24 23:09:40 +08:00
|
|
|
# Fit a mixture of gaussians with EM using five components
|
2011-12-22 07:20:41 +08:00
|
|
|
gmm = mixture.GMM(n_components=5, covariance_type='full')
|
2011-04-24 23:09:40 +08:00
|
|
|
gmm.fit(X)
|
2011-04-15 20:14:24 +08:00
|
|
|
|
2011-04-24 23:09:40 +08:00
|
|
|
# Fit a dirichlet process mixture of gaussians using five components
|
2011-12-22 07:20:41 +08:00
|
|
|
dpgmm = mixture.DPGMM(n_components=5, covariance_type='full')
|
2011-04-24 23:09:40 +08:00
|
|
|
dpgmm.fit(X)
|
2011-04-15 20:14:24 +08:00
|
|
|
|
2011-12-24 02:12:26 +08:00
|
|
|
color_iter = itertools.cycle(['r', 'g', 'b', 'c', 'm'])
|
2010-06-21 20:53:53 +08:00
|
|
|
|
2011-07-20 21:40:52 +08:00
|
|
|
for i, (clf, title) in enumerate([(gmm, 'GMM'),
|
2011-04-24 23:09:40 +08:00
|
|
|
(dpgmm, 'Dirichlet Process GMM')]):
|
2011-12-24 02:12:26 +08:00
|
|
|
splot = pl.subplot(2, 1, 1 + i)
|
2011-04-24 23:09:40 +08:00
|
|
|
Y_ = clf.predict(X)
|
2012-01-10 07:27:23 +08:00
|
|
|
for i, (mean, covar, color) in enumerate(zip(
|
2012-02-02 22:16:26 +08:00
|
|
|
clf.means_, clf._get_covars(), color_iter)):
|
2011-04-24 07:27:08 +08:00
|
|
|
v, w = linalg.eigh(covar)
|
|
|
|
|
u = w[0] / linalg.norm(w[0])
|
2011-04-15 20:14:24 +08:00
|
|
|
# as the DP will not use every component it has access to
|
|
|
|
|
# unless it needs it, we shouldn't plot the redundant
|
|
|
|
|
# components.
|
2011-04-24 23:09:40 +08:00
|
|
|
if not np.any(Y_ == i):
|
2011-04-15 20:14:24 +08:00
|
|
|
continue
|
2011-12-24 02:12:26 +08:00
|
|
|
pl.scatter(X[Y_ == i, 0], X[Y_ == i, 1], .8, color=color)
|
2011-04-24 23:09:40 +08:00
|
|
|
|
|
|
|
|
# Plot an ellipse to show the Gaussian component
|
2011-12-24 02:12:26 +08:00
|
|
|
angle = np.arctan(u[1] / u[0])
|
|
|
|
|
angle = 180 * angle / np.pi # convert to degrees
|
2011-04-24 07:27:08 +08:00
|
|
|
ell = mpl.patches.Ellipse(mean, v[0], v[1], 180 + angle, color=color)
|
2011-04-15 20:14:24 +08:00
|
|
|
ell.set_clip_box(splot.bbox)
|
|
|
|
|
ell.set_alpha(0.5)
|
|
|
|
|
splot.add_artist(ell)
|
2010-06-21 20:53:53 +08:00
|
|
|
|
2011-04-24 23:09:40 +08:00
|
|
|
pl.xlim(-10, 10)
|
|
|
|
|
pl.ylim(-3, 6)
|
2011-05-03 01:15:38 +08:00
|
|
|
pl.xticks(())
|
|
|
|
|
pl.yticks(())
|
2011-04-24 23:09:40 +08:00
|
|
|
pl.title(title)
|
|
|
|
|
|
2010-06-21 20:53:53 +08:00
|
|
|
pl.show()
|