w3resource

NumPy: Generate a generic 2D Gaussian-like array

NumPy: Array Object Exercise-79 with Solution

Write a NumPy program to generate a generic 2D Gaussian-like array.

Sample Solution:

Python Code:

# Importing the NumPy library and aliasing it as 'np'
import numpy as np

# Generating 2D grids 'x' and 'y' using meshgrid with 10 evenly spaced points from -1 to 1
x, y = np.meshgrid(np.linspace(-1, 1, 10), np.linspace(-1, 1, 10))

# Calculating the Euclidean distance 'd' from the origin using the generated grids 'x' and 'y'
d = np.sqrt(x*x + y*y)

# Defining parameters sigma and mu for a Gaussian-like distribution
sigma, mu = 1.0, 0.0

# Calculating the Gaussian-like distribution 'g' based on the distance 'd', sigma, and mu
g = np.exp(-((d - mu)**2 / (2.0 * sigma**2)))

# Printing a message indicating a 2D Gaussian-like array will be displayed
print("2D Gaussian-like array:")

# Printing the calculated 2D Gaussian-like array 'g'
print(g) 

Sample Output:

2D Gaussian-like 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]]

Explanation:

In the above code –

  • x, y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10)): Create two 2D arrays ‘x’ and ‘y’ using np.meshgrid and np.linspace. np.linspace creates 10 evenly spaced points between -1 and 1 for both arrays.
  • d = np.sqrt(x*x+y*y): Calculate the Euclidean distance from the origin (0,0) to each coordinate in the grid using the Pythagorean theorem.
  • sigma, mu = 1.0, 0.0: Define the standard deviation sigma and the mean mu of the Gaussian function.
  • g = np.exp(-( (d-mu)**2 / ( 2.0 * sigma**2 ) ) ): This line computes the Gaussian function values for each distance d using the given sigma and mu. This generates a 2D Gaussian-like array, where the values represent the amplitude of the Gaussian function at each grid point.
  • print("2D Gaussian-like array:"): Prints a "2D Gaussian-like array:"
  • print(g): Print the generated 2D Gaussian-like array g.

Python-Numpy Code Editor:

Previous: Write a NumPy program to create a record array from a (flat) list of arrays.
Next: Write a NumPy program to convert a NumPy array into Python list structure.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/numpy/python-numpy-exercise-79.php