我正在尝试创建HMM,我想创建我的转换矩阵,但是我不确定如何。我有一本包含转换的字典,以及出现这些转换的可能性,如下所示(仅更大):
{(1, 2): 0.0035842293906810036, (2, 3): 0.0035842293906810036, (3, 4): 0.0035842293906810036, (4, 5): 0.0035842293906810036, (5, 6): 0.0035842293906810036, (6, 7): 0.0035842293906810036, (7, 8)}
我定义如下:
# create a list of bigrams
bigrams = []
for i in range(len(integer_list)):
if i+1 in range(len(integer_list)):
bigrams.append((integer_list[i], integer_list[i+1]))
# Create a dictionary containing the counts each bigram occurs in the dataset
bigrams_dict = Counter(bigrams)
values = bigrams_dict.values()
# create a dictionary containing the probability of a word occurring. <- initial probs
frequencies = {key:float(value)/sum(counts_dict.values()) for (key,value) in counts_dict.items()}
frequency_list = []
for value in frequencies.values():
frequency_list.append(value)
现在,我想以此为基础制作一个转换矩阵,它是一个多维数组,但是我不确定如何做到这一点。谁能帮我。
过渡矩阵看起来像这样的示例(当然只有更多的状态):
0 1/3 2/3
0 2/3 1/3
1 0 0
一般的过程只是用正确的尺寸预定义零矩阵,然后一次填充一个元素。不要想太多这类任务。
例如,如果您知道您刚好有8个状态,则可以使用frequencies
字典来构建矩阵,如下所示:>
import numpy as np n_states = 8 transitions = np.zeroes((n_states, n_states), dtype=np.float) for (state1, state2), probability in frequencies.items(): transitions[state1, state2] = probability
对于许多州,这可能需要一段时间,具体取决于计算机的速度。
如果不知道状态总数,可以通过计算数据中最大的状态数来估计它:
from itertools import chain
n_states = max(chain.from_iterable(frequencies.keys()))