import sys from hmmlearn.hmm import MultinomialHMM import numpy as np dice_num = 3 x_num = 8 dice_hmm = MultinomialHMM(n_components=3,n_features=8,n_trials=5) dice_hmm.startprob_ = np.ones(3) / 3.0 dice_hmm.transmat_ = np.ones((3, 3)) / 3.0 dice_hmm.emissionprob_ = np.array([[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]]) dice_hmm.emissionprob_ /= dice_hmm.emissionprob_.sum(axis=1)[:, np.newaxis] X = np.array([[0], [5], [2], [4], [1], [6], [2], [4], [1], [3], [2], [5], [0], [4], [3]]) Z = dice_hmm.decode(X) # 问题A logprob = dice_hmm.score(X) # 问题B # 问题C x_next = np.dot(dice_hmm.transmat_, dice_hmm.emissionprob_) print("state: ", Z) print("logprob: ", logprob) print("prob of x_next: ", x_next)请修改和完善以上代码
时间: 2023-07-02 13:03:52 浏览: 205
import sys
from hmmlearn.hmm import MultinomialHMM
import numpy as np
dice_num = 3
x_num = 8
dice_hmm = MultinomialHMM(n_components=3, n_features=8)
dice_hmm.startprob_ = np.ones(3) / 3.0
dice_hmm.transmat_ = np.ones((3, 3)) / 3.0
dice_hmm.emissionprob_ = np.array([[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0],
[1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0],
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]])
dice_hmm.emissionprob_ /= dice_hmm.emissionprob_.sum(axis=1)[:, np.newaxis]
X = np.array([[0], [5], [2], [4], [1], [6], [2], [4], [1], [3], [2], [5], [0], [4], [3]])
# 问题A
Z = dice_hmm.predict(X)
# 问题B
logprob = dice_hmm.score(X)
# 问题C
x_next = np.dot(dice_hmm.transmat_, dice_hmm.emissionprob_)
print("state: ", Z)
print("logprob: ", logprob)
print("prob of x_next: ", x_next)
阅读全文