高斯朴素贝叶斯GaussianNB
高斯朴素贝叶斯假设 P ( x i ∣ Y ) P(x_i|Y) P(xi∣Y)服从高斯正态分布,来估计每个特征下每个类别上的条件概率。
参数 | 含义 |
---|---|
priors:array-like of shape (n_classes,) | 可输入任何类数组结构,形状为(n_class,)表示类的先验概率。如果指定,则不根据数据调整先验,如果不指定则自行根据数据计算先验概率。 |
var_smoothing:float, default=1e-9 | 在估计方差时,为了追求估计得稳定性,将所有特征的方差中最大的方差以某个比例添加到估计得方差中 |
import numpy as np
import matplotlib.pyplot as plt
from sklearn.naive_bayes import GaussianNB
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
digits = load_digits()
x, y = digits.data, digits.target
xtrain, xtest, ytrain, ytest = train_test_split(x,y,test_size=0.3, random_state=420)
xtrain
array([[ 0., 0., 3., ..., 12., 0., 0.],
[ 0., 0., 4., ..., 15., 2., 0.],
[ 0., 0., 4., ..., 7., 0., 0.],
...,
[ 0., 0., 0., ..., 2., 0., 0.],
[ 0., 0., 7., ..., 13., 2., 0.],
[ 0., 0., 1., ..., 6., 0., 0.]])
xtrain.shape
(1257, 64)
xtest.shape
(540, 64)
ytrain
array([8, 3, 8, ..., 4, 9, 8])
np.unique(ytrain) #多分类问题,有10个类别
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
gnb = GaussianNB().fit(xtrain, ytrain) #实例化
#查看分数
acc_score = gnb.score(xtest, ytest)
acc_score
0.8592592592592593
#查看预测结果
y_pred = gnb.predict(xtest)
y_pred
array([6, 1, 3, 0, 4, 5, 0, 8, 3, 8, 6, 8, 7, 8, 8, 8, 5, 9, 5, 6, 5, 4,
7, 4, 8, 2, 7, 2, 8, 9, 2, 8, 3, 6, 0, 3, 8, 8, 1, 5, 2, 8, 8, 9,
2, 2, 0, 7, 3, 6, 7, 2, 8, 0, 5, 4, 1, 9, 4, 0, 5, 8, 9, 1, 7, 8,
7, 5, 8, 2, 4, 4, 8, 2, 6, 1, 2, 1, 7, 8, 8, 5, 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, 9, 8, 3, 6, 6, 8, 7, 5, 8, 4, 5, 8, 6, 7, 6, 7,
7, 8, 0, 8, 2, 2, 0, 5, 7, 3, 0, 2, 8, 2, 0, 2, 3, 6, 8, 1, 7, 5,
7, 1, 7, 7, 2, 7, 5, 2, 6, 5, 8, 0, 0, 8, 1, 3, 7, 6, 1, 5, 6, 2,
0, 1, 5, 7, 8, 0, 3, 5, 0, 7, 5, 4, 4, 1, 5, 9, 5, 3, 7, 1, 7, 3,
5, 8, 5, 8, 5, 6, 1, 6, 7, 4, 3, 7, 0, 5, 4, 9, 3, 3, 6, 3, 5, 2,
9, 8, 9, 3, 9, 7, 3, 4, 9, 4, 3, 1])
#查看越策的概率 结果
prob = gnb.predict_proba(xtest)
prob
array([[0.00000000e+000, 4.69391744e-052, 1.74871280e-098, ...,
0.00000000e+000, 4.19588993e-033, 1.51751459e-119],
[0.00000000e+000, 1.00000000e+000, 9.26742456e-013, ...,
0.00000000e+000, 0.00000000e+000, 0.00000000e+000],
[0.00000000e+000, 0.00000000e+000, 3.73608152e-026, ...,
0.00000000e+000, 1.29541754e-039, 5.54684869e-077],
...,
[0.00000000e+000, 2.43314963e-047, 4.82483668e-305, ...,
2.31612692e-008, 1.23891596e-126, 2.87896140e-257],
[0.00000000e+000, 8.26462929e-129, 4.99150558e-012, ...,
0.00000000e+000, 4.01802372e-003, 6.19000712e-013],
[0.00000000e+000, 9.99929965e-001, 1.45462767e-013, ...,
5.05856094e-005, 1.94498169e-005, 3.42317317e-042]])
prob.shape #每一列对应一个标签类别下的概率
(540, 10)
prob[1,:].sum() #每一行的和都是一
1.000000000000003
prob.sum(axis=1)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
prob.sum(axis=1).shape
(540,)
from sklearn.metrics import confusion_matrix as CM
CM(ytest, y_pred)
array([[47, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[ 0, 46, 2, 0, 0, 0, 0, 3, 6, 2],
[ 0, 2, 35, 0, 0, 0, 1, 0, 16, 0],
[ 0, 0, 1, 40, 0, 1, 0, 3, 4, 0],
[ 0, 0, 1, 0, 39, 0, 1, 4, 0, 0],
[ 0, 0, 0, 2, 0, 58, 1, 1, 1, 0],
[ 0, 0, 1, 0, 0, 1, 49, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 54, 0, 0],
[ 0, 3, 0, 1, 0, 0, 0, 2, 55, 0],
[ 1, 1, 0, 1, 2, 0, 0, 3, 7, 41]], dtype=int64)
import numpy as np
import matplotlib.pyplot as plt
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier as RFC
from sklearn.tree import DecisionTreeClassifier as DTC
from sklearn.linear_model import LogisticRegression as LR
from sklearn.datasets import load_digits
from sklearn.model_selection import learning_curve
from sklearn.model_selection import ShuffleSplit
from time import time
import datetime
def plot_learning_curve(estimator, title, x, y,
ax, #选择子图
ylim=None, #设置纵坐标的取值范围
cv=None, #交叉验证
n_jobs=None #设定要使用的线程
):
train_sizes, train_scores, test_scores = learning_curve(estimator, #分类器
x, y, #特征矩阵和标签
cv = cv, #交叉验证模式
n_jobs=n_jobs
)
ax.set_title(title)
if ylim is not None:
ax.set_ylim(*ylim)
ax.set_xlabel