ML贝叶斯分类算法

1.贝叶斯定理(Bayes' theorem)

描述的是关于随机事件A和B的条件概率。一般情况下,事件A和在事件B发生的条件下的概率,与事件B在时间A发生的条件下的概率是不同的,但这两者之间是存在一定的关系的,贝叶斯定理描述的就是这种关系。记L(A|B)在B发生的条件下A发生的概率。

术语:
(1) p(A)事件A发生的概率

(2) p(A|B)在事件B发生的条件下,A发生的概率

(3) p(B|A)在事件A发生的条件下,B发生的概率

(4) p(B)事件B发生的概率

(5) p(AB)事件A和事件B同时发生的概率

Base formula:
P(A|B) = p(AB)/p(B)

Bayes' theorem:

p(B|A) = p(A|B)p(B)/p(A)


2.朴素贝叶斯分类(Naive Bayes)

待分类项:x = {a1, a2, ..., an}为一个待分类项,其中每个a为x的一个特征属性 

类别集合:C = {y1, y2, ..., ym}


Naive Bayes Classification计算过程:

阶段一:数据准备

(1) x,其特征属性为a1, a2, ..., an

(2) 样本集:(x1, x2, ..., xk)

阶段二:训练阶段

(1) 计算每个类别发生的概率p(yi)

(2) 计算各个类别下各个特征属性的条件概率p(aj/yi)

阶段三:预测阶段

(1) 计算样本属于每个类别的概率p(x|yi)p(yi)

(2) 取最大项作为x的类别max(p(x|yi)p(yi))


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
由于朴素贝叶斯算法模型涉及到数据预处理、特征选择、概率计算等复杂过程,因此整个模型的实现需要一定的编程基础和数据处理经验。下面给出一个简单的朴素贝叶斯算法模型的代码实现示例,供参考。 首先,我们需要导入需要用到的库: ```python import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.naive_bayes import GaussianNB from sklearn.metrics import classification_report, confusion_matrix ``` 接下来,我们需要读取数据并进行预处理。这里以鸢尾花数据集为例: ```python # 读取数据 df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None) # 将类别标签转化为数字 df[4] = pd.Categorical(df[4]).codes # 将数据分为特征和标签 X = df.iloc[:, :4].values y = df.iloc[:, 4].values # 将数据集分为训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) ``` 接下来,我们需要选择合适的特征,并进行概率计算。这里使用高斯朴素贝叶斯算法: ```python # 创建高斯朴素贝叶斯分类器对象 gnb = GaussianNB() # 训练模型 gnb.fit(X_train, y_train) # 预测测试集结果 y_pred = gnb.predict(X_test) # 输出混淆矩阵和分类报告 print(confusion_matrix(y_test, y_pred)) print(classification_report(y_test, y_pred)) ``` 最后,我们可以通过调用预测函数预测新数据的分类: ```python # 预测新数据 new_data = np.array([[5.1, 3.5, 1.4, 0.2]]) print(gnb.predict(new_data)) ``` 完整代码如下: ```python import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.naive_bayes import GaussianNB from sklearn.metrics import classification_report, confusion_matrix # 读取数据 df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None) # 将类别标签转化为数字 df[4] = pd.Categorical(df[4]).codes # 将数据分为特征和标签 X = df.iloc[:, :4].values y = df.iloc[:, 4].values # 将数据集分为训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) # 创建高斯朴素贝叶斯分类器对象 gnb = GaussianNB() # 训练模型 gnb.fit(X_train, y_train) # 预测测试集结果 y_pred = gnb.predict(X_test) # 输出混淆矩阵和分类报告 print(confusion_matrix(y_test, y_pred)) print(classification_report(y_test, y_pred)) # 预测新数据 new_data = np.array([[5.1, 3.5, 1.4, 0.2]]) print(gnb.predict(new_data)) ``` 输出结果如下: ``` [[16 0 0] [ 0 17 1] [ 0 0 11]] precision recall f1-score support 0 1.00 1.00 1.00 16 1 1.00 0.94 0.97 18 2 0.92 1.00 0.96 11 accuracy 0.98 45 macro avg 0.97 0.98 0.98 45 weighted avg 0.98 0.98 0.98 45 [0] ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值