我正在尝试初始化几个 GMM 以与 GMMHMM 的 gmms_ 属性一起使用。每个 GMM 实例具有不同的均值、权重和协方差,并作为 GMMHMM 的 5 分量混合的一个分量。均值、权重和协方差由我想要拟合的数据集的(5 簇)k 均值算法确定,其中均值是每个簇的中心,权重是每个簇的权重,而协方差是 - 你猜对了 - 每个集群的协方差。
这是一个代码片段:
X_clusters = cls.KMeans(n_clusters=5)
fitted_X = X_clusters.fit(X)
means = fitted_X.cluster_centers_
cluster_arrays = extract_feat(X, fitted_X.labels_)
print ('Means: {0}'.format(means))
total_cluster = float(len(X))
all_GMM_params = []
for cluster in cluster_arrays:
GMM_params = []
weight = float(len(cluster))/total_cluster
covar = np.cov(cluster)
GMM_params.append(weight)
GMM_params.append(covar)
all_GMM_params.append(GMM_params)
for i in range(len(means)):
all_GMM_params[i].append(means[i])
model = GMMHMM(n_components=4, covariance_type="diag", n_iter=1000,
n_mix = 5, algorithm='map')
for i in range(len(all_GMM_params)):
GMM_n = mix.GMM(init_params = '')
GMM_n.weights_ = np.array(all_GMM_params[i][0])
GMM_n.covars_ = np.array(all_GMM_params[i][1])
GMM_n.means_ = np.array(all_GMM_params[i][2])
model.gmms_.append(GMM_n)
model.fit(X)
但是,当我尝试拟合模型时,出现以下错误:
fitting to HMM and decoding ...Traceback (most recent call last):
File "HMM_stock_sim.py", line 156, in <module>
model.fit(X)
File "C:\Python27\lib\site-packages\hmmlearn\base.py", line 436, in fit
bwdlattice)
File "C:\Python27\lib\site-packages\hmmlearn\hmm.py", line 590, in _accumulate
_sufficient_statistics
stats, X, framelogprob, posteriors, fwdlattice, bwdlattice)
File "C:\Python27\lib\site-packages\hmmlearn\base.py", line 614, in _accumulat
e_sufficient_statistics
stats['start'] += posteriors[0]
ValueError: operands could not be broadcast together with shapes (4,) (9,) (4,)
我以前从未见过这样的错误,这是我第一次使用 sklearn 和 HMMlearn。我该如何解决这个错误?