Python在点周围的半径内添加高斯噪声

Python在点周围的半径内添加高斯噪声,python,numpy,scipy,gaussian,Python,Numpy,Scipy,Gaussian,给定一个点(x,y),我如何创建n个随机点,这些点与(x,y)的距离是以σ为高斯分布,并以均值为参数?您必须使用多变量正态分布。对于您的情况,必须在X轴和Y轴上使用正态分布。如果绘制分布图,它将是一条三维钟形曲线 使用numpy的多元正态分布 numpy.random.multivariate_normal(mean, cov[, size]) mean : 1-D array_like, of length N Mean of the N-dimensional distribution.

给定一个点(x,y),我如何创建n个随机点,这些点与(x,y)的距离是以σ为高斯分布,并以均值为参数?

您必须使用多变量正态分布。对于您的情况,必须在X轴和Y轴上使用正态分布。如果绘制分布图,它将是一条三维钟形曲线

使用numpy的多元正态分布

numpy.random.multivariate_normal(mean, cov[, size])

mean : 1-D array_like, of length N
Mean of the N-dimensional distribution.
cov : 2-D array_like, of shape (N, N)
Covariance matrix of the distribution. It must be symmetric and positive-semidefinite for proper sampling.
size : int or tuple of ints, optional
Given a shape of, for example, (m,n,k), m*n*k samples are generated, and packed in an m-by-n-by-k arrangement. Because each sample is N-dimensional, the output shape is (m,n,k,N). If no shape is specified, a single (N-D) sample is returned.
Returns:    
out : ndarray
The drawn samples, of shape size, if that was provided. If not, the shape is (N,).
In other words, each entry out[i,j,...,:] is an N-dimensional value drawn from the distribution.

您可以使用
numpy.random.normal
从新x和y坐标的高斯分布中提取随机数

from numpy.random import normal

sigma = 1.0
point_0 = (0.0, 0.0)

point_1 = [i + normal(0, sigma) for i in point]

这在这种情况下有效,因为将x和y维度上的高斯分布相乘将得到径向维度上的高斯分布。也就是说,exp(-r^2/a^2)=exp(-x^2/a^2)*exp(-y^2/a^2)

用于二维分布。诀窍是你需要得到每个维度的分布。例如,对于sigma为0.1的点(4,4)周围的随机分布:

sample_x = np.random.normal(4, 0.1, 500)
sample_y = np.random.normal(4, 0.1, 500)

fig, ax = plt.subplots()
ax.plot(sample_x, sample_y, '.')
fig.show()

您可以通过以下方式完成相同的任务:

mean = np.array([4,4])
sigma = np.array([0.1,0.1])
covariance = np.diag(sigma ** 2)
x, y = np.random.multivariate_normal(mean, covariance, 1000)

fig, ax = plt.subplots()
ax.plot(x, y, '.')
对于三维分布,您可以这样使用:


你想要类似的东西吗?@ManelFornos是的!
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from scipy.stats import multivariate_normal

x, y = np.mgrid[3:5:100j, 3:5:100j]
xy = np.column_stack([x.flat, y.flat])
mu = np.array([4.0, 4.0])
sigma = np.array([0.1, 0.1])
covariance = np.diag(sigma ** 2)
z = multivariate_normal.pdf(xy, mean=mu, cov=covariance)
z = z.reshape(x.shape)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z)
fig.show()