Description of the Meshgrid function in Numpy and two application scenarios: numpymeshgrid
Recently, we have seen the use of meshgrid in several places, although we have noticed the use of meshgrid before.
However, I am not very impressed with the use cases of meshgrid.
Therefore, this article will further introduce the usage of meshgrid in Numpy.
Basic usage of the Meshgrid Function
In the official Numpy article, the English description of the meshgrid function is also very complicated and difficult to understand.
As you can understand it, the meshgrid function uses the points on the two coordinate axes to draw a grid on the plane.
Usage:
[X, Y] = meshgrid (x, y)
[X, Y] = meshgrid (x) is equivalent to [X, Y] = meshgrid (x, x ).
[X, Y, Z] = meshgrid (x, y, z) to generate a three-dimensional array, which can be used to calculate the Three-variant function and draw a three-dimensional map
Here, we will take [X, Y] = meshgrid (x, y) as an example to introduce this function.
[X, Y] = meshgrid (x, y) converts the regions defined by vectors x and y into matrices X and Y. The row vectors of matrix X are simple copies of vector x, the column vector of matrix Y is a simple copy of vector y (Note: In the following code, both X and Y are arrays, which are collectively referred to as matrices in this article ).
Assume that x is a vector with the length of m and y is a vector with the length of n, the final matrix X and Y are all n * m (note not m * n ).
The text description may not be easy to understand. The following code is used for Demonstration:
Load data
import numpy as npimport matplotlib.pyplot as plt%matplotlib inlinem, n = (5, 3)x = np.linspace(0, 1, m)y = np.linspace(0, 1, n)X, Y = np.meshgrid(x,y)
View vector x and vector y
xout:array([ 0. , 0.25, 0.5 , 0.75, 1. ])yout:array([ 0. , 0.5, 1. ])
View matrix X and matrix Y
Xout:array([[ 0. , 0.25, 0.5 , 0.75, 1. ], [ 0. , 0.25, 0.5 , 0.75, 1. ], [ 0. , 0.25, 0.5 , 0.75, 1. ]])Yout:array([[ 0. , 0. , 0. , 0. , 0. ], [ 0.5, 0.5, 0.5, 0.5, 0.5], [ 1. , 1. , 1. , 1. , 1. ]])
View the dimension corresponding to the matrix
X.shapeout:(3, 5)Y.shapeout:(3, 5)
The running process of the meshgrid function can be further understood through the following:
Furthermore, you can visualize the matplotlib to view the gridded data results after the function is run.
plt.plot(X, Y, marker='.', color='blue', linestyle='none')plt.show()
Of course, we can also obtain coordinate point data on the grid plane, as shown below:
z = [i for i in zip(X.flat,Y.flat)]zout:[(0.0, 0.0), (0.25, 0.0), (0.5, 0.0), (0.75, 0.0), (1.0, 0.0), (0.0, 0.5), (0.25, 0.5), (0.5, 0.5), (0.75, 0.5), (1.0, 0.5), (0.0, 1.0), (0.25, 1.0), (0.5, 1.0), (0.75, 1.0), (1.0, 1.0)]
Some application scenarios of Meshgrid Functions
Common scenarios of the Meshgrid function include contour plot and SVC over-plane plot in Machine Learning (two-dimensional scenario ).
They are shown as follows:
(1) contour lines
(2) Draw the Super Plane of SVC:
Scenarios (1) and scenarios (2) will be further described in subsequent articles.
Of course, there may be other scenarios, so we will not introduce them further here.
If you like my article, follow the public account: Python data path (ID: PyDataRoad)