1

我正在尝试将多条高斯曲线拟合到我的实验数据中。使用 sci-kit 学习混合模型获得高斯混合模型。GM 拟合我的实验数据如下图所示。

GMM 拟合实验数据。

如您所见,多条高斯曲线适合我的数据。但是,我只是希望保留最高峰值的两条曲线,并希望获得这两条高斯曲线的参数,以便我可以独立绘制这两条特定的高斯曲线(注意,单独的均值和协方差不足以重现它们,我还需要知道缩放参数)。有没有办法这样做?我附上了下面的代码。

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)

我在这里找到了我的问题的答案。谢谢

4

0 回答 0