Skip to content
Snippets Groups Projects
Commit f2fc81e4 authored by Alexandre Passos's avatar Alexandre Passos
Browse files

Editing a single example for the GMM and DPGMM explaining the difference

parent 98314020
No related branches found
No related tags found
No related merge requests found
"""
=================================
DP Mixture Model Ellipsoids
=================================
Plot the covariance ellipsoids of a dirichlet process mixture of two
gaussians for varying values of the alpha parameter.
Note that we generate the data from two components, which are
correctly recovered by the dirichlet process, even though the
approximating distribution is truncated at five components.
"""
import numpy as np
from scikits.learn import mixture
import itertools
import pylab as pl
import matplotlib as mpl
n, m = 200, 2
# generate random sample, two components
np.random.seed(0)
C = np.array([[0., -0.7], [3.5, .7]])
X = np.r_[np.dot(np.random.randn(n, 2), C),
np.random.randn(n, 2) + np.array([3, 3])]
for p, alpha in enumerate([0.01, 1.]):
# fit a five-component dirichlet process mixture model on the
# data.
clf = mixture.DPGMM(n_states=5, cvtype='diag', alpha=alpha)
clf.fit(X)
splot = pl.subplot(311 + p, aspect='equal')
color_iter = itertools.cycle(['r', 'g', 'b', 'c', 'm', 'y'])
Y_ = clf.predict(X)
for i, (mean, covar, color) in enumerate(zip(clf.means,
clf.covars,
color_iter)):
v, w = np.linalg.eigh(covar)
u = w[0] / np.linalg.norm(w[0])
# as the DP will not use every component it has access to
# unless it needs it, we shouldn't plot the redundant
# components.
if not sum(Y_ == i) > 1:
continue
pl.scatter(X[Y_ == i, 0], X[Y_ == i, 1], .8, color=color)
angle = np.arctan(u[1] / u[0])
angle = 180 * angle / np.pi # convert to degrees
ell = mpl.patches.Ellipse(mean, v[0], v[1], 180 + angle, color=color)
ell.set_clip_box(splot.bbox)
ell.set_alpha(0.5)
splot.add_artist(ell)
pl.show()
......@@ -3,7 +3,19 @@
Gaussian Mixture Model Ellipsoids
=================================
Plot the confidence ellipsoids of a mixture of two gaussians.
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
a good fit. This is a property of the Dirichlet Process prior.
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.
"""
import numpy as np
......@@ -21,17 +33,29 @@ C = np.array([[0., -0.7], [3.5, .7]])
X = np.r_[np.dot(np.random.randn(n, 2), C),
np.random.randn(n, 2) + np.array([3, 3])]
clf = mixture.GMM(n_states=2, cvtype='full')
# fit a mixture of gaussians with EM using five components
clf = mixture.GMM(n_states=5, cvtype='diag')
clf.fit(X)
splot = pl.subplot(111, aspect='equal')
color_iter = itertools.cycle (['r', 'g', 'b', 'c'])
# fit a dirichlet process mixture of gaussians using five components
dpclf = mixture.DPGMM(n_states=5, cvtype='diag')
dpclf.fit(X)
color_iter = itertools.cycle (['r', 'g', 'b', 'c', 'm'])
Y_ = clf.predict(X)
for i, (mean, covar, color) in enumerate(zip(clf.means, clf.covars, color_iter)):
for i,c in enumerate([clf, dpclf]):
splot = pl.subplot(211+i, aspect='equal')
Y_ = c.predict(X)
for i, (mean, covar, color) in enumerate(zip(c.means, c.covars, color_iter)):
v, w = np.linalg.eigh(covar)
u = w[0] / np.linalg.norm(w[0])
# as the DP will not use every component it has access to
# unless it needs it, we shouldn't plot the redundant
# components.
if not sum(Y_ == i) > 1:
continue
pl.scatter(X[Y_==i, 0], X[Y_==i, 1], .8, color=color)
angle = np.arctan(u[1]/u[0])
angle = 180 * angle / np.pi # convert to degrees
......@@ -40,5 +64,8 @@ for i, (mean, covar, color) in enumerate(zip(clf.means, clf.covars, color_iter))
ell.set_alpha(0.5)
splot.add_artist(ell)
# Note that the GMM will use all components it has access to, while
# the dirichlet process model will only use as many are needed to
# explain the data
pl.show()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment