Skip to content

Instantly share code, notes, and snippets.

@bougui505
Last active August 29, 2015 13:56
Show Gist options
  • Save bougui505/9229225 to your computer and use it in GitHub Desktop.
Save bougui505/9229225 to your computer and use it in GitHub Desktop.
Fit mixture of Gaussian
from sklearn import mixture
%pylab
def fit_mixture(data, ncomp=2, doplot=False):
clf = mixture.GMM(n_components=ncomp, covariance_type='full')
clf.fit(data)
ml = clf.means_
wl = clf.weights_
cl = clf.covars_
ms = [m[0] for m in ml]
cs = [numpy.sqrt(c[0][0]) for c in cl]
ws = [w for w in wl]
ind = argmax(ws)
vmin = ms[ind] - cs[ind]
vmax = ms[ind] + cs[ind]
if doplot == True:
histo = hist(data, 200, normed=True)
for w, m, c in zip(ws, ms, cs):
plot(histo[1],w*matplotlib.mlab.normpdf(histo[1],m,np.sqrt(c)), linewidth=3)
return vmin, vmax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment