[英]Visualize fitted gaussian distributions from GMM model
我試圖從高斯混合模型中可視化擬合的高斯分布,似乎無法弄明白。 這里和這里我已經看到了用於可視化一維模型的擬合分布的示例,並且我沒有弄清楚如何將其應用於具有3個特征的模型。 是否可以將每個訓練特征的擬合分布可視化?
我已經命名了我的模型estimator
並使用X_train
對其進行了X_train
:
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_train
的前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_train
的形狀是(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)
幾個問題:linspace length
不正確,你正在調用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.transAxes是預期的:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.