TVP

# 直方图与核密度估计

DechinPhy

1400

## 制备样本

data = np.random.uniform(-3, 3, (10000, ))

data = np.random.normal(0, 1, (10000, ))

data = np.random.power(5, 10000)

，采10000个样本点。这种采样方法，随着

## 核密度估计函数

for t in range(0, M):
for index in range(0, len(grids)):
grids[index] += omega[t] * gaussian(x[index] - xt, sigma)

grids = gaussian(z[None] - x[:, None], sigma=sigma).sum(axis=0)

## 完整示例

import numpy as np
import matplotlib.pyplot as plt

def gaussian(x, mu=0, sigma=1):
“”“高斯波包函数“””
return np.exp(-(x-mu)**2/2/sigma**2)/np.sqrt(2*np.pi)/sigma

def kde(x, grid_min, grid_max, bins, sigma):
“”“带归一化的核密度估计函数”“”
grid_size = (grid_max - grid_min) / bins
z = grid_size*np.arange(bins) + grid_min + grid_size/2
res = gaussian(z[None]-x[:, None], sigma=sigma).sum(axis=0) / x.shape[-1]
res /= res.sum()*grid_size
return res, z

plt.figure(figsize=(10, 9))
plt.title('Kernel Density Estimation')
# 正态分布采样
data = np.random.normal(0, 1, (3, ))
# Numpy生成的直方图参数
hist, bin_edges = np.histogram(data, bins=20, normed=True)
subplot1 = plt.subplot2grid((4, 3), (0, 0))
subplot1.set_title("Matplotlib Hist")
subplot1.set_ylabel("Normal Distribution")
# Matplotlib自带的直方图
subplot1.hist(data, bins=20, rwidth=0.9, color='black', density=True)
subplot2 = plt.subplot2grid((4, 3), (0, 1))
subplot2.set_title("Numpy Histogram")
subplot2.bar(bin_edges[:-1], hist, width=0.15, color='green', align='center')
subplot3 = plt.subplot2grid((4, 3), (0, 2))
subplot3.set_title("KDE Function")
# 三种不同带宽的核密度估计函数
k, z = kde(data, -3, 3, 30, 0.2)
subplot3.plot(z, k, color='orange', label=r'$\sigma$=0.2')
k, z = kde(data, -3, 3, 30, 0.6)
subplot3.plot(z, k, color='purple', label=r'$\sigma$=0.6')
k, z = kde(data, -3, 3, 30, 1.0)
subplot3.plot(z, k, color='red', label=r'$\sigma$=1.0')
subplot3.legend()
# 有偏置的正态分布
data = np.random.normal(0, 1, (3, )) + 1
hist, bin_edges = np.histogram(data, bins=20, normed=True)
subplot1 = plt.subplot2grid((4, 3), (1, 0))
subplot1.set_ylabel("Bias Normal Distribution")
subplot1.hist(data, bins=20, rwidth=0.9, color='black', density=True)
subplot2 = plt.subplot2grid((4, 3), (1, 1))
subplot2.bar(bin_edges[:-1], hist, width=0.15, color='green', align='center')
subplot3 = plt.subplot2grid((4, 3), (1, 2))
k, z = kde(data, -3, 3, 30, 0.2)
subplot3.plot(z, k, color='orange', label=r'$\sigma$=0.2')
k, z = kde(data, -3, 3, 30, 0.6)
subplot3.plot(z, k, color='purple', label=r'$\sigma$=0.6')
k, z = kde(data, -3, 3, 30, 1.0)
subplot3.plot(z, k, color='red', label=r'$\sigma$=1.0')
subplot3.legend()
# 指数分布
data = np.random.power(5, 3)*7-4
hist, bin_edges = np.histogram(data, bins=20, normed=True)
subplot1 = plt.subplot2grid((4, 3), (2, 0))
subplot1.set_ylabel("Exponential Distribution")
subplot1.hist(data, bins=20, rwidth=0.9, color='black', density=True)
subplot2 = plt.subplot2grid((4, 3), (2, 1))
subplot2.bar(bin_edges[:-1], hist, width=0.15, color='green', align='center')
subplot3 = plt.subplot2grid((4, 3), (2, 2))
k, z = kde(data, -3, 3, 30, 0.2)
subplot3.plot(z, k, color='orange', label=r'$\sigma$=0.2')
k, z = kde(data, -3, 3, 30, 0.6)
subplot3.plot(z, k, color='purple', label=r'$\sigma$=0.6')
k, z = kde(data, -3, 3, 30, 1.0)
subplot3.plot(z, k, color='red', label=r'$\sigma$=1.0')
subplot3.legend()
# 均匀分布
data = np.random.uniform(-3, 3, (3, ))
hist, bin_edges = np.histogram(data, bins=20, normed=True)
subplot1 = plt.subplot2grid((4, 3), (3, 0))
subplot1.set_ylabel("Uniform Distribution")
subplot1.hist(data, bins=20, rwidth=0.9, color='black', density=True)
subplot2 = plt.subplot2grid((4, 3), (3, 1))
subplot2.bar(bin_edges[:-1], hist, width=0.15, color='green', align='center')
subplot3 = plt.subplot2grid((4, 3), (3, 2))
k, z = kde(data, -3, 3, 30, 0.2)
subplot3.plot(z, k, color='orange', label=r'$\sigma$=0.2')
k, z = kde(data, -3, 3, 30, 0.6)
subplot3.plot(z, k, color='purple', label=r'$\sigma$=0.6')
k, z = kde(data, -3, 3, 30, 1.0)
subplot3.plot(z, k, color='red', label=r'$\sigma$=1.0')
subplot3.legend()
# 画图
plt.show()

0 条评论

LV.

• 技术背景
• 制备样本
• 核密度估计函数
• 完整示例
• 总结概要