机器学习专栏(46):高斯混合模型与异常检测(附完整代码与工业级案例)

目录

一、高斯混合模型的核心原理

1.1 概率生成过程解析

1.2 EM算法迭代过程

二、Scikit-Learn实战:从基础到进阶

2.1 基础建模与可视化

2.2 协方差类型对比实验

三、工业级异常检测实战

3.1 信用卡欺诈检测

3.2 动态阈值调整

四、贝叶斯高斯混合模型进阶

4.1 自动确定聚类数量

4.2 先验知识注入

五、异常检测算法横向评测

5.1 算法特性矩阵

5.2 综合性能对比

六、最佳实践与调优策略

6.1 特征工程指南

​编辑

6.2 参数调优检查表

七、前沿进展:深度生成模型

7.1 变分自编码器(VAE)异常检测

7.2 生成对抗网络(GAN)异常检测


一、高斯混合模型的核心原理

1.1 概率生成过程解析

给定数据集X,GMM假设数据由k个高斯分布混合生成:

1.2 EM算法迭代过程

二、Scikit-Learn实战:从基础到进阶

2.1 基础建模与可视化

from sklearn.mixture import GaussianMixture
import matplotlib.pyplot as plt
import numpy as np

# 生成模拟数据
np.random.seed(42)
X1 = np.random.normal(0, 1, (300, 2))
X2 = np.random.normal(5, 1.5, (700, 2))
X = np.vstack([X1, X2])

# 训练GMM模型
gmm = GaussianMixture(n_components=2, covariance_type='full')
gmm.fit(X)

# 可视化决策边界
x_min, x_max = X[:,0].min()-1, X[:,0].max()+1
y_min, y_max = X[:,1].min()-1, X[:,1].max()+1
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 100),
                     np.linspace(y_min, y_max, 100))
Z = -gmm.score_samples(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

plt.contourf(xx, yy, Z, levels=20, cmap='viridis')
plt.scatter(X[:,0], X[:,1], s=5, c='white')
plt.title("GMM概率密度分布")
plt.colorbar(label='负对数概率密度')

2.2 协方差类型对比实验

cov_types = ['spherical', 'tied', 'diag', 'full']
plt.figure(figsize=(15,10))

for i, cov_type in enumerate(cov_types):
    gmm = GaussianMixture(n_components=2, covariance_type=cov_type)
    gmm.fit(X)
    
    # 获取椭圆参数
    if cov_type == 'spherical':
        widths = heights = 2 * np.sqrt(gmm.covariances_)
    else:
        from scipy.stats import multivariate_normal
        widths, heights = [], []
        for cov in gmm.covariances_:
            v, _ = np.linalg.eigh(cov)
            widths.append(2*np.sqrt(v[0]))
            heights.append(2*np.sqrt(v[1]))
    
    plt.subplot(2,2,i+1)
    plt.scatter(X[:,0], X[:,1], s=5, c=gmm.predict(X))
    for j in range(len(gmm.means_)):
        plt.gca().add_patch(plt.Ellipse(gmm.means_[j], widths[j], heights[j], 
                                      angle=0, alpha=0.3))
    plt.title(f'Covariance Type: {cov_type}')

三、工业级异常检测实战

3.1 信用卡欺诈检测

from sklearn.preprocessing import RobustScaler
from sklearn.metrics import classification_report

# 数据预处理
scaler = RobustScaler()
X_trai
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sonal_Lynn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值