python - scikit 学习 GaussianHMM ValueError : input must be a square array

标签 python numpy scikit-learn hidden-markov-models

我正在使用 scikit-learn 的 GaussianHMM,当我尝试将其拟合到某些观察结果时,我得到了以下 ValueError。这是演示错误的代码:

>>> from sklearn.hmm import GaussianHMM
>>> arr = np.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> arr
matrix([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])
>>> gmm = GaussianHMM ()
>>> gmm.fit (arr)
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/function_base.py:2005: RuntimeWarning: invalid value encountered in divide
  return (dot(X, X.T.conj()) / fact).squeeze()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/sklearn/hmm.py", line 427, in fit
    framelogprob = self._compute_log_likelihood(seq)
  File "/Library/Python/2.7/site-packages/sklearn/hmm.py", line 737, in _compute_log_likelihood
    obs, self._means_, self._covars_, self._covariance_type)
  File "/Library/Python/2.7/site-packages/sklearn/mixture/gmm.py", line 58, in log_multivariate_normal_density
    X, means, covars)
  File "/Library/Python/2.7/site-packages/sklearn/mixture/gmm.py", line 564, in _log_multivariate_normal_density_diag
    + np.dot(X ** 2, (1.0 / covars).T))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/matrixlib/defmatrix.py", line 343, in __pow__
    return matrix_power(self, other)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/matrixlib/defmatrix.py", line 160, in matrix_power
    raise ValueError("input must be a square array")
ValueError: input must be a square array
>>> 

我该如何补救?看来我正在给它有效的输入。谢谢!

最佳答案

你必须符合一个列表,参见official examples :

>>> gmm.fit([arr])
GaussianHMM(algorithm='viterbi', covariance_type='diag', covars_prior=0.01,
      covars_weight=1,
      init_params='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
      means_prior=None, means_weight=0, n_components=1, n_iter=10,
      params='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
      random_state=None, startprob=None, startprob_prior=1.0, thresh=0.01,
      transmat=None, transmat_prior=1.0)
>>> gmm.n_features
3
>>> gmm.n_components
1

关于python - scikit 学习 GaussianHMM ValueError : input must be a square array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20619734/

相关文章:

python - 如何通过(字符串)名称选择变量?

python - 通过 PyPI 与发行包管理器安装 Numpy

python - 如何找到两个 Pandas DataFrame 之间的差异

python - 来自 joblib 的多处理不并行化?

python-3.x - 使用代理加载 fetch_lfw_people

python - 将 S3 key 的内容保存到 boto3 中的字符串的直接方法?

python - 使用Python将XML解析为CSV nonetype错误

Python 多处理,在循环中多次使用池在第一次迭代后卡住

python - 使用此命令 `np.linalg.eig(H*H)` 计算特征分解是否合适?

python - 修剪 sklearn 决策树以确保单调性