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

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

问题描述

生成包含在 n 维点网格上评估的函数值的 numpy 数组的好方法是什么?

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?

附言在 StackOverflow 上已经多次以各种形式询问过这个问题,但我找不到简明扼要的问题和答案.我发布这个是为了提供一个简洁的简单解决方案(如下).

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(xaxis[:,None], yaxis[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[:,None]",使 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 个维度,使用与您拥有的维度一样多的 None:.每个 : 构成一个维度,每个 None 构成一个空"维度.下一个示例更多地展示了这些空维度是如何工作的.如您所见,如果您使用 None,形状会发生变化,这表明在下一个示例中它是一个 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)

无需自己输入 None

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)

通过这种方式,您可以使用 None 切片来创建额外的空维度,方法是将您提供给 ndm 的第一个参数作为第一个完整维度,第二个作为第二个完整维度等 - 它与之前使用的硬编码"无类型语法相同.

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(xaxis, yaxis, zaxis) 和做一样

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 方法也应该适用于更多维度,而无需像刚才显示的那样在多行中对 None 切片进行硬编码.这也适用于 1.8 之前的 numpy 版本,而 numpy.meshgrid 仅适用于高于 2 维的 numpy 1.8 或更高版本.

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天全站免登陆