我在尝试使用网上找到的一个关于期望最大化算法的配方(http://code.activestate.com/recipes/577735-expectation-maximization/)。运行时遇到了以下错误:
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=int64
和m.dtype=float64
,我认为这应该是可以工作的。
有谁有任何想法吗?
回答:
你似乎传递的是一个列表的列表,而不是数组。你可以这样做:
ts = np.array([[1,2,3,4,5],[2,3,4,5,6],[9,8,7,4,1]])expectation_maximization(ts, 2)
在我的电脑上,这似乎在某个时候在进行平方根运算时出现了一些问题,但我认为这可能是因为这些数据对这个算法来说不太合适(但我不知道这个算法试图做什么,所以我不能确定)。