w3resource

NumPy: Generate a generic 2D Gaussian-like array


Generate 2D Gaussian-Like Array

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.

For more Practice: Solve these Related Problems:

  • Write a NumPy program to generate a 2D array that mimics a Gaussian distribution using meshgrid and exponential functions.
  • Create a function that returns a normalized 2D Gaussian-like array and verifies that the peak value is at the center.
  • Generate a 2D Gaussian-like pattern and adjust parameters to observe changes in spread and amplitude.
  • Implement a Gaussian filter on an image array and compare the result with a manually generated 2D Gaussian array.

Go to:


PREV : Create Record Array from Flat Lists
NEXT : Convert NumPy Array to Python List


Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.