I am trying to initialize several GMM's for use with the GMMHMM's gmms_ attribute. Each GMM instance has a different mean, weight and co-variance and serves as a component of a 5-component mixture for the GMMHMM. The mean, weight and co-variance are determined from a (5-cluster) k-means algorithm of the data-set I want to fit, where the mean is center of each cluster, the weight is the weight of each cluster and the co-variance is the - you guessed it - co-variance of each cluster.
Here is a code snippet:
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)
When I try to fit the model, however, I get the following error:
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,)
I have never seen error like this before, its my first time working with sklearn and HMMlearn. How do I go about fixing this error?