简体   繁体   English

在 numpy 网格中评估每个 position 并插入一个数组

[英]evaluating each position in a numpy meshgrid and inserting an array

My goal is to evaluate a function over a 2D plane and return an RGB value for each point on the plane so that my final output is a nested array with an [RGB] for each pixel.我的目标是在 2D 平面上评估 function 并为平面上的每个点返回一个 RGB 值,以便我的最终 output 是一个嵌套数组,每个像素都有一个 [RGB]。 Here was my first attempt:这是我的第一次尝试:

@np.vectorize
def foo(x,y):
     return [R,G,B]

x = np.linspace(-10,10)
y = np.linspace(-10,10)

xx, yy = np.meshgrid(x,y)

output_array = foo(xx,yy)

which raises:这引发了:

ValueError: setting an array element with a sequence.

I somewhat understand this error?我有点理解这个错误? I found other threads on here about this error being raised, but none of them used meshgrid.我在这里发现了有关此错误的其他线程,但没有一个使用网格网格。 When I replaced the output of foo(x,y) with a single boolean rather than an RGB array, I was generating a reasonable output, but this isn't what I'm looking for.当我用单个 boolean 而不是 RGB 阵列替换foo(x,y)的 output 时,我正在生成一个合理的 output,但这不是我要找的。

My current solution is:我目前的解决方案是:

def foo(x,y):
     return [R,G,B]

a = np.linspace(-10,10)
b = np.linspace(10,10)
arr = np.zeros((10,10,3), dtype = np.uint8)

for i,x in enumerate(a):
    for j,y in enumerate(b):
        arr[i][j] = foo(x,y)

output_array = arr

This works, but it is slow, and it doesn't seem pythonic.这行得通,但速度很慢,而且看起来不像是pythonic。 I'm creating an array and then iterating over it, which from what I've read is a no-no.我正在创建一个数组,然后对其进行迭代,从我读过的内容来看,这是一个禁忌。 Should I do something other than meshgrid?我应该做网格网格以外的事情吗? Is my current solution the best solution?我目前的解决方案是最好的解决方案吗? Thanks谢谢

Vectorize is designed for convenience not necessarily for speed. Vectorize 是为方便而设计的,不一定是为了速度。 It can be quite tricky to work with so if it is not convenient to adapt a function with vectorize it is often better to rewrite the function yourself to handle the indented full scale objects.使用它可能非常棘手,因此如果不方便使用矢量化调整 function,通常最好自己重写 function 以处理缩进的全尺寸对象。 That being said there are a few things going on here.话虽如此,这里发生了一些事情。 First your output is a sequence object not a scalar.首先,您的 output 是序列 object 不是标量。 Numpy is expecting to create an array of scalars the same shape as the input array. Numpy 期望创建一个与输入数组形状相同的标量数组。 The 'signature' keyword parameter will be helpful here, forcing numpy to respect the intended output shape. 'signature' 关键字参数在这里会有所帮助,强制 numpy 尊重预期的 output 形状。 Also you are trying to vecorize over two arrays at once xx and yy.此外,您正在尝试同时对两个 arrays 进行 xx 和 yy 的 vecorize。 This is not really possible as far as I know.据我所知,这实际上是不可能的。 Put xx and yy into a three dimensional array and vectorize over this object.将 xx 和 yy 放入一个三维数组并在此 object 上进行矢量化。 For example with a 5 by 5 grid of x, y values you have the following.例如,对于 x、y 值的 5 x 5 网格,您有以下内容。

Input:输入:

# design foo to take x and y values in one sequence object
def foo(xy):
    x = xy[0]
    y = xy[1]

    # do stuff with x and y and get ouput RGB array
    return np.array([x+y, x*y, x*y])

# force numpy to respect input/ouput array shapes
foo = np.vectorize(foo, signature='(2)->(3)')

# x and y value grids
x = np.linspace(-10, 10, 5)
y = np.linspace(-10, 10, 5)
xx, yy  = np.meshgrid(x, y)

# 3 dimensional array with both x and y values at each grid point
xxyy = np.concatenate([np.expand_dims(xx, axis=2), np.expand_dims(yy, axis=2)], axis=2)

# call foo with only one array to vecorise over
output_array = foo(xxyy)
output_array

Output: Output:

array([[[ -20.,  100.,  100.],
        [ -15.,   50.,   50.],
        [ -10.,   -0.,   -0.],
        [  -5.,  -50.,  -50.],
        [   0., -100., -100.]],

       [[ -15.,   50.,   50.],
        [ -10.,   25.,   25.],
        [  -5.,   -0.,   -0.],
        [   0.,  -25.,  -25.],
        [   5.,  -50.,  -50.]],

       [[ -10.,   -0.,   -0.],
        [  -5.,   -0.,   -0.],
        [   0.,    0.,    0.],
        [   5.,    0.,    0.],
        [  10.,    0.,    0.]],

       [[  -5.,  -50.,  -50.],
        [   0.,  -25.,  -25.],
        [   5.,    0.,    0.],
        [  10.,   25.,   25.],
        [  15.,   50.,   50.]],

       [[   0., -100., -100.],
        [   5.,  -50.,  -50.],
        [  10.,    0.,    0.],
        [  15.,   50.,   50.],
        [  20.,  100.,  100.]]])

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

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