How to generate 2-D Gaussian array using NumPy? - GeeksforGeeks

20 Apr, 2022
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
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.linspace()– 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 packageimport numpy as np # Initializing value of x-axis and y-axis# in the range -1 to 1x, y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10))dst = np.sqrt(x*x+y*y) # Initializing sigma and muusigma = 1muu = 0.000 # Calculating Gaussian arraygauss = 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.602798180.57375342 0.51979489 0.44822088 0.36787944][0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.734443670.69905581 0.63331324 0.54610814 0.44822088][0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.851723080.81068432 0.73444367 0.63331324 0.51979489][0.57375342 0.69905581 0.81068432 0.89483932 0.9401382  0.94013820.89483932 0.81068432 0.69905581 0.57375342][0.60279818 0.73444367 0.85172308 0.9401382  0.98773022 0.987730220.9401382  0.85172308 0.73444367 0.60279818][0.60279818 0.73444367 0.85172308 0.9401382  0.98773022 0.987730220.9401382  0.85172308 0.73444367 0.60279818][0.57375342 0.69905581 0.81068432 0.89483932 0.9401382  0.94013820.89483932 0.81068432 0.69905581 0.57375342][0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.851723080.81068432 0.73444367 0.63331324 0.51979489][0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.734443670.69905581 0.63331324 0.54610814 0.44822088][0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.602798180.57375342 0.51979489 0.44822088 0.36787944]] 
Example 2:
Python3
# Importing Numpy packageimport numpy as np # Initializing value of x-axis and y-axis# in the range -2 to +2x, y = np.meshgrid(np.linspace(-2,2,15), np.linspace(-2,2,15))dst = np.sqrt(x*x+y*y) # Initializing sigma and muusigma = 1muu = 0.000 # Calculating Gaussian arraygauss = 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]]
 
arorakashish0911
Pyhton numpy-arrayCreation
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Python Classes and Objects
Create a directory in Python