相关知识参考:《统计学习方法》朴素贝叶斯法(Naive Bayes,NB)
通过最大概率来预测类:
y=arg maxckP(Y=ck)∏jP(X(j)=x(j)∣Y=ck)\color{red} y=\argmax\limits_{c_k} P(Y=c_k) \prod\limits_{j} P(X^{(j)}=x^{(j)}|Y=c_k)y=ckargmaxP(Y=ck)j∏P(X(j)=x(j)∣Y=ck)
模型假设:
模型变体:
朴素贝叶斯的假设很少为真,但是NB模型可以有效地判别线性可分类
%matplotlib inline
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=11)
# stratify=y, 对标签进行分层抽样,确保数据集之间的样本占比一致
lr = LogisticRegression()
nb = GaussianNB()
lr_scores = []
nb_scores = []
train_sizes = range(10, len(X_train), 10)
for train_size in train_sizes:
X_slice, _, y_slice, _ = train_test_split(
X_train, y_train, train_size=train_size, stratify=y_train, random_state=11)
nb.fit(X_slice, y_slice)
nb_scores.append(nb.score(X_test, y_test))
lr.fit(X_slice, y_slice)
lr_scores.append(lr.score(X_test, y_test))
plt.plot(train_sizes, nb_scores, label='Naive Bayes')
plt.plot(train_sizes, lr_scores, linestyle='--', label='Logistic Regression')
plt.rcParams['font.sans-serif'] = 'SimHei' # 消除乱码
plt.title("NB vs LogRg 对比")
plt.xlabel("训练样本数")
plt.ylabel("测试集预测准确率")
plt.legend()