这是我的问题,我正在尝试使用 hmmlearn 教授隐马尔可夫模型。我是这门语言的新手,我很难理解列表和数组之间的差异。这是我的代码:
from hmmlearn import hmm
from babel import lists
import numpy as np
import unidecode as u
from numpy import char
l = []
data = []
gods_egypt = ["Amon","Anat","Anouket","Anubis","Apis","Atoum","Bastet","Bès","Gheb","Hâpy","Harmachis","Hathor","Heh","Héket","Horus","Isis","Ka","Khepri","Khonsou","Khnoum","Maât","Meresger","Mout","Nefertoum","Neith","Nekhbet","Nephtys","Nout","Onouris","Osiris","Ouadjet","Oupaout","Ptah","Rê","Rechef","Renenoutet","Satet","Sebek","Sekhmet","Selkis","Seth","Shou","Sokaris","Tatenen","Tefnout","Thot","Thouéris"]
for i in range(0, len(gods_egypt)):
data.append([])
for j in range(0, len(gods_egypt[i])):
data[i].append([u.unidecode(gods_egypt[i][j].lower())])
l.append(len(data[i]))
data = np.asarray(data).reshape(-1,1)
model = hmm.MultinomialHMM(20, verbose=True)
model = model.fit(data, l)
和生成的输出
Traceback (most recent call last):
File "~~~HMM_test.py", line 17, in <module>
model = model.fit(data, l)
File "~~~PythonPython36site-packageshmmlearnbase.py", line 420, in fit
X = check_array(X)
File "~~~Python36-32libsite-packagessklearnutilsvalidation.py", line 402, in check_array
array = np.array(array, dtype=dtype, order=order, copy=copy)
ValueError: setting an array element with a sequence.
我在 ValueError 中看到:使用序列设置数组元素,这可能是不同数组长度的问题,但我无法弄清楚如何解决它。
有什么建议吗?
错误本身来自这样一个事实,即model.fit()
期待一个数值数组数组。现在,您的输入data
是一个字符串列表列表数组数组。这就是引发错误的原因,因为函数发现它期望array element
is a sequence
,即列表(字符串列表(。
但是,即使您修复了列表问题,也会出现另一个问题: 学习HMM意味着通过一组方程计算数值量。学习HMM的输入数据应该是数字,而不是一组字母。(除非hmmlearn
对我不知道的角色有一个非常特殊的选项。
如果要使用 HMM,则需要首先将字母转换为数字。
我不知道你的最终目标是什么。HMM 旨在为生成或分类目的对数据进行建模(如果训练了多个 HMM(。一旦你从组成单词的字母中得到了一个训练好的模型,你打算做什么?
至于数据应该以何种格式提供给不同的功能,我建议您查看文档。它包括使用库的教程。