[英]How can I fix error of broadcasting state and emission probability using hidden_markov?
I am having problem with hidden_markov package when applying a slight change in a simple example in its documentation. 在其文档的一个简单示例中进行细微更改时,hidden_markov软件包出现问题。 In the following code I try 2 states and 3 possible observations (in the documentation's example there are 2 possible observations and the code works fine):
在下面的代码中,我尝试2种状态和3种可能的观察结果(在文档示例中,有2种可能的观察结果,并且代码运行正常):
states = ('s', 't')
possible_observation = ('A', 'B', 'C')
# Numpy arrays of the data
start_probability = np.matrix( '0.5 0.5')
transition_probability = np.matrix('0.6 0.4 ; 0.3 0.7')
emission_probability = np.matrix( '0.3 0.2 0.5 ; 0.3 0.1 0.6')
# Initialize class object
test = hmm(states,possible_observation,
start_probability,
transition_probability,
emission_probability)
observations = ('A', 'B','B','A', 'C')
obs4 = ('B', 'C', 'A','B')
observation_tuple = []
observation_tuple.extend( [observations,obs4] )
quantities_observations = [18, 28]
num_iter=1000
e,t,s = test.train_hmm(observation_tuple,num_iter,quantities_observations)
After running the code I get the error: 运行代码后,出现错误:
ValueError: operands could not be broadcast together with shapes (2,3) (1,2)
Interestingly, when I try 3 states and 3 possible observations (and modify the probability matrices based on this change) the code works fine. 有趣的是,当我尝试3种状态和3种可能的观察结果(并根据此更改修改概率矩阵)时,代码可以正常工作。 Either I am missing something, or the number of states and possible observations should always be equal which does not make sense.
我丢失了某些东西,或者状态数和可能的观察值应该始终相等,这没有任何意义。
Based on the matrix you have provided above, there are only two matrices that have the shape (2,3)
and (1,2)
which are emission_probability
and start_probability
respectively and the error is of the matrix dimension mismatch. 根据您在上面提供的矩阵,只有两个形状分别为
(2,3)
和(1,2)
的矩阵分别为emission_probability
概率和start_probability
概率,误差为矩阵维数不匹配。 To match the matrix dimensions so that they can be used for calculation of dot product is by doing. 为了匹配矩阵尺寸,以便可以将它们用于点积的计算。
emission_probability = emission_probability.T
start_probability = start_probability.T
This step has to be done before initializing the class object. 必须在初始化类对象之前完成此步骤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.