Python 高斯分布可视化

2020-04-26  本文已影响0人  升不上三段的大鱼

要用到的库

import numpy as np
import matplotlib.pyplot as plt
# This import registers the 3D projection, but is otherwise unused.
from mpl_toolkits.mplot3d import Axes3D

1. 单变量高斯分布

单变量的高斯分布:
p(x;μ,\sigma)= \frac{1}{\sqrt{(2π)\sigma}}exp(−\frac{(x−μ)^2}{2\sigma^2})
随机生成的符合高斯分布的样本点与概率密度曲线

def gaussian(mu, sigma, X):
    return np.exp(-(X-mu)* (X-mu)/(2*sigma*sigma)) / (sigma*np.sqrt(2*np.pi))

# univariant gaussian distribution
mu = 1
sigma = 0.2
n_samples = 1000
bins = 30
Y_sampled = np.random.normal(mu, sigma, n_samples)
x = np.linspace(0, 2)
Y_truth = 50 * gaussian(mu, sigma, x)

plt.figure(figsize=(8,4))

plt.subplot(1,2,1)
plt.hist(Y_sampled, bins=bins, label='mu:{:0.2f}, sigma:{:0.2f}, bins:{:2d}'.format(mu, sigma, bins))
plt.legend()
plt.subplot(1,2,2)
plt.plot(x, Y_truth, 'r', label='mu:{:0.2f}, sigma:{:0.2f}'.format(mu, sigma))
plt.legend()
plt.suptitle('Figure 1:Sampled and Ground Truth Distribution in 1D')
Figure_1 Sampled and Ground Truth Distribution in 1D.png

2. 多变量高斯分布

多变量高斯分布的公式
p(x;μ,Σ)= \frac{1}{\sqrt{(2π)n|Σ|}}exp(−\frac{1}{2}(x−μ)^TΣ−1(x−μ))
其中:
x=(x_{1}, x_{2}, ..., x_{n})
Σ: n*n的协方差矩阵

def multivariate_gaussian(mu, sigma, X):
    d = mu.shape[0]
    sigma_det = np.linalg.det(sigma)
    sigma_inv = np.linalg.inv(sigma)
    D = np.sqrt((2 * np.pi)**d * sigma_det)

    # This einsum call calculates (x-mu)T.sigma-1.(x-mu) in a vectorized
    # way across all the input variables.
    fac = np.einsum('...k,kl,...l->...', X-mu, sigma_inv, X-mu)

    return np.exp(-fac /2) / D


# Multivariate gaussian distribution
# Mean vector and covariance matrix
mu_v = np.array([0.5, -0.2])
sigma_v = np.array([[2,0.3], [0.3,0.5]])
bins = 30

# Random 2D gaussian distributed samples
Y_sampled=np.random.multivariate_normal(mu_v,sigma_v, 10000)

fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(121, projection='3d')
z,x,y= np.histogram2d(Y_sampled[:,0], Y_sampled[:,1], bins= 30, range=[[-6,6], [-6,6]])

# Construct arrays for the anchor position
x_pos, y_pos = np.meshgrid(x[:-1] + 0.25, y[:-1] + 0.25, indexing="ij")
x_pos = x_pos.ravel()
y_pos = y_pos.ravel()
z_pos = 0

# Construct arrays with the dimensions
dx = dy = 0.5 * np.ones_like(z_pos)
dz = z.ravel()
ax.bar3d(x_pos, y_pos,z_pos,dx,dy,dz, zsort='average')


x = y= np.linspace(-6, 6, 30, endpoint=False)
X, Y = np.meshgrid(x, y)

# Pack X and Y into a single 3-dimensional array
pos = np.empty(X.shape + (2,))
pos[:, :, 0] = X
pos[:, :, 1] = Y

Z = multivariate_gaussian(mu_v, sigma_v, pos)


ax1 = fig.add_subplot(122, projection='3d')
ax1.plot_surface(X, Y, Z)

plt.suptitle('Figure 2:Sampled and Ground Truth Distribution in 2D')

plt.show()
Figure_2 Experimental and Ground Truth Distribution in 2D.png
上一篇 下一篇

猜你喜欢

热点阅读