简体   繁体   English

Numpy错误:形状不匹配

[英]Numpy error: shape mismatch

When I was trying to solve a scientific problem with Python (Numpy), a 'shape mismatch' error came up: "shape mismatch: objects cannot be broadcast to a single shape". 当我试图用Python(Numpy)解决科学问题时,出现了“形状不匹配”错误:“形状不匹配:对象无法广播到单个形状”。 I managed to reproduce the same error in a simpler form, as shown below: 我设法以更简单的形式重现相同的错误,如下所示:

import numpy as np
nx = 3; ny = 5
ff = np.ones([nx,ny,7])
def test(x, y):
    z = 0.0
    for i in range(7):
        z = z + ff[x,y,i]
    return z

print test(np.arange(nx),np.arange(ny))

When I tried to call test(x,y) with x=1,y=np.arange(ny) , everything works fine. 当我试图用x=1,y=np.arange(ny)调用test(x,y)时,一切正常。 So what's going on here? 那么这里发生了什么? Why can't the both parameters be numpy arrays? 为什么两个参数都不能成为numpy数组?

UPDATE UPDATE

I have worked out the problem with some hints from @Saullo Castro. 我用@Saullo Castro的一些提示解决了这个问题。 Here's some updated info for you guys who tried to help but feel unclear about my intention: 这里有一些更新的信息给那些试图帮助但不清楚我的意图的人:

Basically I created a mesh grid with dimension nx*ny and another array ff that stores some value for each node. 基本上我创建了一个维度为nx * ny的网格网格和另一个为每个节点存储一些值的数组ff In the above code, ff has 7 values for each node and I was trying to sum up the 7 values to get a new nx*ny array. 在上面的代码中, ff为每个节点有7个值,我试图总结7个值以获得一个新的nx * ny数组。

However, the "shape mismatch" error is not due to the summing process as many of you might have guess now. 但是,“形状不匹配”错误并不是由于求和过程,因为许多人现在可能已经猜到了。 I have misunderstood the rule of functions taking ndarray objects as input parameters. 我误解了将ndarray对象作为输入参数的函数规则。 I tried to pass np.arange(nx), np.arange(ny) to test() is not gonna give me what I desired, even if nx==ny . 我试图传递np.arange(nx), np.arange(ny)test()不会给我我想要的东西,即使nx==ny

Back to my original intention, I solve the problem by creating another function and used np.fromfunction to created the array: 回到我的初衷,我通过创建另一个函数并使用np.fromfunction来创建数组来解决问题:

def tt(x, y):  
    return np.fromfunction(lambda a,b: test(a,b), (x, y))

which is not perfect but it works. 这不完美,但它的工作原理。 (In this example there seems to be no need to create a new function, but in my actual code I modified it a bit so it can be used for slice of the grid) (在这个例子中,似乎没有必要创建一个新函数,但在我的实际代码中我修改了一下,因此它可以用于网格的切片)

Anyway, I do believe there's a much better way compared to my kind of dirty solution. 无论如何,我相信与我的那种肮脏的解决方案相比,有更好的方法。 So if you have any idea about that, please share with us :). 所以,如果您对此有任何想法,请与我们分享:)。

Let's look into an array similar to your ff array: 让我们看一下类似于你的ff数组的数组:

nx = 3; ny = 4
ff = np.arange(nx*ny*5).reshape(nx,ny,5)
#array([[[ 0,  1,  2,  3,  4],
#        [ 5,  6,  7,  8,  9],
#        [10, 11, 12, 13, 14],
#        [15, 16, 17, 18, 19]],
#
#       [[20, 21, 22, 23, 24],
#        [25, 26, 27, 28, 29],
#        [30, 31, 32, 33, 34],
#        [35, 36, 37, 38, 39]],
#
#       [[40, 41, 42, 43, 44],
#        [45, 46, 47, 48, 49],
#        [50, 51, 52, 53, 54],
#        [55, 56, 57, 58, 59]]])

When you index using arrays of indices a, b, c like in ff[a, b, c] , a, b, c must have the same shape, and numpy will build a new array based on the indices. 使用索引a, b, c索引进行索引时a, b, cff[a, b, c]a, b, c必须具有相同的形状, numpy将根据索引构建一个新数组。 For example: 例如:

ff[[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 2, 3], [0, 0, 0, 1, 1, 1]]
#array([ 0,  5, 20, 26, 51, 56])

This is called fancy indexing, which is like building an array with: 这称为花式索引,就像构建一个数组:

np.array([ff[0, 0, 0], ff[0, 1, 0], ff[1, 0, 0], ..., ff[2, 3, 1]])

In your case the f[x, y, i] will produce a shape mismatch error since a, b, c do not have the same shape. 在您的情况下, f[x, y, i]将产生形状不匹配错误,因为a, b, c不具有相同的形状。

Looks like you want to sum ff over the last dimension, with the 1st 2 dimensions covering their whole range. 看起来你想要在最后一个维度上加总ff ,其中前两个维度涵盖了它们的整个范围。 : is used to denote the whole range of a dimension: :用于表示维度的整个范围:

def test():
    z = 0.0
    for i in range(7):
        z = z + ff[:,:,i]
    return z
print test()

But you can get the same result without looping, by using the sum method. 但是,通过使用sum方法,您可以在不循环的情况下获得相同的结果。

print ff.sum(axis=-1)

: is shorthand for 0:n :0:n简写

ff[0:nx, 0:ny, 0]==ff[:,:,0]

It is possible to index a block of ff with ranges, but you have to be much more careful about the shapes of the indexing arrays. 可以使用范围索引ff块,但是您必须更加小心索引数组的形状。 For a beginner it is better to focus on getting slicing and broadcasting correct. 对于初学者来说,最好将注意力集中在slicingbroadcasting上。


edit - 编辑 -

You can index an array like ff with arrays generated by meshgrid : 您可以使用meshgrid生成的数组索引像ff的数组:

I,J = meshgrid(np.arange(nx),np.arange(ny),indexing='ij',sparse=False)
I.shape # (nx,ny)
ff[I,J,:]

also works with 也适用

I,J = meshgrid(np.arange(nx),np.arange(ny),indexing='ij',sparse=True)
I.shape # (nx,1)
J.shape # (1, ny)

ogrid and mgrid are alternatives to meshgrid . ogridmgridmeshgrid替代meshgrid

Let's reproduce your problem in 2D case, so it is easier to see: 让我们在2D情况下重现您的问题,因此更容易看到:

import numpy as np

a = np.arange(15).reshape(3,5)

x = np.arange(3)
y = np.arange(5)

Demo: 演示:

>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> a[x, y] # <- This is the error that you are getting
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape

# You are getting the error because x and y are different lengths,
# If x and y were the same lengths, the code would work: 

>>> a[x, x]
array([ 0,  6, 12])

# mixing arrays and scalars is not a problem

>>> a[x, 2]
array([ 2,  7, 12])

It is not clear in your question what you are trying to do or what result are you expecting. 在你的问题中,你不清楚你想要做什么或你期望什么结果。 It seems, though, that you are trying to calculate a total with your variable z . 但是,似乎您正在尝试使用变量z计算总计。

Check if the sum method produces the result that you need: 检查sum方法是否产生您需要的结果:

import numpy as np
nx = 3; ny = 5

ff = ff = np.array(np.arange(nx*ny*7)).reshape(nx,ny,7)

print ff.sum()          # 5460

print ff.sum(axis=0)    # array([[105, 108, 111, 114, 117, 120, 123],
                        #        [126, 129, 132, 135, 138, 141, 144],
                        #        [147, 150, 153, 156, 159, 162, 165],
                        #        [168, 171, 174, 177, 180, 183, 186],
                        #        [189, 192, 195, 198, 201, 204, 207]]) shape(5,7)

print ff.sum(axis=1)    # array([[ 70,  75,  80,  85,  90,  95, 100],
                        #        [245, 250, 255, 260, 265, 270, 275],
                        #        [420, 425, 430, 435, 440, 445, 450]]) shape (3,7)

print ff.sum(axis=2)    # array([[ 21,  70, 119, 168, 217],
                        #        [266, 315, 364, 413, 462],
                        #        [511, 560, 609, 658, 707]]) shape (3,5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM