Python:tile()返回奇怪的错误

时间:2013-11-03 18:32:05

标签: python numpy scipy

我想做以下事情:

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()

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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]))