sklearn上机笔记3:朴素贝叶斯算法实践

本文记录了使用sklearn库中的朴素贝叶斯算法进行威斯康星乳腺肿瘤分类的实践过程,探讨了算法在医学数据集上的应用及其效果。
摘要由CSDN通过智能技术生成

 

 

 

import pandas as pd
import numpy as np
#导入数据集生成工具
from sklearn.datasets import make_blobs

 

#随机生成500个类别数是5的分类数据
X,y = make_blobs(n_samples = 500,centers = 5,random_state = 5)
#数据可视化
import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(X[:,0],X[:,1],c = y,cmap = plt.cm.spring,edgecolors = 'k')

#拆分训练集与测试集
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state = 8)

 

#导入朴素贝叶斯分类器
from sklearn.naive_bayes import BernoulliNB,MultinomialNB,GaussianNB

#构建伯努利分布(二项分布)朴素贝叶斯分类器
nb = BernoulliNB()
nb.fit(X_train,y_train)
print('模型得分:{:.3f}'.format(nb.score(X_test,y_test)))

#拟合结果可视化
plt.figure(dpi = 300)
x_min,x_max = X[:,0].min()-0.5,X[:,0].max()+0.5
y_min,y_max = X[:,1].min()-0.5,X[:,1].max()+0.5
xx,yy = np.meshgrid(np.arange(x_min,x_max,.02),
                    np.arange(y_min,y_max,.02))
z = nb.predict(np.c_[(xx.ravel(),yy.ravel())]).reshape(xx.shape)
plt.pcolormesh(xx,yy,z,cmap = plt.cm.Pastel1)
plt.scatter(X_train[:,0],X_train[:,1],c = y_train,cmap = plt.cm.cool,edgecolor = 'k')
plt.scatter(X_test[:,0],X_test[:,1],c = y_test,cmap = plt.cm.cool,marker = '*',
           edgecolor = 'k')
plt.xlim(xx.min(),xx.max())
plt.ylim(yy.min(),yy.max())
plt.title('Classifier:BernoulliNB')
plt.show

 

#构建多项分布朴素贝叶斯分类器
mnb = MultinomialNB()
mnb.fit(X_train,y_train)
print('模型得分:{:.3f}'.format(mnb.score(X_test,y_test)))

X

 

array([[-1.03627407e+00,  2.08982548e+00],
       [-2.93381863e+00, -5.99131201e+00],
       [ 1.53703669e-01,  1.80568727e+00],
       [-5.91896553e+00,  8.01811773e+00],
       [-5.98814699e+00,  7.56447506e+00],
       [-5.43845287e+00,  1.09607877e+01],
       [-6.07039263e+00,  7.93190548e+00],
       [-6.72483849e+00,  6.01757210e+00],
       [ 5.67661970e+00,  1.68576076e+00],
       [-5.45939839e+00,  7.77008460e+00],
       [-1.09394449e-01,  3.92488330e+00],
       [-4.22640456e+00,  7.13207352e+00],
       [-9.14953234e+00,  7.59352491e+00],
       [-4.71382623e-01,  2.96332032e+00],
       [ 3.48826186e-01,  3.27154564e+00],
       [-4.11625547e+00,  7.85760238e+00],
       [-7.71636615e-01,  1.31043975e+00],
       [-5.36247649e+00,  8.74949470e+00],
       [ 5.12176441e-02,  2.08644338e+00],
       [-9.31289497e-01,  3.17416541e+00],
       [-8.64020025e+00,  9.45084629e+00],
       [-7.37110801e-02,  1.03163659e+00],
       [-3.42815006e+00, -6.08016684e+00],
       [-3.29107834e+00, -7.02117162e+00],
       [ 2.27469586e-01,  2.26757693e+00],
       [-6.09377785e-01,  6.77576187e-01],
       [-5.92854611e+00,  5.16552800e+00],
       [ 1.65239303e+00,  4.92244975e-01],
       [ 3.86093790e+00, -6.13310338e-02],
       [ 5.49555049e+00, -4.65162941e-01],
       [-4.42079360e+00,  7.90908652e+00],
       [-4.11128057e+00,  8.58678214e+00],
       [-4.14882577e+00, -7.23254810e+00],
       [ 4.53784583e+00,  4.03497041e-01],
       [-3.52122094e+00, -5.84466313e+00],
       [-6.54627096e+00,  7.74771477e+00],
       [ 6.15562083e+00,  7.43865304e-01],
       [-4.14673856e+00,  7.63590025e+00],
       [-7.15983508e+00,  8.29179894e+00],
       [ 4.78292494e+00,  1.62539127e+00],
       [-3.85750611e+00, -6.24217718e+00],
       [-6.58069814e+00,  8.32097963e+00],
       [ 4.98183594e-01,  1.42099539e+00],
       [ 6.67958015e+00,  3.64092415e-01],
       [-6.01081708e+00,  9.33601946e+00],
       [ 1.13226528e+00,  1.15224609e+00],
       [ 4.42094483e+00,  8.45447097e-02],
       [-4.78353050e+00, -6.57019545e+00],
       [-5.34959412e+00,  7.76114787e+00],
       [ 5.93088467e+00,  2.60033155e+00],
       [ 4.72301753e+00, -2.03095558e+00],
       [-3.47085190e+00, -6.74846176e+00],
       [-2.90477499e+00, -6.07471689e+00],
       [-2.74432946e+00, -5.90493130e+00],
       [ 3.53344174e+00,  1.73776474e+00],
       [ 6.13909995e+00,  1.57580589e-01],
       [-6.54074446e+00,  6.55779297e+00],
       [-7.34128788e+00,  6.66674034e+00],
       [-5.10247851e+00,  9.65261174e+00],
       [-4.33239321e+00, -5.77802757e+00],
       [-3.01907353e-01,  7.29354105e-01],
       [-3.67716795e+00, -4.66248249e+00],
       [-6.58610935e+00, -5.37570900e+00],
       [-3.67278957e+00, -5.86688286e+00],
       [ 6.74418062e+00,  1.38146677e+00],
       [-6.03143291e+00,  1.10175716e+01],
       [-1.16476066e+00,  1.89415337e+00],
       [ 4.82647422e+00,  1.37239594e+00],
       [ 1.11073566e+00,  1.23554652e+00],
       [ 5.50675281e&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值