2017-04-26 123 views
0

给定一个点(x,y)我将如何创建n个随机点,它们与(x,y)的距离是用sigma和mean表示的高斯分布作为参数?Python在点附近添加高斯噪声

+0

你想要[this](https://i.stack.imgur.com/eu7Jc.png)之类的东西吗? – manelfp

+0

@ManelFornos是的! –

回答

2

您必须使用多变量正态分布。对于你的情况,你必须在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. 
0

您可以使用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维度上乘以高斯分布将给出径向维度的高斯分布。 I.E. exp(-r^2/a^2) = exp(-x^2/a^2) * exp(-y^2/a^2)

6

对于2-D分配使用numpy.random.normal。诀窍是你需要获得每个维度的分布。因此,例如周边的点(4,4)的随机分布,西格马0.1:

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() 

enter image description here

可以完成同样的事情numpy.random.multivariate_normal如下:

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, '.') 

对于你可以使用scipy.stats.multivariate_normal这样的3-D分布:

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() 

enter image description here