本文整理汇总了Python中hmm.HMM.forward_backward方法的典型用法代码示例。如果您正苦于以下问题:Python HMM.forward_backward方法的具体用法?Python HMM.forward_backward怎么用?Python HMM.forward_backward使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hmm.HMM
的用法示例。
在下文中一共展示了HMM.forward_backward方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestHMM
# 需要导入模块: from hmm import HMM [as 别名]
# 或者: from hmm.HMM import forward_backward [as 别名]
class TestHMM():
def __init__(self):
self.Z = numpy.array([
[0.8, 0.09, 0.01],
[0.09, 0.8, 0.01],
[0.1, 0, 0.8]
])
self.b = numpy.array([
[0.1, 0.1, 0.8],
[0.05, 0.9, 0.05],
[0.8, 0.1, 0.1]
])
self.pi = numpy.array([0.9,0.05,0.05])
self.T = 2000
# we want the errors to be less than 20%
self.error_threshold = 0.2
def setup(self):
self.model = HMM(self.Z,self.b,self.pi)
def gen_states_obs(self):
states = []
obsvns = []
for (s,o) in self.model.gen(self.T):
states.append(s)
obsvns.append(o)
return states, obsvns
def test_init(self):
self.model = HMM(self.Z,self.b,self.pi)
def test_gen(self):
self.setup()
states = []
obsvns = []
for (s,o) in self.model.gen(10):
states.append(s)
obsvns.append(o)
assert len(states) == 10
assert len(obsvns) == 10
def test_forward_backward(self):
self.setup()
states, obsvns = self.gen_states_obs()
alpha,beta = self.model.forward_backward(obsvns)
gamma = [a*b/sum(a*b) for a,b in zip(alpha,beta)]
state_est = numpy.array([numpy.where(g==max(g))[0][0] for g in gamma])
err = sum(state_est != numpy.array(states))/float(len(states))
assert err < self.error_threshold
def test_viterbi(self):
self.setup()
states, obsvns = self.gen_states_obs()
state_est = self.model.viterbi(obsvns)
err = sum(state_est != numpy.array(states))/float(len(states))
assert err < self.error_threshold