Plot the confidence ellipsoids of a mixture of two gaussians.
Source code: plot_gmm_pdf.py
import numpy as np
from scikits.learn import gmm
import itertools
import pylab as pl
import matplotlib as mpl
import matplotlib.pyplot as plt
n, m = 300, 2
# generate random sample, two components
np.random.seed(0)
C = np.array([[0., -0.7], [3.5, .7]])
X_train = np.r_[np.dot(np.random.randn(n, 2), C),
np.random.randn(n, 2) + np.array([20, 20])]
clf = gmm.GMM(2, cvtype='full')
clf.fit(X_train)
x = np.linspace(-20.0, 30.0)
y = np.linspace(-20.0, 40.0)
X, Y = np.meshgrid(x, y)
XX = np.c_[X.ravel(), Y.ravel()]
Z = np.log(-clf.eval(XX)[0])
Z = Z.reshape(X.shape)
CS = pl.contour(X, Y, Z)
CB = plt.colorbar(CS, shrink=0.8, extend='both')
pl.scatter(X_train[:, 0], X_train[:, 1], .8)
pl.axis('tight')
pl.show()