我正在用马尔可夫链进行一些工作,我需要从给定状态变化序列的转换矩阵中查找转换概率。如何在numpy中有效地做到这一点?
例如:
import numpy as np
#here is the sequence that I need to look up in the transition matrix
sequence = [0, 1, 0, 1, 1]
#the transition matrix that gives the probability to change between each
of the states
transition_matrix = np.array([[0.2, 0.8], [0.6, 0.4]])
#desired output
result = [0.8, 0.6, 0.8, 0.4]
因此,结果只是在转换矩阵中查找的概率值。当状态很多且顺序很长时,如何有效地做到这一点?
谢谢。
答案 0 :(得分:0)
只需使用zip
:
result = []
for (step, next_step) in zip(sequence[:-1], sequence[1:]):
result.append(transition_matrix[step][next_step])
结果:
[0.8, 0.6, 0.8, 0.4]