1

我想做以下事情:

vectors = hstack(((array([[nA, nM, nE]])),
        nP.T,(array([[nM,nA]]))))

tile(P[newaxis, newaxis, newaxis,
        ...,newaxis,newaxis], vectors)

但是,我收到一条错误消息。接下来,我使用了几个变量的输出/内容:

# output:
print vectors.shape: (1L, 14L)
print vectors: array([[ 3.,  2.,  5.,  2.,  2.,  4.,  4.,  4.,  8.,  8.,  8.,  8.,  2.,  3.]])
print P.shape: (2L, 2L, 4L, 4L, 4L, 8L, 8L, 8L, 8L)
print P[newaxis, newaxis, newaxis,
        ...,newaxis,newaxis].shape: (1L, 1L, 1L, 2L, 2L, 4L, 4L, 4L, 8L, 8L, 8L, 8L, 1L, 1L)
tile(P[newaxis, newaxis, newaxis,
        ...,newaxis,newaxis], vectors): ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() (somewhere inside tile())

最后是完整的追溯:

ValueError                                Traceback (most recent call last)
<ipython-input-1050-25019e870a05> in <module>()
      1 P = tile(P[newaxis, newaxis, newaxis,
----> 2     ...,newaxis,newaxis], vectors)

C:\Users\sdaro\AppData\Local\Enthought\Canopy\User\lib\site-packages\numpy\lib\shape_base.pyc in tile(A, reps)
    831         tup = (1,)*(c.ndim-d) + tup
    832     for i, nrep in enumerate(tup):
--> 833         if nrep!=1:
    834             c = c.reshape(-1,n).repeat(nrep,0)
    835         dim_in = shape[i]

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4

2 回答 2

1

您正在传递一个形状为 (1,14) 的二维数组 ( vectors) 作为 的第二个参数tile。显然它必须是一维的。尝试:

tile(P[newaxis, newaxis, newaxis,
        ...,newaxis,newaxis], vectors[0])
于 2013-11-03T20:27:02.410 回答
0

你用tile(A, reps)错了。第二个参数reps应该是整数的向量(一维数组),指定沿每个轴的重复次数。无论你传递什么(我拒绝试图理解你的vectors任务混乱)......

vectors = hstack(((array([[nA, nM, nE]])),
    nP.T,(array([[nM,nA]]))))

...是一个多维数组,因此在某些时候 numpy 会尝试将数组的元素视为数字,但失败了:

# What is x supposed to be?
x = int(np.array([1, 2, 3]))
于 2013-11-03T20:27:26.990 回答