在python中使用隐马尔可夫时发生ValueError

2024-04-27 07:23:36 发布

您现在位置:Python中文网/ 问答频道 /正文

运行train.hmm()时出现以下错误

ValueError: operands could not be broadcast together with shapes (7,20) (1,7)

我知道什么是广播错误,看了文件。但我不能改变矩阵的维数,因为它会在另一个地方弹出一个错误。代码如下所示。你知道吗

start_probability = np.matrix( '0.5 0.02 0.18 0.05 0.01 0.2 0.04 ')
transition_probability = np.matrix('0.9 0.012 0.012 0.012 0.002 0.05 0.012 \
;  0.03 0.35 0.05 0.05 0.02 0.45 0.05 \
;  0.03 0.05 0.45 0.05 0.02 0.35 0.05 \
;  0.1 0.1 0.1 0.4 0.1 0.1 0.1 \
;  0.1 0.1 0.1 0.1 0.4 0.1 0.1 \
;  0.2 0.05 0.05 0.05 0.05 0.4 0.2 \
;  0.12 0.12 0.12 0.12 0.01 0.12 0.39')
emission_probability = np.matrix(np.ones((7, 20)) * 0.05)

test = hmm(states,possible_observation,start_probability,transition_probability,emission_probability)
observations = ['A', 'S','T','A']
obs4 = ['C', 'A','G']
observation_tuple = []
observation_tuple.extend( [observations,obs4] )
quantities_observations = [10, 20]
num_iter=1000
e,t,s = test.train_hmm(observation_tuple,num_iter,quantities_observations)

错误对应于最后一行。如果我转置start_prbabilityemission_probability矩阵,test = hmm()行本身就会出错。你知道吗

我做错什么了?你知道吗


Tags: test错误nptrain矩阵startmatrixprobability
1条回答
网友
1楼 · 发布于 2024-04-27 07:23:36

hmm_class.py的第349行改为

emProbNew = emProbNew/ np.reshape(em_norm.transpose(),[-1,1])

这不是我所知道的最好的解决方案,但我认为作者忽略了一个事实,即Python并不总是能够知道他在这里使用除法意味着什么。你知道吗

相关问题 更多 >