吴裕雄 python 机器学习——伯努利贝叶斯BernoulliNB模型
import numpy as np
import matplotlib.pyplot as plt from sklearn import datasets,naive_bayes
from sklearn.model_selection import train_test_split # 加载 scikit-learn 自带的 digits 数据集
def load_data():
'''
加载用于分类问题的数据集。这里使用 scikit-learn 自带的 digits 数据集
'''
digits=datasets.load_digits()
return train_test_split(digits.data,digits.target,test_size=0.25,random_state=0,stratify=digits.target) #伯努利贝叶斯BernoulliNB模型
def test_BernoulliNB(*data):
X_train,X_test,y_train,y_test=data
cls=naive_bayes.BernoulliNB()
cls.fit(X_train,y_train)
print('Training Score: %.2f' % cls.score(X_train,y_train))
print('Testing Score: %.2f' % cls.score(X_test, y_test)) # 产生用于分类问题的数据集
X_train,X_test,y_train,y_test=load_data()
# 调用 test_BernoulliNB
test_BernoulliNB(X_train,X_test,y_train,y_test)

def test_BernoulliNB_alpha(*data):
'''
测试 BernoulliNB 的预测性能随 alpha 参数的影响
'''
X_train,X_test,y_train,y_test=data
alphas=np.logspace(-2,5,num=200)
train_scores=[]
test_scores=[]
for alpha in alphas:
cls=naive_bayes.BernoulliNB(alpha=alpha)
cls.fit(X_train,y_train)
train_scores.append(cls.score(X_train,y_train))
test_scores.append(cls.score(X_test, y_test)) ## 绘图
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(alphas,train_scores,label="Training Score")
ax.plot(alphas,test_scores,label="Testing Score")
ax.set_xlabel(r"$\alpha$")
ax.set_ylabel("score")
ax.set_ylim(0,1.0)
ax.set_title("BernoulliNB")
ax.set_xscale("log")
ax.legend(loc="best")
plt.show() # 调用 test_BernoulliNB_alpha
test_BernoulliNB_alpha(X_train,X_test,y_train,y_test)

def test_BernoulliNB_binarize(*data):
'''
测试 BernoulliNB 的预测性能随 binarize 参数的影响
'''
X_train,X_test,y_train,y_test=data
min_x=min(np.min(X_train.ravel()),np.min(X_test.ravel()))-0.1
max_x=max(np.max(X_train.ravel()),np.max(X_test.ravel()))+0.1
binarizes=np.linspace(min_x,max_x,endpoint=True,num=100)
train_scores=[]
test_scores=[]
for binarize in binarizes:
cls=naive_bayes.BernoulliNB(binarize=binarize)
cls.fit(X_train,y_train)
train_scores.append(cls.score(X_train,y_train))
test_scores.append(cls.score(X_test, y_test)) ## 绘图
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(binarizes,train_scores,label="Training Score")
ax.plot(binarizes,test_scores,label="Testing Score")
ax.set_xlabel("binarize")
ax.set_ylabel("score")
ax.set_ylim(0,1.0)
ax.set_xlim(min_x-1,max_x+1)
ax.set_title("BernoulliNB")
ax.legend(loc="best")
plt.show() # 调用 test_BernoulliNB_binarize
test_BernoulliNB_binarize(X_train,X_test,y_train,y_test)

吴裕雄 python 机器学习——伯努利贝叶斯BernoulliNB模型的更多相关文章
- 【sklearn朴素贝叶斯算法】高斯分布/多项式/伯努利贝叶斯算法以及代码实例
朴素贝叶斯 朴素贝叶斯方法是一组基于贝叶斯定理的监督学习算法,其"朴素"假设是:给定类别变量的每一对特征之间条件独立.贝叶斯定理描述了如下关系: 给定类别变量\(y\)以及属性值向 ...
- 概率图模型(PGM)学习笔记(四)-贝叶斯网络-伯努利贝叶斯-多项式贝叶斯
之前忘记强调了一个重要差别:条件概率链式法则和贝叶斯网络链式法则的差别 条件概率链式法则 贝叶斯网络链式法则,如图1 图1 乍一看非常easy认为贝叶斯网络链式法则不就是大家曾经学的链式法则么,事实上 ...
- 概率图形模型(PGM)学习笔记(四)-贝叶斯网络-伯努利贝叶斯-贝叶斯多项式
之前忘记强调重要的差异:链式法则的条件概率和贝叶斯网络的链式法则之间的差异 条件概率链式法则 P\left({D,I,G,S,L} \right) = P\left( D \right)P\left( ...
- 吴裕雄 python 机器学习——数据预处理过滤式特征选取SelectPercentile模型
from sklearn.feature_selection import SelectPercentile,f_classif #数据预处理过滤式特征选取SelectPercentile模型 def ...
- 吴裕雄 python 机器学习——数据预处理过滤式特征选取VarianceThreshold模型
from sklearn.feature_selection import VarianceThreshold #数据预处理过滤式特征选取VarianceThreshold模型 def test_Va ...
- 吴裕雄 python 机器学习——集成学习随机森林RandomForestRegressor回归模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklear ...
- 吴裕雄 python 机器学习——集成学习随机森林RandomForestClassifier分类模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklear ...
- Python机器学习笔记:朴素贝叶斯算法
朴素贝叶斯是经典的机器学习算法之一,也是为数不多的基于概率论的分类算法.对于大多数的分类算法,在所有的机器学习分类算法中,朴素贝叶斯和其他绝大多数的分类算法都不同.比如决策树,KNN,逻辑回归,支持向 ...
- 吴裕雄 python 机器学习——高斯贝叶斯分类器GaussianNB
import matplotlib.pyplot as plt from sklearn import datasets,naive_bayes from sklearn.model_selectio ...
随机推荐
- PL/SQL快键键——自动替换(输入sf直接跳出来select * from)
PL/SQL Developer使用技巧.快捷键 1.类SQL PLUS窗口:File->New->Command Window,这个类似于oracle的客户端工具sql plus,但比它 ...
- 22.01.Cluster
1. 클러스터링 iris 데이터셋 확인¶ In [2]: from sklearn import cluster from sklearn import datasets iris = dat ...
- 1.mysql卸载重新安装问题
前言:在开发过程中,MySQL数据库是很频繁使用的数据库,但是有时候,数据库一单出错,或者其他原因,想要重装数据库,难免会遇到MySQL重装之后服务启不来,,下面我就跟大家讨论下如何干净的卸载MySQ ...
- [JSOI2010] 连通数 - 强连通分量,缩点
复习一下手工 tarjan #include <bits/stdc++.h> using namespace std; vector <int> g[2005],scc[200 ...
- centos 部署 aspnetMVC 网页
在Linux上运行ASP.NET网站或WebApi的传统步骤是,先安装libgdiplus,再安装mono,然后安装Jexus.在这个过程中,虽然安装Jexus是挺简便的一件事,但是安装mono就相对 ...
- ASP.NET MVC模型绑定1
一.模型绑定原理 模型绑定是指为Controller的Action方法的参数提供值的过程,例如我有一个名为Blog的实体类(准确的说是ViewModel),它有一个名为Title的属性,如果我在VIE ...
- ScrollView示例(转载)
// 初始化var scrollView = new ccui.ScrollView(); // 设置方向scrollView.setDirection(ccui.ScrollView.DIR_VER ...
- 06 部署redis缓存数据库
1 安装redis $ sudo apt-get install redis-server 安装完成后,Redis服务器会自动启动,检查Redis服务器程序 注:在安装过程中,腾讯服务器会中途停止. ...
- 呼叫到达率100%,网易云信信令SDK免费上线!
近期,网易云信推出一款稳定可靠.到达率高.扩展性较强的信令通道产品--信令SDK.它能够提供可靠的消息通道,可用于搭建音视频场景下的呼叫邀请机制.信令SDK目前兼容市面上所有主流的音视频SDK,呼叫到 ...
- 题解 【洛谷P4995】跳跳!
一看题目名字,下意识地认为DP. 打开题目,发现是一道水的贪心,和DP没一分钱关系(毕竟是洛谷最水月赛的T2). 废话不多说. 看完题面,首先想到排序.要将乱序的石头高度变为有序,才能更好地想题. C ...