python中grid函数_Python / NumPy中meshgrid的目的是什么?

实际上文档中已经提到了np.mgrid的目的:

np.mgrid

从坐标向量返回坐标矩阵。

在给定一维坐标数组x1,x2,...,xn的情况下,为N-D网格上的N-D标量/矢量场的矢量化评估制作N-D坐标数组。

所以它的主要目的是创建一个坐标矩阵。

你可能只是问自己:

为什么我们需要创建坐标矩阵?

你需要使用Python / NumPy坐标矩阵的原因是坐标与值之间没有直接的关系,除非你的坐标从零开始并且是纯正整数。 然后你可以使用数组的索引作为索引。但是,如果不是这种情况,您需要在数据旁边存储坐标。 这就是网格进来的地方。

假设您的数据是:

1 2 1

2 5 2

1 2 1

但是,每个值代表水平2公里宽的区域和垂直3公里。 假设您的原点位于左上角,并且您需要表示可以使用的距离的数组:

import numpy as np

h, v = np.meshgrid(np.arange(3)*3, np.arange(3)*2)

其中np.mgrid是:

0 2 4

0 2 4

0 2 4

和h:

0 0 0

3 3 3

6 6 6

所以,如果你有两个指数,那么就说np.mgrid和x(这就是为什么y的返回值通常是x或y而不是np.ogrid而不是np.ogrid为什么我在水平上选择了sparse=True!)然后你可以得到点的x坐标, 通过使用以下点的y坐标和该点的值:

h[x, y] # horizontal coordinate

v[x, y] # vertical coordinate

data[x, y] # value

这样可以更容易地跟踪坐标,并且(更重要的是)您可以将它们传递给需要知道坐标的函数。

一个稍长的解释

但是,np.mgrid本身并不经常直接使用,大多数只使用类似对象之一x或y。这里x代表y和np.ogrid sparse=True案例(我参考sparse参数np.meshgrid)。 请注意,两者之间存在显着差异np.meshgrid和np.ogrid和np.mgrid:前两个返回值(如果有两个或更多)是相反的。 通常这没关系,但你应该给出有意义的变量名称取决于具体情况。 例如,在2D网格和matplotlib.pyplot.imshow的情况下,将第一个返回的项目命名为np.meshgrid x和第二个y是有意义的。另一种方式为np.mgrid和np.ogrid。

np.mgrid和稀疏网格

>>> import numpy as np

>>> yy, xx = np.ogrid[-5:6, -5:6]

>>> xx

array([[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]])

>>> yy

array([[-5],

[-4],

[-3],

[-2],

[-1],

[ 0],

[ 1],

[ 2],

[ 3],

[ 4],

[ 5]])

如前所述,与np.mgrid相比,输出反转,这就是为什么我将其解压缩为x而不是y:

>>> xx, yy = np.meshgrid(np.arange(-5, 6), np.arange(-5, 6), sparse=True)

>>> xx

array([[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]])

>>> yy

array([[-5],

[-4],

[-3],

[-2],

[-1],

[ 0],

[ 1],

[ 2],

[ 3],

[ 4],

[ 5]])

这看起来像坐标,特别是2D图的x和y线。

可视化:

yy, xx = np.ogrid[-5:6, -5:6]

plt.figure()

plt.title('ogrid (sparse meshgrid)')

plt.grid()

plt.xticks(xx.ravel())

plt.yticks(yy.ravel())

plt.scatter(xx, np.zeros_like(xx), color="blue", marker="*")

plt.scatter(np.zeros_like(yy), yy, color="red", marker="x")

np.mgrid和密集/充实的网格

>>> yy, xx = np.mgrid[-5:6, -5:6]

>>> xx

array([[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]])

>>> yy

array([[-5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5],

[-4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4],

[-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3],

[-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2],

[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],

[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],

[ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],

[ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],

[ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],

[ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]])

这同样适用:与np.mgrid相比,输出反转:

>>> xx, yy = np.meshgrid(np.arange(-5, 6), np.arange(-5, 6))

>>> xx

array([[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]])

>>> yy

array([[-5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5],

[-4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4],

[-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3],

[-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2],

[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],

[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],

[ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],

[ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],

[ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],

[ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]])

与np.mgrid不同,这些阵列包含-5< = xx< = 5;中的所有x和y坐标。 -5< = yy< = 5网格。

yy, xx = np.mgrid[-5:6, -5:6]

plt.figure()

plt.title('mgrid (dense meshgrid)')

plt.grid()

plt.xticks(xx[0])

plt.yticks(yy[:, 0])

plt.scatter(xx, yy, color="red", marker="x")

功能

它不仅限于2D,这些函数适用于任意维度(嗯,Python中函数的最大参数数量和NumPy允许的最大维数):

>>> x1, x2, x3, x4 = np.ogrid[:3, 1:4, 2:5, 3:6]

>>> for i, x in enumerate([x1, x2, x3, x4]):

... print('x{}'.format(i+1))

... print(repr(x))

x1

array([[[[0]]],

[[[1]]],

[[[2]]]])

x2

array([[[[1]],

[[2]],

[[3]]]])

x3

array([[[[2],

[3],

[4]]]])

x4

array([[[[3, 4, 5]]]])

>>> # equivalent meshgrid output, note how the first two arguments are reversed and the unpacking

>>> x2, x1, x3, x4 = np.meshgrid(np.arange(1,4), np.arange(3), np.arange(2, 5), np.arange(3, 6), sparse=True)

>>> for i, x in enumerate([x1, x2, x3, x4]):

... print('x{}'.format(i+1))

... print(repr(x))

# Identical output so it's omitted here.

即使这些也适用于1D,有两个(更常见的)1D网格创建功能:

np.mgrid

np.mgrid

除了np.mgrid和x参数之外,它还支持y参数(甚至表示步骤数的复杂步骤):

>>> x1, x2 = np.mgrid[1:10:2, 1:10:4j]

>>> x1 # The dimension with the explicit step width of 2

array([[1., 1., 1., 1.],

[3., 3., 3., 3.],

[5., 5., 5., 5.],

[7., 7., 7., 7.],

[9., 9., 9., 9.]])

>>> x2 # The dimension with the "number of steps"

array([[ 1., 4., 7., 10.],

[ 1., 4., 7., 10.],

[ 1., 4., 7., 10.],

[ 1., 4., 7., 10.],

[ 1., 4., 7., 10.]])

应用

您特别询问了目的,实际上如果您需要坐标系,这些网格非常有用。

例如,如果您有一个NumPy函数来计算二维距离:

def distance_2d(x_point, y_point, x, y):

return np.hypot(x-x_point, y-y_point)

你想知道每个点的距离:

>>> ys, xs = np.ogrid[-5:5, -5:5]

>>> distances = distance_2d(1, 2, xs, ys) # distance to point (1, 2)

>>> distances

array([[9.21954446, 8.60232527, 8.06225775, 7.61577311, 7.28010989,

7.07106781, 7. , 7.07106781, 7.28010989, 7.61577311],

[8.48528137, 7.81024968, 7.21110255, 6.70820393, 6.32455532,

6.08276253, 6. , 6.08276253, 6.32455532, 6.70820393],

[7.81024968, 7.07106781, 6.40312424, 5.83095189, 5.38516481,

5.09901951, 5. , 5.09901951, 5.38516481, 5.83095189],

[7.21110255, 6.40312424, 5.65685425, 5. , 4.47213595,

4.12310563, 4. , 4.12310563, 4.47213595, 5. ],

[6.70820393, 5.83095189, 5. , 4.24264069, 3.60555128,

3.16227766, 3. , 3.16227766, 3.60555128, 4.24264069],

[6.32455532, 5.38516481, 4.47213595, 3.60555128, 2.82842712,

2.23606798, 2. , 2.23606798, 2.82842712, 3.60555128],

[6.08276253, 5.09901951, 4.12310563, 3.16227766, 2.23606798,

1.41421356, 1. , 1.41421356, 2.23606798, 3.16227766],

[6. , 5. , 4. , 3. , 2. ,

1. , 0. , 1. , 2. , 3. ],

[6.08276253, 5.09901951, 4.12310563, 3.16227766, 2.23606798,

1.41421356, 1. , 1.41421356, 2.23606798, 3.16227766],

[6.32455532, 5.38516481, 4.47213595, 3.60555128, 2.82842712,

2.23606798, 2. , 2.23606798, 2.82842712, 3.60555128]])

如果在密集网格而不是开放网格中传递,则输出将是相同的。 NumPys广播使其成为可能!

让我们看看结果:

plt.figure()

plt.title('distance to point (1, 2)')

plt.imshow(distances, origin='lower', interpolation="none")

plt.xticks(np.arange(xs.shape[1]), xs.ravel()) # need to set the ticks manually

plt.yticks(np.arange(ys.shape[0]), ys.ravel())

plt.colorbar()

这也是当NumPys np.mgrid和x变得非常方便时,因为它可以让您轻松更改网格的分辨率:

ys, xs = np.ogrid[-5:5:200j, -5:5:200j]

# otherwise same code as above

但是,由于np.mgrid不支持x和y输入,因此必须手动更改滴答。 如果能接受x和y坐标会非常方便吗?

使用NumPy编写与网格自然对应的函数很容易。 此外,NumPy,SciPy,MatPlotLib中有几个函数可以让你传入网格。

我喜欢图像所以让我们探索np.mgrid:

ys, xs = np.mgrid[-5:5:200j, -5:5:200j]

density = np.sin(ys)-np.cos(xs)

plt.figure()

plt.contour(xs, ys, density)

请注意坐标是如何正确设置的! 如果您刚刚通过np.mgrid,情况就不会如此。

或者使用astropy模型给出另一个有趣的例子(这次我不关心坐标,我只是用它们来创建一些网格):

from astropy.modeling import models

z = np.zeros((100, 100))

y, x = np.mgrid[0:100, 0:100]

for _ in range(10):

g2d = models.Gaussian2D(amplitude=100,

x_mean=np.random.randint(0, 100),

y_mean=np.random.randint(0, 100),

x_stddev=3,

y_stddev=3)

z += g2d(x, y)

a2d = models.AiryDisk2D(amplitude=70,

x_0=np.random.randint(0, 100),

y_0=np.random.randint(0, 100),

radius=5)

z += a2d(x, y)

虽然这只是“为了外观”几个与功能模型和拟合相关的功能(例如np.mgrid,np.mgrid甚至在Scipy等中使用np.mgrid显示示例需要网格。 其中大多数使用开放式网格和密集的网格,但有些只与其中一个一起工作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值