Python 从GMM模型可视化拟合高斯分布
我试图从高斯混合模型中可视化拟合的高斯分布,但似乎无法理解它。我已经看到了一个一维模型拟合分布的可视化示例,但我不知道如何将其应用到一个具有3个特征的模型。是否可以可视化每个训练特征的拟合分布 我已将我的模型命名为Python 从GMM模型可视化拟合高斯分布,python,matplotlib,scikit-learn,mixture-model,Python,Matplotlib,Scikit Learn,Mixture Model,我试图从高斯混合模型中可视化拟合的高斯分布,但似乎无法理解它。我已经看到了一个一维模型拟合分布的可视化示例,但我不知道如何将其应用到一个具有3个特征的模型。是否可以可视化每个训练特征的拟合分布 我已将我的模型命名为估计器,并使用X\u训练对其进行训练: estimator = GaussianMixture(covariance_type='full', init_params='kmeans', max_iter=100, means_init=array([[ 0.41297
估计器
,并使用X\u训练对其进行训练
:
estimator = GaussianMixture(covariance_type='full', init_params='kmeans', max_iter=100,
means_init=array([[ 0.41297, 3.39635, 2.68793],
[ 0.33418, 3.82157, 4.47384],
[ 0.29792, 3.98821, 5.78627]]),
n_components=3, n_init=1, precisions_init=None, random_state=0,
reg_covar=1e-06, tol=0.001, verbose=0, verbose_interval=10,
warm_start=False, weights_init=None)
X_列车的前5个样本如下所示:
X_train[:6,:] = array([[ 0.29818663, 3.72573161, 4.19829702],
[ 0.24693619, 4.33026266, 10.74416161],
[ 0.21932575, 3.98019433, 8.02464581],
[ 0.24426255, 4.41868353, 10.52576923],
[ 0.16577695, 4.35316706, 12.63638592],
[ 0.28952628, 4.03706551, 8.03804016]])
X\u列车的形状是(3753L,3L)
。我绘制第一个特征的拟合高斯分布的程序如下:
fig, (ax1,ax2,a3) = plt.subplots(nrows=3)
#Domain for pdf
x = np.linspace(0,0.8,3753)
logprob = estimator.score_samples(X_train)
resp = estimator.predict_proba(X_train)
pdf = np.exp(logprob)
pdf_individual = resp * pdf[:, np.newaxis]
ax1.hist(X_train[:,0],30, normed=True, histtype='stepfilled', alpha=0.4)
ax1.plot(x, pdf, '-k')
ax1.plot(x, pdf_individual, '--k')
ax1.text(0.04, 0.96, "Best-fit Mixture",
ha='left', va='top', transform=ax.transAxes)
ax1.set_xlabel('$x$')
ax1.set_ylabel('$p(x)$')
plt.show()
但这似乎不起作用。关于如何实现这一点,有什么想法吗?如果我加载您的样本数据并拟合估计器:
X_train = np.array([[ 0.29818663, 3.72573161, 4.19829702],
[ 0.24693619, 4.33026266, 10.74416161],
[ 0.21932575, 3.98019433, 8.02464581],
[ 0.24426255, 4.41868353, 10.52576923],
[ 0.16577695, 4.35316706, 12.63638592],
[ 0.28952628, 4.03706551, 8.03804016]])
estimator.fit(X_train)
两个问题:linspacelength
不正确,您正在调用ax.transAxes
,但尚未定义任何ax
。以下是一个有效的版本:
fig, (ax1,ax2,a3) = plt.subplots(nrows=3)
logprob = estimator.score_samples(X_train)
resp = estimator.predict_proba(X_train)
这里的长度应该与logprob/pdf匹配
#Domain for pdf
x = np.linspace(0,0.8,len(logprob))
pdf = np.exp(logprob)
pdf_individual = resp * pdf[:, np.newaxis]
ax1.hist(X_train[:,0],30, normed=True, histtype='stepfilled', alpha=0.4)
ax1.plot(x, pdf, '-k')
ax1.plot(x, pdf_individual, '--k')
在这里,ax1.0是预期的:
ax1.text(0.04, 0.96, "Best-fit Mixture",
ha='left', va='top', transform=ax1.transAxes)
ax1.set_xlabel('$x$')
ax1.set_ylabel('$p(x)$')
plt.show()
如果我加载您的样本数据并拟合估计器:
X_train = np.array([[ 0.29818663, 3.72573161, 4.19829702],
[ 0.24693619, 4.33026266, 10.74416161],
[ 0.21932575, 3.98019433, 8.02464581],
[ 0.24426255, 4.41868353, 10.52576923],
[ 0.16577695, 4.35316706, 12.63638592],
[ 0.28952628, 4.03706551, 8.03804016]])
estimator.fit(X_train)
两个问题:linspacelength
不正确,您正在调用ax.transAxes
,但尚未定义任何ax
。以下是一个有效的版本:
fig, (ax1,ax2,a3) = plt.subplots(nrows=3)
logprob = estimator.score_samples(X_train)
resp = estimator.predict_proba(X_train)
这里的长度应该与logprob/pdf匹配
#Domain for pdf
x = np.linspace(0,0.8,len(logprob))
pdf = np.exp(logprob)
pdf_individual = resp * pdf[:, np.newaxis]
ax1.hist(X_train[:,0],30, normed=True, histtype='stepfilled', alpha=0.4)
ax1.plot(x, pdf, '-k')
ax1.plot(x, pdf_individual, '--k')
在这里,ax1.0是预期的:
ax1.text(0.04, 0.96, "Best-fit Mixture",
ha='left', va='top', transform=ax1.transAxes)
ax1.set_xlabel('$x$')
ax1.set_ylabel('$p(x)$')
plt.show()
您的错误是什么?你是如何计算出这个估计数的<代码>估计器.fit(X_列车)
?您的错误是什么?你是如何计算出这个估计数的<代码>估计器拟合(X_序列)
?