本文整理汇总了Python中hmm.HMM.pi方法的典型用法代码示例。如果您正苦于以下问题:Python HMM.pi方法的具体用法?Python HMM.pi怎么用?Python HMM.pi使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hmm.HMM
的用法示例。
在下文中一共展示了HMM.pi方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: train
# 需要导入模块: from hmm import HMM [as 别名]
# 或者: from hmm.HMM import pi [as 别名]
def train(self):
# Estimate the HMM parameters using the maximum likelihood method.
states, norm_signal, slope_signal = self.read_states_signals()
hmm_model = HMM()
hmm_model.dim = 2
# States number
state_list = [int(state) for state in list(states)]
hmm_model.states = len(np.unique(np.array(state_list)))
# Initial state probabilities vector
init_state = state_list[0]
hmm_model.pi = [0.0] * hmm_model.states
hmm_model.pi[init_state] = 1.0
# Transition
trans_matrix = np.zeros((hmm_model.states, hmm_model.states))
for (x, y), c in Counter(zip(state_list, state_list[1:])).iteritems():
trans_matrix[x, y] = c
for i in range(hmm_model.states):
trans_list = list()
for j in range(hmm_model.states):
trans_list.append(trans_matrix[i][j])
trans_sum = sum(trans_list) * 1.0
prob_list = [e / trans_sum for e in trans_list]
# make sure that the sum of this line converge to 1
total_prob = sum(prob_list)
if total_prob != 1.0:
prob_list[0] += (1.0 - total_prob)
hmm_model.A.append(prob_list)
# Emission
for i in range(hmm_model.states):
norm = list()
slope = list()
for j in range(len(state_list)):
if state_list[j] == i:
norm.append(norm_signal[j])
slope.append(slope_signal[j])
# Compute the mean of norm and slope signal
means_list = list()
means_list.append(np.mean(norm))
means_list.append(np.mean(slope))
hmm_model.means.append(means_list)
# Compute covariance matrix of norm and slope signal
covs_list = list()
covs_matrix = np.cov(norm, slope)
for j in range(hmm_model.dim):
for k in range(hmm_model.dim):
covs_list.append(covs_matrix[j][k] + 0.000001) # covariance must be symmetric, positive-definite
hmm_model.covs.append(covs_list)
if self.estimate_bias_correction:
model_fname = os.path.join(self.output_locaiton, "Model", "{}_{}".format(self.k_nb, self.atac_forward_shift))
else:
model_fname = os.path.join(self.output_locaiton, "Model", self.output_fname)
hmm_model.save_hmm(model_fname)