高斯朴素贝叶斯分类的原理解释和手写代码实现

Gaussian Naive Bayes (GNB) 是一种基于概率方法和高斯分布的机器学习的分类技术。朴素贝叶斯假设每个参数(也称为特征或预测变量)具有预测输出变量的独立能力。所有参数的预测组合是最终预测,它返回因变量被分类到每个组中的概率,最后的分类被分配给概率较高的分组(类)。

什么是高斯分布?

高斯分布也称为正态分布,是描述自然界中连续随机变量的统计分布的统计模型。正态分布由其钟形曲线定义, 正态分布中两个最重要的特征是均值 (μ) 和标准差 (σ)。平均值是分布的平均值,标准差是分布在平均值周围的“宽度”。

重要的是要知道正态分布的变量 (X) 从 -∞ < X < +∞ 连续分布(连续变量),并且模型曲线下的总面积为 1。

多分类的高斯朴素贝叶斯

导入必要的库:

  1. from random import random
  2. from random import randint
  3. import pandas as pd
  4. import numpy as np
  5. import seaborn as sns
  6. import matplotlib.pyplot as plt
  7. import statistics
  8. from sklearn.model_selection import train_test_split
  9. from sklearn.preprocessing import StandardScaler
  10. from sklearn.naive_bayes import GaussianNB
  11. from sklearn.metrics import confusion_matrix
  12. from mlxtend.plotting import plot_decision_regions

现在创建一个预测变量呈正态分布的数据集。

  1. #Creating values for FeNO with 3 classes:
  2. FeNO_0 = np.random.normal(20, 19, 200)
  3. FeNO_1 = np.random.normal(40, 20, 200)
  4. FeNO_2 = np.random.normal(60, 20, 200)
  5. #Creating values for FEV1 with 3 classes:
  6. FEV1_0 = np.random.normal(4.65, 1, 200)
  7. FEV1_1 = np.random.normal(3.75, 1.2, 200)
  8. FEV1_2 = np.random.normal(2.85, 1.2, 200)
  9. #Creating values for Broncho Dilation with 3 classes:
  10. BD_0 = np.random.normal(150,49, 200)
  11. BD_1 = np.random.normal(201,50, 200)
  12. BD_2 = np.random.normal(251, 50, 200)
  13. #Creating labels variable with three classes:(2)disease (1)possible disease (0)no disease:
  14. not_asthma = np.zeros((200,), dtype=int)
  15. poss_asthma = np.ones((200,), dtype=int)
  16. asthma = np.full((200,), 2, dtype=int)
  17. #Concatenate classes into one variable:
  18. FeNO = np.concatenate([FeNO_0, FeNO_1, FeNO_2])
  19. FEV1 = np.concatenate([FEV1_0, FEV1_1, FEV1_2])
  20. BD = np.concatenate([BD_0, BD_1, BD_2])
  21. dx = np.concatenate([not_asthma, poss_asthma, asthma])
  22. #Create DataFrame:
  23. df = pd.DataFrame()
  24. #Add variables to DataFrame:
  25. df['FeNO'] = FeNO.tolist()
  26. df['FEV1'] = FEV1.tolist()
  27. df['BD'] = BD.tolist()
  28. df['dx'] = dx.tolist()
  29. #Check database:
  30. df

我们的df有 600 行和 4 列。现在我们可以通过可视化检查变量的分布:

  1. fig, axs = plt.subplots(2, 3, figsize=(14, 7))
  2. sns.kdeplot(df['FEV1'], shade=True, color="b", ax=axs[0, 0])
  3. sns.kdeplot(df['FeNO'], shade=True, color="b", ax=axs[0, 1])
  4. sns.kdeplot(df['BD'], shade=True, color="b", ax=axs[0, 2])
  5. sns.distplot( a=df["FEV1"], hist=True, kde=True, rug=False, ax=axs[1, 0])
  6. sns.distplot( a=df["FeNO"], hist=True, kde=True, rug=False, ax=axs[1, 1])
  7. sns.distplot( a=df["BD"], hist=True, kde=True, rug=False, ax=axs[1, 2])
  8. plt.show()

通过人肉的检查,数据似乎接近高斯分布。还可以使用 qq-plots仔细检查:

  1. from statsmodels.graphics.gofplots import qqplot
  2. from matplotlib import pyplot
  3. #q-q plot:
  4. fig, axs = pyplot.subplots(1, 3, figsize=(15, 5))
  5. qqplot(df['FEV1'], line='s', ax=axs[0])
  6. qqplot(df['FeNO'], line='s', ax=axs[1])
  7. qqplot(df['BD'], line='s', ax=axs[2])
  8. pyplot.show()

虽然不是完美的正态分布,但已经很接近了。下面查看的数据集和变量之间的相关性:

完整文章

https://www.overfit.cn/post/0457f85f2c184ff0864db5256654aef1

posted @   deephub  阅读(418)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-04-07 使用PyTorch Lightning构建轻量化强化学习DQN
点击右上角即可分享
微信分享提示