用于二维分配使用numpy.random.normal.诀窍是你需要得到每个维度的分布.因此,例如对于西格玛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()
Run Code Online (Sandbox Code Playgroud)
你可以通过以下方式完成同样的事情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, '.')
Run Code Online (Sandbox Code Playgroud)
对于3D分布,您可以这样使用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()
Run Code Online (Sandbox Code Playgroud)