zoukankan      html  css  js  c++  java
  • 朴素贝叶斯分类器(伯努利贝叶斯+高斯贝叶斯+多项式贝叶斯)

    1 from sklearn.datasets import load_diabetes
    2 X,y=load_diabetes().data,load_diabetes().target
    3 X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=8)
    4 lr=LinearRegression().fit(X_train,y_train)
    5 print("the coefficient:{}".format(lr.coef_))
    6 print('the intercept:{}'.format(lr.intercept_))
    7 print("the score of this model:{:.3f}".format(lr.score(X_test,y_test)))
    1 import matplotlib.pyplot as plt
    2 plt.scatter(X[:,0],X[:,1],c=y,cmap=plt.cm.spring,edgecolors='k')
    3 plt.show()
    1 #伯努利贝叶斯分类器
    2 from sklearn.naive_bayes import BernoulliNB
    3 bnb=BernoulliNB()
    4 bnb.fit(X_train,y_train)
    5 print("the score of this model:{}".format(bnb.score(X_test,y_test)))
     1 import numpy as np
     2 x_min=X[:,0].min()-0.5
     3 x_max=X[:,0].max()+0.5
     4 y_min=X[:,1].min()-0.5
     5 y_max=X[:,1].max()+0.5
     6 xx,yy=np.meshgrid(np.arange(x_min,x_max,.02),
     7                   np.arange(y_min,y_max,.02))
     8 z=bnb.predict(np.c_[(xx.ravel(),yy.ravel())])
     9 z=z.reshape(xx.shape)
    10 plt.pcolormesh(xx,yy,z,cmap=plt.cm.Pastel1)
    11 plt.scatter(X_train[:,0],X_train[:,1],c=y_train,cmap=plt.cm.spring,edgecolors='k')
    12 plt.scatter(X_test[:,0],X_test[:,1],c=y_test,cmap=plt.cm.spring,edgecolors='k',marker='*')
    13 plt.xlim(xx.min(),xx.max())
    14 plt.ylim(yy.min(),yy.max())
    15 plt.title("Classifier:BernoulliNB")
    16 plt.show()
    1 #高斯贝叶斯分类器
    2 from sklearn.naive_bayes import GaussianNB
    3 gnb=GaussianNB()
    4 gnb.fit(X_train,y_train)
    5 print("the score of this model:{}".format(gnb.score(X_test,y_test)))
     1 import numpy as np
     2 x_min=X[:,0].min()-0.5
     3 x_max=X[:,0].max()+0.5
     4 y_min=X[:,1].min()-0.5
     5 y_max=X[:,1].max()+0.5
     6 xx,yy=np.meshgrid(np.arange(x_min,x_max,.02),
     7                   np.arange(y_min,y_max,.02))
     8 z=gnb.predict(np.c_[(xx.ravel(),yy.ravel())])
     9 z=z.reshape(xx.shape)
    10 plt.pcolormesh(xx,yy,z,cmap=plt.cm.Pastel1)
    11 plt.scatter(X_train[:,0],X_train[:,1],c=y_train,cmap=plt.cm.spring,edgecolors='k')
    12 plt.scatter(X_test[:,0],X_test[:,1],c=y_test,cmap=plt.cm.spring,edgecolors='k',marker='*')
    13 plt.xlim(xx.min(),xx.max())
    14 plt.ylim(yy.min(),yy.max())
    15 plt.title("Classifier:GaussianNB")
    16 plt.show()
     1 #最大最小预处理,处理非负数据
     2 from sklearn.preprocessing import MinMaxScaler
     3 scaler=MinMaxScaler()
     4 scaler.fit(X_train)
     5 X_train_scaled=scaler.transform(X_train)
     6 X_test_scaled=scaler.transform(X_test)
     7 #多项式朴素贝叶斯
     8 from sklearn.naive_bayes import MultinomialNB
     9 mnb=MultinomialNB()
    10 mnb.fit(X_train_scaled,y_train)
    11 print("the score of this model:{}".format(mnb.score(X_test_scaled,y_test)))
     1 import numpy as np
     2 x_min=X[:,0].min()-0.5
     3 x_max=X[:,0].max()+0.5
     4 y_min=X[:,1].min()-0.5
     5 y_max=X[:,1].max()+0.5
     6 xx,yy=np.meshgrid(np.arange(x_min,x_max,.02),
     7                   np.arange(y_min,y_max,.02))
     8 z=mnb.predict(np.c_[(xx.ravel(),yy.ravel())])
     9 z=z.reshape(xx.shape)
    10 plt.pcolormesh(xx,yy,z,cmap=plt.cm.Pastel1)
    11 plt.scatter(X_train[:,0],X_train[:,1],c=y_train,cmap=plt.cm.spring,edgecolors='k')
    12 plt.scatter(X_test[:,0],X_test[:,1],c=y_test,cmap=plt.cm.spring,edgecolors='k',marker='*')
    13 plt.xlim(xx.min(),xx.max())
    14 plt.ylim(yy.min(),yy.max())
    15 plt.title("Classifier:MultinomialNB")
    16 plt.show()

    以上三个分类器均是二维可视化的。

  • 相关阅读:
    WinCE下SQLCE数据库开发(VS,VB.net,VC++)
    基于VC++的WinCE网口通信
    WinCE下的串口通信开发(VS2005,VB.Net,VC++)
    多线程CSerialPort类的多串口通信实现
    双T滤波电路用于PWM方式DAC的分析
    AD9516锁相环功能外接环路滤波器的设计与分析
    块结构中断有序化处理方法(一种单片机单线程方式下处理多中断的方法)
    STM32F10X固件库函数——串口清状态位函数分析
    STM32和STR71X移植uCos-II操作系统比较分析
    基于uIP和uC/OS-II嵌入式网络开发
  • 原文地址:https://www.cnblogs.com/St-Lovaer/p/12245939.html
Copyright © 2011-2022 走看看