如何使用 NumPy 生成二维高斯数组?
在本文中,让我们讨论如何使用 NumPy 生成二维高斯数组。使用 Numpy Python模块创建二维高斯数组
使用的功能:
- numpy.meshgrid() –它 用于从表示笛卡尔索引或矩阵索引的两个给定一维数组中创建矩形网格。
句法:
numpy.meshgrid(*xi, copy=True, sparse=False, indexing=’xy’)
- numpy.linespace() –均匀地返回数字空间 wrt 间隔。
句法:
numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None)
- numpy.exp() -他的数学函数帮助用户计算输入数组中所有元素的指数。
句法:
numpy.exp(array, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None)
示例 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)
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)
输出:
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]]
示例 2:
蟒蛇3
# 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)
输出:
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]]