一维高斯混合模型

时间:2019-10-15 01:42:16

标签: python numpy scikit-learn gmm mixture

是否可以使用skylearn GMM绘制训练数据的所有迭代图?

另一件事,我的代码发布在下面,正如您看到的那样,绘制它时,与PDF相比,它将返回“小的”高斯分布,有人知道为什么吗?

import math
import matplotlib.pyplot as plt
from sklearn import cluster, datasets, mixture
import numpy as np
from scipy.stats import multivariate_normal
from scipy import stats

n_samples = 1000
mu1, sigma1 = -4, 1.2 # mean and variance
mu2, sigma2 = 4, 1.8 # mean and variance
mu3, sigma3 = 0, 1.6 # mean and variance

x1 = np.random.normal(mu1, np.sqrt(sigma1), n_samples)
x2 = np.random.normal(mu2, np.sqrt(sigma2), n_samples)
x3 = np.random.normal(mu3, np.sqrt(sigma3), n_samples)

X = np.array(list(x1) + list(x2) + list(x3))
np.random.shuffle(X)
#print("Dataset:")
#print(X)

a = np.zeros(np.size(X))
plt.plot(X,a,'|b')

#plot pdf
x1 = np.sort(x1)
y1 = multivariate_normal.pdf (x1,mu1,np.sqrt(sigma1))
plt.plot(x1,y1, color = 'gray')

x2 = np.sort(x2)
y2 = multivariate_normal.pdf (x2,mu2,np.sqrt(sigma2))
plt.plot(x2,y2, color = 'gray')

x3 = np.sort(x3)
y3 = multivariate_normal.pdf (x3,mu3,np.sqrt(sigma3))
plt.plot(x3,y3, color = 'gray')

#full training
X = np.sort(X)
X = X.reshape(-1, 1)
g = mixture.GaussianMixture(n_components=3)
g.fit(X)

weights = g.weights_
means = g.means_
covars = g.covariances_

mean = means[0]
sigma = np.sqrt(covars[0])
plt.plot(X,weights[0]*multivariate_normal.pdf(X,mean,sigma), c='red')

mean = means[1]
sigma = np.sqrt(covars[1])
plt.plot(X,weights[1]*multivariate_normal.pdf(X,mean,sigma), c='blue')

mean = means[2]
sigma = np.sqrt(covars[2])
plt.plot(X,weights[2]*multivariate_normal.pdf(X,mean,sigma), c='green')

plt.show()

谢谢

0 个答案:

没有答案