2017-04-26 128 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