Python/NumPy中的meshgrid的用途是什么? [英] What is the purpose of meshgrid in Python / NumPy?
问题描述
有人可以向我解释Numpy中meshgrid
函数的目的是什么?我知道它会创建某种用于绘制的坐标网格,但我真的看不到它的直接好处.
Can someone explain to me what is the purpose of meshgrid
function in Numpy? I know it creates some kind of grid of coordinates for plotting, but I can't really see the direct benefit of it.
我正在研究Sebastian Raschka的"Python机器学习",他正在使用它来绘制决策边界.请参见输入11 此处.
I am studying "Python Machine Learning" from Sebastian Raschka, and he is using it for plotting the decision borders. See input 11 here.
我也在官方文档中尝试过此代码,但是,再次,输出对我来说真的没有意义.
I have also tried this code from official documentation, but, again, the output doesn't really make sense to me.
x = np.arange(-5, 5, 1)
y = np.arange(-5, 5, 1)
xx, yy = np.meshgrid(x, y, sparse=True)
z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
h = plt.contourf(x,y,z)
如果可能的话,还请给我展示很多真实的例子.
Please, if possible, also show me a lot of real-world examples.
推荐答案
meshgrid
的目的是根据x值数组和y值数组创建矩形网格.
The purpose of meshgrid
is to create a rectangular grid out of an array of x values and an array of y values.
因此,例如,如果要创建一个网格,则在x和y方向上每个介于0和4之间的整数值处都有一个点.要创建矩形网格,我们需要x
和y
点的每种组合.
So, for example, if we want to create a grid where we have a point at each integer value between 0 and 4 in both the x and y directions. To create a rectangular grid, we need every combination of the x
and y
points.
这将是25分,对吧?因此,如果我们想为所有这些点创建一个x和y数组,我们可以执行以下操作.
This is going to be 25 points, right? So if we wanted to create an x and y array for all of these points, we could do the following.
x[0,0] = 0 y[0,0] = 0
x[0,1] = 1 y[0,1] = 0
x[0,2] = 2 y[0,2] = 0
x[0,3] = 3 y[0,3] = 0
x[0,4] = 4 y[0,4] = 0
x[1,0] = 0 y[1,0] = 1
x[1,1] = 1 y[1,1] = 1
...
x[4,3] = 3 y[4,3] = 4
x[4,4] = 4 y[4,4] = 4
这将导致以下x
和y
矩阵,这样,每个矩阵中对应元素的配对将给出网格中某个点的x和y坐标.
This would result in the following x
and y
matrices, such that the pairing of the corresponding element in each matrix gives the x and y coordinates of a point in the grid.
x = 0 1 2 3 4 y = 0 0 0 0 0
0 1 2 3 4 1 1 1 1 1
0 1 2 3 4 2 2 2 2 2
0 1 2 3 4 3 3 3 3 3
0 1 2 3 4 4 4 4 4 4
然后我们可以绘制这些图形以确认它们是网格:
We can then plot these to verify that they are a grid:
plt.plot(x,y, marker='.', color='k', linestyle='none')
显然,这变得非常乏味,尤其是对于大范围的x
和y
.实际上,meshgrid
可以为我们生成此代码:我们只需指定唯一的x
和y
值即可.
Obviously, this gets very tedious especially for large ranges of x
and y
. Instead, meshgrid
can actually generate this for us: all we have to specify are the unique x
and y
values.
xvalues = np.array([0, 1, 2, 3, 4]);
yvalues = np.array([0, 1, 2, 3, 4]);
现在,当我们调用meshgrid
时,我们将自动获得先前的输出.
Now, when we call meshgrid
, we get the previous output automatically.
xx, yy = np.meshgrid(xvalues, yvalues)
plt.plot(xx, yy, marker='.', color='k', linestyle='none')
创建这些矩形网格对于许多任务很有用.在您的帖子中提供的示例中,这只是在x
和y
的值范围内对函数(sin(x**2 + y**2) / (x**2 + y**2)
)进行采样的一种简单方法.
Creation of these rectangular grids is useful for a number of tasks. In the example that you have provided in your post, it is simply a way to sample a function (sin(x**2 + y**2) / (x**2 + y**2)
) over a range of values for x
and y
.
由于此功能已在矩形网格上采样,因此现在可以将其可视化为图像".
Because this function has been sampled on a rectangular grid, the function can now be visualized as an "image".
另外,现在可以将结果传递给期望在矩形网格上显示数据的函数(即contourf
)
Additionally, the result can now be passed to functions which expect data on rectangular grid (i.e. contourf
)
这篇关于Python/NumPy中的meshgrid的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!