如何将某些函数应用于 python 网格网格?

编程入门 行业动态 更新时间:2024-10-24 01:48:35
本文介绍了如何将某些函数应用于 python 网格网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我想为网格上的每个点计算一个值.我会定义一些函数 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 Z

The 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 网格网格?

本文发布于:2023-11-23 04:44:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620133.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   应用于   如何将   函数   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!