def _train_transition(self,observations):
# Initialize transition matrix
new_trans_prob = np.asmatrix(np.zeros(self.trans_prob.shape))
# Find alpha and beta
alpha,c = self._alpha_cal(observations)
beta = self._beta_cal(observations,c)
# calculate transition matrix values
for t in range(len(observations)-1):
temp1 = np.multiply(alpha[:,t],beta[:,t+1].transpose())
temp1 = np.multiply(self.trans_prob,temp1)
new_trans_prob = new_trans_prob + np.multiply(temp1,self.em_prob[:,self.obs_map[observations[t+1]]].transpose())
# Normalize values so that sum of probabilities is 1
for i in range(self.trans_prob.shape[0]):
new_trans_prob[i,:] = new_trans_prob[i,:]/np.sum(new_trans_prob[i,:])
return new_trans_prob
评论列表
文章目录