简体   繁体   English

如何使用hidden_​​markov修复广播状态和发射概率的错误?

[英]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.

相关问题 在python中使用hidden_​​markov时发生ValueError - ValueError when working with hidden_markov in python 如何使用pymc制作离散状态Markov模型? - How can I make a discrete state Markov model with pymc? 导出 Tensorflow 概率的隐马尔可夫 Model - Exporting Tensorflow probability's Hidden Markov Model 使用hmmlearn收敛到一个状态的隐马尔可夫模型 - Hidden Markov Model converging to one state using hmmlearn 如何使用马尔可夫链获得所有可能性? - How can I get all possibilities using a markov chain? 如何修复 python 循环中的值错误广播? - How to fix value error broadcasting in python loop? 如何使用 tensorflow_probability 解决此问题? - how can i fix this issue with tensorflow_probability? 使用testfixtures测试Django中的日志记录时,如何使日志发射静音? - When testing logging in Django using testfixtures, how can I silence the log emission? Python:我不断收到广播错误,但我不知道如何解决它 - Python: I keep getting a broadcasting error, but I'm not sure how to fix it 使用隐马尔可夫模型进行情感分析 - Sentiment analysis using Hidden Markov Model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM