numpy __array_prepare__错误 [英] Numpy __array_prepare__ error

查看:613
本文介绍了numpy __array_prepare__错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试网上找到一个可以使期望最大化的食谱(

I'm trying to get a recipe working that I found online for doing expectation maximization (http://code.activestate.com/recipes/577735-expectation-maximization/). I run into the following error:

Traceback (most recent call last):
  File "./runem.py", line 7, in <module>
    print expectation_maximization([[1,2,3,4,5],[2,3,4,5,6],[9,8,7,4,1]], 2)
  File "/local/scratch-3/dk427/rp/em.py", line 83, in expectation_maximization
    Px[o,c] = pnorm(t[o,:], params[c]['mu'], params[c]['sigma'])
  File "/local/scratch-3/dk427/rp/em.py", line 18, in pnorm
    xmt = np.matrix(x-m).transpose()
TypeError: __array_prepare__ must return an ndarray or subclass thereof which is otherwise identical to its input

该算法一定有缺陷,或者我给它输入了错误的信息,但是我找不到问题所在.我发现错误是由减法x-m引起的,但是由x.dtype=int64m.dtype=float64引起的,我认为这应该起作用.

There must be some flaw in the algorithm, or I'm giving it the wrong input, but I can't find what's going wrong. I've found that the error is caused by the subtraction x-m, but x.dtype=int64 and m.dtype=float64, which I think should work.

有人有什么想法吗?

推荐答案

您似乎传递的是列表列表,而不是数组.您可以执行以下操作:

You seem to be passing in a list of lists rather than an array. You could do something like:

ts = np.array([[1,2,3,4,5],[2,3,4,5,6],[9,8,7,4,1]])
expectation_maximization(ts, 2)

在计算机上取平方根似乎在某些时候存在一些问题,但是我认为这可能是因为出于某种原因该数据不适用于该算法(但是我不知道该算法是什么)正在尝试这样做,所以我不能确定).

That seems to have some problems at some point with taking a square root on my computer, but I think that might be because that data isn't good for this algorithm for some reason (but I don't know what the algorithm is trying to do, so I can't say for sure).

这篇关于numpy __array_prepare__错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆