Last active
August 29, 2015 13:56
-
-
Save bougui505/9229225 to your computer and use it in GitHub Desktop.
Fit mixture of Gaussian
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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