假设我想为网格上的每个点计算一个值.我会定义一些函数 func,它接受两个值 x 和 y 作为参数并返回第三个值.在下面的示例中,计算此值需要在外部字典中查找.然后我会生成一个点网格并在每个点上评估 func 以获得我想要的结果.
下面的代码正是这样做的,但有点迂回.首先,我将 X 和 Y 坐标矩阵都重新整形为一维数组,计算所有值,然后将结果重新整形为矩阵.我的问题是,这可以以更优雅的方式完成吗?
将集合导入为 c# 一些任意的查找表a = c.defaultdict(int)[1] = 2[2] = 3[3] = 2[4] = 3定义函数(x,y):# 一些任意函数返回 a[x] + a[y]X,Y = np.mgrid[1:3, 1:4]X = X.TY = Y.TZ = np.array([func(x,y) for (x,y) in zip(X.ravel(), Y.ravel())]).reshape(X.shape)打印 Z此代码的目的是生成一组值,我可以将这些值与 matplotlib 中的 pcolor 一起使用以创建热图类型的图.
解决方案我会使用 numpy.vectorize 来矢量化"你的函数.请注意,尽管名称如此,vectorize 并不是为了让您的代码运行得更快——只是稍微简化一下.
以下是一些示例:
>>>将 numpy 导入为 np>>>@np.vectorize... def foo(a, b):...返回 a + b...>>>foo([1,3,5], [2,4,6])数组([ 3, 7, 11])>>>foo(np.arange(9).reshape(3,3), np.arange(9).reshape(3,3))数组([[ 0, 2, 4],[ 6, 8, 10],[12, 14, 16]])使用您的代码,用 np.vectorize 装饰 func 应该就足够了,然后您可以将其称为 func(X, Y) -- 不需要 raveling 或 reshapeing :
将 numpy 导入为 np将集合导入为 c# 一些任意的查找表a = c.defaultdict(int)[1] = 2[2] = 3[3] = 2[4] = 3@np.vectorize定义函数(x,y):# 一些任意函数返回 a[x] + a[y]X,Y = np.mgrid[1:3, 1:4]X = X.TY = Y.TZ = func(X, Y)Say I want to calculate a value for every point on a grid. I would define some function func that takes two values x and y as parameters and returns a third value. In the example below, calculating this value requires a look-up in an external dictionary. I would then generate a grid of points and evaluate func on each of them to get my desired result.
The code below does precisely this, but in a somewhat roundabout way. First I reshape both the X and Y coordinate matrices into one-dimensional arrays, calculate all the values, and then reshape the result back into a matrix. My questions is, can this be done in a more elegant manner?
import collections as c # some arbitrary lookup table a = c.defaultdict(int) a[1] = 2 a[2] = 3 a[3] = 2 a[4] = 3 def func(x,y): # some arbitrary function return a[x] + a[y] X,Y = np.mgrid[1:3, 1:4] X = X.T Y = Y.T Z = np.array([func(x,y) for (x,y) in zip(X.ravel(), Y.ravel())]).reshape(X.shape) print ZThe purpose of this code is to generate a set of values that I can use with pcolor in matplotlib to create a heatmap-type plot.
解决方案I'd use numpy.vectorize to "vectorize" your function. Note that despite the name, vectorize is not intended to make your code run faster -- Just simplify it a bit.
Here's some examples:
>>> import numpy as np >>> @np.vectorize ... def foo(a, b): ... return a + b ... >>> foo([1,3,5], [2,4,6]) array([ 3, 7, 11]) >>> foo(np.arange(9).reshape(3,3), np.arange(9).reshape(3,3)) array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]])With your code, it should be enough to decorate func with np.vectorize and then you can probably just call it as func(X, Y) -- No raveling or reshapeing necessary:
import numpy as np import collections as c # some arbitrary lookup table a = c.defaultdict(int) a[1] = 2 a[2] = 3 a[3] = 2 a[4] = 3 @np.vectorize def func(x,y): # some arbitrary function return a[x] + a[y] X,Y = np.mgrid[1:3, 1:4] X = X.T Y = Y.T Z = func(X, Y)
更多推荐
如何将某些函数应用于 python 网格网格?
发布评论