get parameters of each gaussian curve predicted by Scikit learn Gaussian Mixture model?

Viewed 403

I am trying to fit multiple Gaussian curves to my experimental data. The Gaussian mixture model was obtained using sci-kit learn Mixture models. The GM fit over my experimental data is shown in the image below.

GMM fit over experimental data.

As you can see multiple Gaussian curves fit my data. However, I just wish to retain the two curves with the highest peak and wish to obtain the parameters of these two Gaussian curves such that I can independently plot these two specific Gaussian curves (Note that the mean and covariance alone is not enough to reproduce them, I also need to know the scaling parameter). Is there a way to do so? I have attached the code below.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.ticker as ticker
from sklearn.mixture import GaussianMixture
import random as random
## Generating random data resembling experimental data
C1 = np.zeros(2000)
for c in range(2000):
    if c<=400:
        C1[c] = random.gauss(0.7, 0.2)
    elif c<=600:
        C1[c] = random.gauss(0.9,0.25)
    elif c<= 800:
        C1[c] = random.gauss(2.5,0.2)
    elif c<= 1200:
        C1[c] = random.gauss(1.5, 0.5)
    elif c<=1600:
        C1[c] = random.gauss(5,3.5)
    elif c<2000:
        C1[c] = random.gauss(10, 5)
C1[C1<0] = 0
C1 = np.sort(C1)
#### Plotting a normalised histogram
fig, ax = plt.subplots()
fig.set_figheight(10)
fig.set_figwidth(10)
n, bins, patches = ax.hist(C1, 
                           bins = 250,align = 'mid', density = True,color = 'grey' )

""" Using machine Learning i.e Gaussian mixture models """
### Using GMM to predict different Gaussain Curves
X = np.array(C1)
gmm = GaussianMixture(n_components=6, random_state=0).fit(X.reshape(-1, 1))
labels = gmm.predict(X.reshape(-1,1))
gmm_y = np.exp(gmm.score_samples(X.reshape(-1, 1)))
ax.plot(X.reshape(-1,1), gmm_y, color="crimson", lw=2, label="GMM")
ax.tick_params(labelsize=26)

I found the answer to my question here. Thanks

0 Answers
Related