问题描述
给出一个点(x,y),我将如何创建n个随机点,使它们与(x,y)的距离为高斯分布,且其sigma为均值?
Given a point (x,y) how would i create n random points that their distance from (x,y) is gaussian distributed with sigma and mean as a param?
推荐答案
对于2D发行版,请使用 numpy.random.normal
.诀窍是您需要获取每个维度的分布.因此,例如,对于点(4,4)周围的σ为0.1的随机分布:
For the 2-D distribution use numpy.random.normal
. The trick is that you need to get the distribution for each dimension. So for example for a random distribution around point (4,4) with 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
如下:
You can accomplish the same thing with numpy.random.multivariate_normal
as follows:
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
就像这样:
For the 3-D distribution you can use scipy.stats.multivariate_normal
like so:
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()
这篇关于Python在点周围的半径中添加高斯噪声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!