hmmlearn kmeans clustering
@@ -64,15 +64,17 @@ cpdef initial_state_kmeans( list training_data, k = 3 ):
for i in range (k):
ls.append(labels.count(i))
initial_state = [x / sum(ls) for x in ls]
- return initial_state
+ initial_means = kmeans.cluster_centers_
+ initial_cov = np.cov(initial_means)
+ return initial_state, initial_means, initial_cov
-cpdef hmm_training( list training_data, initial_state ):
+cpdef hmm_training( list training_data, initial_state, initial_means, initial_cov ):
# training data should be in array like format: X = np.array([[.5, .3, .1, .1], [.6, .4, 0, 0]])
# if we do not want init_prob to be updated through learning, set params = 'tmc' and init_prob = initial_state otherwise it will be overwritten
# according to base documentation, if init_prob not stated, it is set to be equally likely for any state (1/ # of components)
# if we have other known parameters, we should set these (ie: means_weights, covariance_type etc.)
# hmm_model = hmm.GaussianHMM( n_components=3, covariance_type = 'full' )
- hmm_model = hmm.GaussianHMM( n_components=3, init_prob=initial_state, params='tmc')
+ hmm_model = hmm.GaussianHMM( n_components=3, init_prob=initial_state, means_prior=initial_means, covar_prior=initial_cov, params='tmc')
hmm_model.fit( training_data )
return hmm_model
