QBoard » Artificial Intelligence & ML » AI and ML - Python » What is the purpose of meshgrid in Python / NumPy?

What is the purpose of meshgrid in Python / NumPy?

  • 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.

    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.

      September 13, 2021 1:44 PM IST
    0
  • Basic Idea
    Given possible x values, xs, (think of them as the tick-marks on the x-axis of a plot) and possible y values, ys, meshgrid generates the corresponding set of (x, y) grid points---analogous to set((x, y) for x in xs for y in yx). For example, if xs=[1,2,3] and ys=[4,5,6], we'd get the set of coordinates {(1,4), (2,4), (3,4), (1,5), (2,5), (3,5), (1,6), (2,6), (3,6)}.

    Form of the Return Value
    However, the representation that meshgrid returns is different from the above expression in two ways:

    First, meshgrid lays out the grid points in a 2d array: rows correspond to different y-values, columns correspond to different x-values---as in list(list((x, y) for x in xs) for y in ys), which would give the following array:

     [[(1,4), (2,4), (3,4)],
        [(1,5), (2,5), (3,5)],
        [(1,6), (2,6), (3,6)]]​

    Second, meshgrid returns the x and y coordinates separately (i.e. in two different numpy 2d arrays):


    xcoords, ycoords = (
           array([[1, 2, 3],
                  [1, 2, 3],
                  [1, 2, 3]]),
           array([[4, 4, 4],
                  [5, 5, 5],
                  [6, 6, 6]]))
       # same thing using np.meshgrid:
       xcoords, ycoords = np.meshgrid([1,2,3], [4,5,6])
       # same thing without meshgrid:
       xcoords = np.array([xs] * len(ys)
       ycoords = np.array([ys] * len(xs)).T​


    Note, np.meshgrid can also generate grids for higher dimensions. Given xs, ys, and zs, you'd get back xcoords, ycoords, zcoords as 3d arrays. meshgrid also supports reverse ordering of the dimensions as well as sparse representation of the result.

    Applications
    Why would we want this form of output?

    Apply a function at every point on a grid: One motivation is that binary operators like (+, -, *, /, **) are overloaded for numpy arrays as elementwise operations. This means that if I have a function def f(x, y): return (x - y) ** 2 that works on two scalars, I can also apply it on two numpy arrays to get an array of elementwise results: e.g. f(xcoords, ycoords) or f(*np.meshgrid(xs, ys)) gives the following on the above example:

    array([[ 9,  4,  1],
           [16,  9,  4],
           [25, 16,  9]])​


    Higher dimensional outer product: I'm not sure how efficient this is, but you can get high-dimensional outer products this way: np.prod(np.meshgrid([1,2,3], [1,2], [1,2,3,4]), axis=0).

    Contour plots in matplotlib: I came across meshgrid when investigating drawing contour plots with matplotlib for plotting decision boundaries. For this, you generate a grid with meshgrid, evaluate the function at each grid point (e.g. as shown above), and then pass the xcoords, ycoords, and computed f-values (i.e. zcoords) into the contourf function.
      September 29, 2021 2:32 PM IST
    0
  • The purpose of meshgrid is to create a rectangular grid out of an array of x values and an array of y values.
    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.
    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
    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')
    enter image description here
    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]);
    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')
    enter image description here
    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".
    enter image description here
    Additionally, the result can now be passed to functions which expect data on rectangular grid (i.e. contourf)
      September 13, 2021 6:29 PM IST
    0
  • Short answer

    The purpose of meshgrid is to help replace Python loops (slow interpreted code) by vectorized operations within C NumPy library.

    Borrowed from this site.

    enter image description here

    x = np.arange(-4, 4, 0.25)
    y = np.arange(-4, 4, 0.25)
    X, Y = np.meshgrid(x, y)
    R = np.sqrt(X**2 + Y**2)
    Z = np.sin(R)

    meshgrid is used to create pairs of coordinates between -4 and +4 with .25 increments in each direction X and Y. Each pair is then used to find R, and Z from it. This way of preparing "a grid" of coordinates is frequently used in plotting 3D surfaces, or coloring 2D surfaces.

      October 2, 2021 2:26 PM IST
    0
  • meshgrid helps in creating a rectangular grid from two 1-D arrays of all pairs of points from the two arrays.

    x = np.array([0, 1, 2, 3, 4])
    y = np.array([0, 1, 2, 3, 4])

     

    Now, if you have defined a function f(x,y) and you wanna apply this function to all the possible combination of points from the arrays 'x' and 'y', then you can do this:

    f(*np.meshgrid(x, y))
    

     

    Say, if your function just produces the product of two elements, then this is how a cartesian product can be achieved, efficiently for large arrays.

      September 18, 2021 12:52 PM IST
    0