【问题标题】:Python add gaussian noise in a radius around a point [closed]Python在一个点周围的半径中添加高斯噪声[关闭]
【发布时间】:2017-04-26 13:51:34
【问题描述】:

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

【问题讨论】:

  • 你想要this这样的东西吗?
  • @ManelFornos 是的!

标签: python numpy scipy gaussian


【解决方案1】:

对于二维分布使用numpy.random.normal。诀窍是您需要获取每个维度的分布。例如,对于点 (4,4) 周围的随机分布,sigma 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()

您可以使用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, '.')

对于 3-D 分布,您可以像这样使用scipy.stats.multivariate_normal

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

【讨论】:

    【解决方案2】:

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

    使用 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.
    

    【讨论】:

      【解决方案3】:

      您可以使用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 维度上乘以高斯分布将在径向维度上得到高斯分布。 IE。 exp(-r^2/a^2) = exp(-x^2/a^2) * exp(-y^2/a^2)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-12
        • 2013-10-14
        • 1970-01-01
        • 1970-01-01
        • 2016-02-07
        • 1970-01-01
        • 2016-01-24
        • 2015-02-04
        相关资源
        最近更新 更多