0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

hmmlearnでtransmat_ rows must sum to 1が出る原因

Last updated at Posted at 2023-04-04

hmmlearnエラーの原因がわかったのでメモ

環境

Python 3.10.4
hmmlearn 0.2.8

エラー再現

import numpy as np
from hmmlearn import hmm

X = np.array([[100], [100], [100], [1]])

model = hmm.GaussianHMM(n_components=2)
model.fit(X)
# Warning: Some rows of transmat_ have zero sum because no transition from the state was ever observed.

model.sample(20) # ここでエラーになる
# ValueError: transmat_ rows must sum to 1 (got [1. 0.])

原因

あるステートから別(または同じ)ステートに一度も移行しない場合に起こる。

これはエラーにならない

X = np.array([[100], [100], [100], [1], [100]])
X = np.array([[100], [100], [100], [1], [1]])

また、根本的な解決にはならないが遷移確率の合計が1にならなかった時にmodel.transmat_に仮の値を代入しても一応エラーは防げる。
例えば

num_states = 2
if model.transmat_.sum() != num_states:
    model.transmat_ = np.array([[0.5, 0.5], [0.5, 0.5]])

とか。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?