Understanding the Numpy mgrid() function in Python

In this article, Hello programmers, we will discuss the numpy mgrid() function in python provided by the Numpy library. The mgrid () function helps to get a dense multi-dimensional ‘meshgrid’. An instance of numpy.lib.index_tricks.nd_grid returns a fleshed out mesh-grid when indexed. Each argument returned has the same shape. The number and dimension of the output arrays are equal to the number of indexing dimensions.

Syntax of Numpy mgrid() function:

numpy.mgrid = <numpy.lib.index_tricks.nd_grid object>

Return value:

Mesh grid of multi dimensional arrays all of same size and dimension.

Examples for different step size

Example 1: For natural non complex number

import numpy as np
a = np.mgrid[0:4, 0:4]
print(a)

Output:

 array([[[0, 0, 0, 0],
        [1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3]],

       [[0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3]]])

Example 2: For complex number –

import numpy as np
a = np.mgrid[-2:2:4j]
print(a)

Output:

array([-2.0, -0.66666667, 0.66666667,  2.0])

Explanation:

With the above examples, we see how the Numpy mgrid() function is used to obtain ndarrays. The ‘ndarrays’ are of the same dimension. The stop value is not inclusive. If the step length is not a complex number. However, if the step length is a complex number (e.g., 4j). Then the integer part of its magnitude is interpreted. And the number of points to create between the start and stop. In this case, the stop value is inclusive.

Numpy mgrid() v/s meshgrid() function in Python

Numpy mgrid() fleshes out the indexes given by broadcasting them to form dense grids. Simultaneously, the meshgrid() function is useful for creating coordinate arrays. And to vectorize function evaluations over a grid. Meshgrid() function is inspired by MATLAB. The meshgrid() basically creates a rectangular grid out of two given one-dimensional arrays representing the Cartesian indexes.

Example of Numpy meshgrid() function:

import numpy as np 

x = np.linspace(-4, 4, 9) 
  
# numpy.linspace creates an array of 9 linearly placed elements between -4 and 4, both inclusive  

y = np.linspace(-5, 5, 11) 
  
x_1, y_1 = np.meshgrid(x, y) 
  
print("x_1 = ") 
print(x_1) 
print("y_1 = ") 
print(y_1) 

Output:

x_1 = 
[[-4. -3. -2. -1.  0.  1.  2.  3.  4.]
 [-4. -3. -2. -1.  0.  1.  2.  3.  4.]
 [-4. -3. -2. -1.  0.  1.  2.  3.  4.]
 [-4. -3. -2. -1.  0.  1.  2.  3.  4.]
 [-4. -3. -2. -1.  0.  1.  2.  3.  4.]
 [-4. -3. -2. -1.  0.  1.  2.  3.  4.]
 [-4. -3. -2. -1.  0.  1.  2.  3.  4.]
 [-4. -3. -2. -1.  0.  1.  2.  3.  4.]
 [-4. -3. -2. -1.  0.  1.  2.  3.  4.]
 [-4. -3. -2. -1.  0.  1.  2.  3.  4.]
 [-4. -3. -2. -1.  0.  1.  2.  3.  4.]]
y_1 = 
[[-5. -5. -5. -5. -5. -5. -5. -5. -5.]
 [-4. -4. -4. -4. -4. -4. -4. -4. -4.]
 [-3. -3. -3. -3. -3. -3. -3. -3. -3.]
 [-2. -2. -2. -2. -2. -2. -2. -2. -2.]
 [-1. -1. -1. -1. -1. -1. -1. -1. -1.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 1.  1.  1.  1.  1.  1.  1.  1.  1.]
 [ 2.  2.  2.  2.  2.  2.  2.  2.  2.]
 [ 3.  3.  3.  3.  3.  3.  3.  3.  3.]
 [ 4.  4.  4.  4.  4.  4.  4.  4.  4.]
 [ 5.  5.  5.  5.  5.  5.  5.  5.  5.]]

Numpy mgrid() v/s ogrid() function in Python

Unlike mgrid() function, which converts indexes into dense mesh grids of the same sizes, the Ogrid stands for “open grid.”It basically provides a way to act on an image’s specific pixels based on their row and column index. 

Basic example of Numpy ogrid():

from numpy import ogrid

x,y = np.ogrid[0:11, 0:6]

print(x)

print(y)

Output:

array([[0],
       [1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7],
       [8],
       [9],
       [10]])

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

Numpy mgrid() using vstack() function in Python

The vstack() function can be used to implement mgrid() function in Python. The vstack() function is used to stack the sequence of input arrays vertically to make a single array. The stacked array is similar to the dense grids formed when the mgrid() function is used. The only point of difference between mgrid() and vstack is in their input values. The mgrid() returns dense grids based on the input index values. Whereas vstack just stacks the input arrays into a single array.

Example of Numpy vstack():

import numpy as np 
   
in_a1 = np.array([[ 1, 2, 3], [ -1, -2, -3]] ) 
print ("1st Input array : \n", in_a1)  
  
in_a2 = np.array([[ 4, 5, 6], [ -4, -5, -6]] ) 
print ("2nd Input array : \n", in_a2)  
  
# Stacking the two arrays vertically 
out_arr = np.vstack((in_arr1, in_arr2)) 
print ("Output stacked array :\n ", out_arr) 

Output:

1st Input array : 
 [[ 1  2  3]
  [-1 -2 -3]]
2nd Input array : 
 [[ 4  5  6]
  [-4 -5 -6]]
Output stacked array :
  [[ 1  2  3]
   [-1 -2 -3]
   [ 4  5  6]
   [-4 -5 -6]]

Numpy linspace() function to implement mgrid() function

The numpy.linspace() function returns number spaces evenly concerning the interval. We can implement mgrid() function using linspace() as it returns an output array just like that given by mgrid() function. However, it takes the intervals to inform sample numbers, unlike step size as in the case of mgrid().

Example of numpy.linspace():

import numpy as np 
  
# restep set to True 
print("A\n", np.linspace(2.0, 3.0, num=5, retstep=True), "\n") 
  

Output:

[ 0.          0.22039774  0.42995636  0.6183698   0.77637192  0.8961922
  0.9719379   0.99988386  0.9786557   0.90929743]

What’s Next?

NumPy is mighty and incredibly essential for information science in Python. That being true if you are interested in data science in Python. You really ought to find out more about Python.

You might like our following tutorials on numpy.

Conclusion

We have discussed using the numpy mgrid() function. And also various ways of implementing it. There are very subtle differences between Numpy mgrid(), mesh grid(), and ogrid() functions, which are also highlighted in this article. The linspace() function of is used to implement the mgrid() function in python up to some extent. It has also been discussed with examples. I hope you find this article useful while implementing the Numpy mgrid() function in your python programs.

If you still have any questions regarding the NumPy multiply function. Leave your question in the comments below.

Happy Pythonning!

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
x