【机器学习】机器学习的基本分类-无监督学习-核密度估计(Kernel Density Estimation, KDE)

发布于:2024-12-18 ⋅ 阅读:(169) ⋅ 点赞:(0)

核密度估计(Kernel Density Estimation, KDE)

核密度估计(KDE)是一种非参数化方法,用于估计数据的概率密度函数(PDF)。与直方图相比,KDE 能够生成平滑的概率密度曲线,是统计数据分析中的重要工具。


1. 核密度估计的基本公式

假设我们有 n 个独立同分布的样本 x_1, x_2, \ldots, x_n,核密度估计的公式为:

\hat{f}(x) = \frac{1}{n h} \sum_{i=1}^{n} K\left(\frac{x - x_i}{h}\right)

其中:

  • \hat{f}(x):估计的概率密度函数值。
  • K(\cdot):核函数,用于计算样本点对位置 xxx 的贡献。
  • h > 0:带宽(平滑参数),控制核函数的宽度。
  • n:样本数量。

2. 核函数 K

核函数决定了每个样本对目标位置 x 的影响。常见的核函数有:

核函数 表达式 特点
高斯核(Gaussian) K(u) = \frac{1}{\sqrt{2\pi}} e^{-u^2/2} 光滑、常用
均匀核(Uniform)

K(u) = \begin{cases} \frac{1}{2}, & \text{if } |u| \leq 1, \\ 0, & \text{otherwise.} \end{cases}

简单

缺乏平滑性

支持范围有限

三角核(Triangular)

K(u) = \begin{cases} 1 - |u|, & \text{if } |u| \leq 1, \\ 0, & \text{otherwise.} \end{cases}

线性递减

支持范围有限

平滑性较好

Epanechnikov 核

K(u) = \begin{cases} \frac{3}{4}(1 - u^2), & \text{if } |u| \leq 1, \\ 0, & \text{otherwise.} \end{cases}

理论上最优

支持范围有限

计算简单

\mathbb{I}(\cdot) 是指示函数,值为 1 或 0,表示条件是否成立。


 3. 带宽 h

带宽是 KDE 的关键参数,决定估计的平滑程度:

  • h 小:曲线更接近实际数据,可能导致过拟合。
  • h 大:曲线更光滑,但可能导致欠拟合。

带宽的选择通常通过交叉验证或其他算法自动完成。


4. KDE 的直观理解

  • KDE 的核心思想是将每个数据点 x_i 转化为一个核函数分布(如高斯分布),然后将所有核函数叠加,得到概率密度函数。
  • 直方图是一个简单的密度估计方法,而 KDE 是其平滑版。

5. KDE 的优缺点

优点
  1. 平滑:避免了直方图中“块状”分布的问题。
  2. 非参数:无需假设数据分布形状。
  3. 灵活:适合一维、多维数据。
缺点
  1. 计算复杂度高,特别是高维数据时。
  2. 带宽选择对结果影响较大。
  3. 对数据稀疏的区域,密度估计可能不准确。

6. KDE 的 Python 实现

以下是 KDE 的简单实现,使用 scipyseaborn 库:

数据生成
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde

# 生成样本数据
np.random.seed(42)
data = np.concatenate([np.random.normal(0, 1, 500), np.random.normal(5, 1, 300)])

# 使用 scipy 进行 KDE
kde = gaussian_kde(data)
x = np.linspace(-3, 8, 1000)
density = kde(x)

# 绘制密度曲线
plt.figure(figsize=(8, 6))
plt.plot(x, density, label='KDE (Gaussian Kernel)', color='blue')
plt.hist(data, bins=30, density=True, alpha=0.4, label='Histogram', color='orange')
plt.title('Kernel Density Estimation')
plt.xlabel('Value')
plt.ylabel('Density')
plt.legend()
plt.show()

Seaborn 快速绘图 
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
import seaborn as sns

# 生成样本数据
np.random.seed(42)
data = np.concatenate([np.random.normal(0, 1, 500), np.random.normal(5, 1, 300)])

# 使用 scipy 进行 KDE
kde = gaussian_kde(data)
x = np.linspace(-3, 8, 1000)
density = kde(x)

# 绘制密度曲线
sns.kdeplot(data, fill=True, color='blue', alpha=0.6)
plt.title('KDE with Seaborn')
plt.xlabel('Value')
plt.ylabel('Density')
plt.show()


 7. 多维 KDE

对于多维数据,KDE 的公式为:

\hat{f}(x) = \frac{1}{n h^d} \sum_{i=1}^{n} K\left(\frac{x - x_i}{h}\right)

  • d 是数据的维度。
  • 核函数 K 可以扩展为多维(如多维高斯核)。

Python 示例(二维 KDE):

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde

# 生成样本数据
np.random.seed(42)
data = np.concatenate([np.random.normal(0, 1, 500), np.random.normal(5, 1, 300)])

# 使用 scipy 进行 KDE
kde = gaussian_kde(data)
# 生成二维数据
x, y = np.random.normal(0, 1, 500), np.random.normal(5, 1, 500)
xy = np.vstack([x, y])

# KDE 估计
kde = gaussian_kde(xy)
xx, yy = np.meshgrid(np.linspace(-3, 3, 100), np.linspace(3, 7, 100))
positions = np.vstack([xx.ravel(), yy.ravel()])
density = kde(positions).reshape(xx.shape)

# 绘图
plt.figure(figsize=(8, 6))
plt.contourf(xx, yy, density, levels=20, cmap='Blues')
plt.scatter(x, y, alpha=0.4, color='orange', s=10, label='Data Points')
plt.title('2D Kernel Density Estimation')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
plt.show()


 8. KDE 的实际应用场景

  1. 数据分布分析:探索数据的潜在分布。
  2. 异常检测:发现低密度区域的异常点。
  3. 概率估计:为概率模型提供基础。
  4. 模式识别:识别数据的高密度区域。
  5. 密度绘图:用于可视化数据分布。

9. KDE 的改进方向

  1. 快速算法:如基于网格的快速 KDE。
  2. 自动带宽选择:利用交叉验证等方法选择最优带宽。
  3. 结合其他方法:如在 GMM 中作为密度估计的辅助。

10. 总结

核密度估计(KDE)是统计分析和机器学习中的重要工具,其平滑、高灵活性的特点,使其成为直方图的强大替代方案。熟悉 KDE 的实现和参数选择,有助于更好地理解数据的分布特征并应用于实际问题。


网站公告

今日签到

点亮在社区的每一天
去签到