numpy的 - 在点网格评价函数 [英] numpy - evaluate function on a grid of points

查看:210
本文介绍了numpy的 - 在点网格评价函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是产生含有上的点的一个n维栅格

What is a good way to produce a numpy array containing the values of a function evaluated on an n-dimensional grid of points?

例如,假设我要评估由

def func(x, y):
    return <some function of x and y>

假设我要评估它的点的二维阵列的x值从0到4的十步,而y值在二十步打算从-1到1上。是什么在numpy的做到这一点的好办法?

Suppose I want to evaluate it on a two dimensional array of points with the x values going from 0 to 4 in ten steps, and the y values going from -1 to 1 in twenty steps. What's a good way to do this in numpy?

P.S。这已被要求以各种形式在计算器上很多次,但我无法找到一个简明地陈述问题和答案。我张贴这提供了一个简明的简单的解决方案(如下图)。

P.S. This has been asked in various forms on StackOverflow many times, but I couldn't find a concisely stated question and answer. I posted this to provide a concise simple solution (below).

推荐答案

更​​短,速度更快,更清晰的答案,避免meshgrid:

shorter, faster and clearer answer, avoiding meshgrid:

import numpy as np

def func(x, y):
    return np.sin(y * x)

xaxis = np.linspace(0, 4, 10)
yaxis = np.linspace(-1, 1, 20)
result = func(x[:,None], y[None,:])

这将是更快的内存,如果你得到的东西如x ^ 2 + Y的函数,因为比X ^ 2一维数组(而不是二维的)上进行,并且在尺寸的增加只发生在你做+。为meshgrid中,x ^ 2将一个二维数组上进行,其中,基本上每行是相同的,从而导致大量的时间增加。

This will be faster in memory if you get something like x^2+y as function, since than x^2 is done on a 1D array (instead of a 2D one), and the increase in dimension only happens when you do the "+". For meshgrid, x^2 will be done on a 2D array, in which essentially every row is the same, causing massive time increases.

编辑:X [:,无],使得X要一个二维数组,但有一个空的第二个维度。这种无是相同的用×[:,numpy.newaxis]。同样的事情和Y做,但做一个空的第一个维度。

the "x[:,None]", makes x to a 2D array, but with an empty second dimension. This "None" is the same as using "x[:,numpy.newaxis]". The same thing is done with Y, but with making an empty first dimension.

编辑:在3个维度:

def func2(x, y, z):
    return np.sin(y * x)+z

xaxis = np.linspace(0, 4, 10)
yaxis = np.linspace(-1, 1, 20)
zaxis = np.linspace(0, 1, 20)
result2 = func2(xaxis[:,None,None], yaxis[None,:,None],zaxis[None,None,:])

这样,您就可以轻松地扩展到n维,如果你愿意的话,使用尽可能多的为你有尺寸。每个使得维度,每个使空的层面。下一个示例显示多一点这些空尺寸是如何工作的。正如你所看到的,形状的变化,如果你使用,表明它是在下面的例子中3D对象,但空尺寸只有当你用乘法被填满这实际上有东西在这些维度的对象(听起来很复杂,但接下来的例子显示了我的意思)

This way you can easily extend to n dimensions if you wish, using as many None or : as you have dimensions. Each : makes a dimension, and each None makes an "empty" dimension. The next example shows a bit more how these empty dimensions work. As you can see, the shape changes if you use None, showing that it is a 3D object in the next example, but the empty dimensions only get filled up whenever you multiply with an object that actually has something in those dimensions (sounds complicated, but the next example shows what i mean)

In [1]: import numpy

In [2]: a = numpy.linspace(-1,1,20)

In [3]: a.shape
Out[3]: (20,)

In [4]: a[None,:,None].shape 
Out[4]: (1, 20, 1)

In [5]: b = a[None,:,None] # this is a 3D array, but with the first and third dimension being "empty"
In [6]: c = a[:,None,None] # same, but last two dimensions are "empty" here

In [7]: d=b*c 

In [8]: d.shape # only the last dimension is "empty" here
Out[8]: (20, 20, 1)

编辑:无需输入自己无

def ndm(*args):
    return [x[(None,)*i+(slice(None),)+(None,)*(len(args)-i-1)] for i, x in enumerate(args)]


x2,y2,z2  = ndm(xaxis,yaxis,zaxis)
result3 = func2(x2,y2,z2)

这样,你让 -slicing创建额外的空维度,通过你给NDM作为第一个全尺寸的第一个参数,第二个作为第二个全尺寸etc-它像以前一样使用硬codeD无类型的语法。

This way, you make the None-slicing to create the extra empty dimensions, by making the first argument you give to ndm as the first full dimension, the second as second full dimension etc- it does the same as the 'hardcoded' None-typed syntax used before.

简短的解释:这样做 X2,Y2,Z2 = NDM(x轴,y轴,z轴)是一样的做

Short explanation: doing x2, y2, z2 = ndm(xaxis, yaxis, zaxis) is the same as doing

x2 = xaxis[:,None,None]
y2 = yaxis[None,:,None]
z2 = zaxis[None,None,:]

但NDM方法还应该适用于更多的维度,而无需硬code中的就像刚在显示多行-slices。这也将在numpy的1.8版本之前的工作,而如果你有numpy的1.8或更高numpy.meshgrid仅适用于大于2的尺寸。

but the ndm method should also work for more dimensions, without needing to hardcode the None-slices in multiple lines like just shown. This will also work in numpy versions before 1.8, while numpy.meshgrid only works for higher than 2 dimensions if you have numpy 1.8 or higher.

这篇关于numpy的 - 在点网格评价函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆