In this article, Let’s discuss how to generate a 2-D Gaussian array using NumPy. To create a 2 D Gaussian array using Numpy python module
Functions used:
- numpy.meshgrid()– It is used to create a rectangular grid out of two given one-dimensional arrays representing the Cartesian indexing or Matrix indexing.
Syntax:
numpy.meshgrid(*xi, copy=True, sparse=False, indexing=’xy’)
- numpy.linespace()– returns number spaces evenly w.r.t interval.
Syntax:
numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None)
- numpy.exp()– this mathematical function helps the user to calculate the exponential of all the elements in the input array.
Syntax:
numpy.exp(array, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None)
Example 1:
Python3
# Importing Numpy package import numpy as np # Initializing value of x-axis and y-axis # in the range -1 to 1 x, y = np.meshgrid(np.linspace( - 1 , 1 , 10 ), np.linspace( - 1 , 1 , 10 )) dst = np.sqrt(x * x + y * y) # Intializing sigma and muu sigma = 1 muu = 0.000 # Calculating Gaussian array gauss = np.exp( - ( (dst - muu) * * 2 / ( 2.0 * sigma * * 2 ) ) ) print ( "2D Gaussian array :\n" ) print (gauss) |
Output:
2D Gaussian array:
[[0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818
0.57375342 0.51979489 0.44822088 0.36787944]
[0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.73444367
0.69905581 0.63331324 0.54610814 0.44822088]
[0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.85172308
0.81068432 0.73444367 0.63331324 0.51979489]
[0.57375342 0.69905581 0.81068432 0.89483932 0.9401382 0.9401382
0.89483932 0.81068432 0.69905581 0.57375342]
[0.60279818 0.73444367 0.85172308 0.9401382 0.98773022 0.98773022
0.9401382 0.85172308 0.73444367 0.60279818]
[0.60279818 0.73444367 0.85172308 0.9401382 0.98773022 0.98773022
0.9401382 0.85172308 0.73444367 0.60279818]
[0.57375342 0.69905581 0.81068432 0.89483932 0.9401382 0.9401382
0.89483932 0.81068432 0.69905581 0.57375342]
[0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.85172308
0.81068432 0.73444367 0.63331324 0.51979489]
[0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.73444367
0.69905581 0.63331324 0.54610814 0.44822088]
[0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818
0.57375342 0.51979489 0.44822088 0.36787944]]
Example 2:
Python3
# Importing Numpy package import numpy as np # Initializing value of x-axis and y-axis # in the range -2 to +2 x, y = np.meshgrid(np.linspace( - 2 , 2 , 15 ), np.linspace( - 2 , 2 , 15 )) dst = np.sqrt(x * x + y * y) # Intializing sigma and muu sigma = 1 muu = 0.000 # Calculating Gaussian array gauss = np.exp( - ( (dst - muu) * * 2 / ( 2.0 * sigma * * 2 ) ) ) print ( "2D Gaussian array :\n" ) print (gauss) |
Output:
2D Gaussian array:
[[0.01831564 0.03113609 0.0487813 0.07043526 0.09372907 0.11494916
0.12992261 0.13533528 0.12992261 0.11494916 0.09372907 0.07043526
0.0487813 0.03113609 0.01831564]
[0.03113609 0.0529305 0.08292689 0.11973803 0.15933686 0.19541045
0.2208649 0.2300663 0.2208649 0.19541045 0.15933686 0.11973803
0.08292689 0.0529305 0.03113609]
[0.0487813 0.08292689 0.12992261 0.1875951 0.24963508 0.30615203
0.34603184 0.36044779 0.34603184 0.30615203 0.24963508 0.1875951
0.12992261 0.08292689 0.0487813 ]
[0.07043526 0.11973803 0.1875951 0.27086833 0.36044779 0.44205254
0.49963495 0.52045012 0.49963495 0.44205254 0.36044779 0.27086833
0.1875951 0.11973803 0.07043526]
[0.09372907 0.15933686 0.24963508 0.36044779 0.47965227 0.58824471
0.66487032 0.69256932 0.66487032 0.58824471 0.47965227 0.36044779
0.24963508 0.15933686 0.09372907]
[0.11494916 0.19541045 0.30615203 0.44205254 0.58824471 0.72142229
0.81539581 0.84936582 0.81539581 0.72142229 0.58824471 0.44205254
0.30615203 0.19541045 0.11494916]
[0.12992261 0.2208649 0.34603184 0.49963495 0.66487032 0.81539581
0.92161045 0.96000544 0.92161045 0.81539581 0.66487032 0.49963495
0.34603184 0.2208649 0.12992261]
[0.13533528 0.2300663 0.36044779 0.52045012 0.69256932 0.84936582
0.96000544 1. 0.96000544 0.84936582 0.69256932 0.52045012
0.36044779 0.2300663 0.13533528]
[0.12992261 0.2208649 0.34603184 0.49963495 0.66487032 0.81539581
0.92161045 0.96000544 0.92161045 0.81539581 0.66487032 0.49963495
0.34603184 0.2208649 0.12992261]
[0.11494916 0.19541045 0.30615203 0.44205254 0.58824471 0.72142229
0.81539581 0.84936582 0.81539581 0.72142229 0.58824471 0.44205254
0.30615203 0.19541045 0.11494916]
[0.09372907 0.15933686 0.24963508 0.36044779 0.47965227 0.58824471
0.66487032 0.69256932 0.66487032 0.58824471 0.47965227 0.36044779
0.24963508 0.15933686 0.09372907]
[0.07043526 0.11973803 0.1875951 0.27086833 0.36044779 0.44205254
0.49963495 0.52045012 0.49963495 0.44205254 0.36044779 0.27086833
0.1875951 0.11973803 0.07043526]
[0.0487813 0.08292689 0.12992261 0.1875951 0.24963508 0.30615203
0.34603184 0.36044779 0.34603184 0.30615203 0.24963508 0.1875951
0.12992261 0.08292689 0.0487813 ]
[0.03113609 0.0529305 0.08292689 0.11973803 0.15933686 0.19541045
0.2208649 0.2300663 0.2208649 0.19541045 0.15933686 0.11973803
0.08292689 0.0529305 0.03113609]
[0.01831564 0.03113609 0.0487813 0.07043526 0.09372907 0.11494916
0.12992261 0.13533528 0.12992261 0.11494916 0.09372907 0.07043526
0.0487813 0.03113609 0.01831564]]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.