python学习 文本特征提取(三) CountVectorizer TfidfVectorizer 朴素贝叶斯分类性能测试

本文主要是介绍python学习 文本特征提取(三) CountVectorizer TfidfVectorizer 朴素贝叶斯分类性能测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • python学习 文本特征提取(一) DictVectorizer shuihupo
    博客地址,https://blog.csdn.net/shuihupo/article/details/80923414

  • python学习 文本特征提取(二) CountVectorizer TfidfVectorizer 中文处理
    https://blog.csdn.net/shuihupo/article/details/80930801

  • python学习文本特征提取(三) CountVectorizer TfidfVectorizer 朴素贝叶斯分类性能测试
    https://blog.csdn.net/shuihupo/article/details/80931194

CountVectorizer TfidfVectorizer 朴素贝叶斯分类性能测试

学习过了python学习 文本特征提取(二) CountVectorizer TfidfVectorizer 中文处理,如何实战呢。让我们奔腾学习:python学习 文本特征提取(三) CountVectorizer TfidfVectorizer 朴素贝叶斯分类性能测试 。
暂时没有现成的数据,就直接把书上的例子作参考吧,只要大家明确数据的输入格式,其他都不是问题。
这个数据的格式是:
X_train, X_test, y_train, y_test = train_test_split(news.data, news.target, test_size=0.25, random_state=33)
可认为是
X_train, X_test, y_train, y_test = train_test_split(x_文本, y_对应标签, test_size=0.25,)

只使用词频统计的方式将原始训练和测试文本转化为特征向量,朴素贝叶斯分类
# 从sklearn.datasets里导入20类新闻文本数据抓取器。
from sklearn.datasets import fetch_20newsgroups
# 从互联网上即时下载新闻样本,subset='all'参数代表下载全部近2万条文本存储在变量news中。
news = fetch_20newsgroups(subset='all')
print(type(news))
print(news)
# 从sklearn.cross_validation导入train_test_split模块用于分割数据集。
from sklearn.cross_validation import train_test_split
# 对news中的数据data进行分割,25%的文本用作测试集;75%作为训练集。
X_train, X_test, y_train, y_test = train_test_split(news.data, news.target, test_size=0.25, random_state=33)# 从sklearn.feature_extraction.text里导入CountVectorizer
from sklearn.feature_extraction.text import CountVectorizer
# 采用默认的配置对CountVectorizer进行初始化(默认配置不去除英文停用词),并且赋值给变量count_vec。
count_vec = CountVectorizer()# 只使用词频统计的方式将原始训练和测试文本转化为特征向量。
X_count_train = count_vec.fit_transform(X_train)
X_count_test = count_vec.transform(X_test)# 从sklearn.naive_bayes里导入朴素贝叶斯分类器。
from sklearn.naive_bayes import MultinomialNB
# 使用默认的配置对分类器进行初始化。
mnb_count = MultinomialNB()
# 使用朴素贝叶斯分类器,对CountVectorizer(不去除停用词)后的训练样本进行参数学习。
mnb_count.fit(X_count_train, y_train)# 输出模型准确性结果。
print 'The accuracy of classifying 20newsgroups using Naive Bayes (CountVectorizer without filtering stopwords):', mnb_count.score(X_count_test, y_test)
# 将分类预测的结果存储在变量y_count_predict中。
y_count_predict = mnb_count.predict(X_count_test)
# 从sklearn.metrics 导入 classification_report。
from sklearn.metrics import classification_report
# 输出更加详细的其他评价分类性能的指标。
print classification_report(y_test, y_count_predict, target_names = news.target_names)

CountVectorizer TfidfVectorizer 朴素贝叶斯分类性能测试

# 继续沿用如上代码的工具包(在同一份源代码中,或者不关闭解释器环境),分别使用停用词过滤配置初始化CountVectorizer与TfidfVectorizer。
count_filter_vec, tfidf_filter_vec = CountVectorizer(analyzer='word', stop_words='english'), TfidfVectorizer(analyzer='word', stop_words='english')# 使用带有停用词过滤的CountVectorizer对训练和测试文本分别进行量化处理。
X_count_filter_train = count_filter_vec.fit_transform(X_train)
X_count_filter_test = count_filter_vec.transform(X_test)# 使用带有停用词过滤的TfidfVectorizer对训练和测试文本分别进行量化处理。
X_tfidf_filter_train = tfidf_filter_vec.fit_transform(X_train)
X_tfidf_filter_test = tfidf_filter_vec.transform(X_test)# 初始化默认配置的朴素贝叶斯分类器,并对CountVectorizer后的数据进行预测与准确性评估。
mnb_count_filter = MultinomialNB()
mnb_count_filter.fit(X_count_filter_train, y_train)
print 'The accuracy of classifying 20newsgroups using Naive Bayes (CountVectorizer by filtering stopwords):', mnb_count_filter.score(X_count_filter_test, y_test)
y_count_filter_predict = mnb_count_filter.predict(X_count_filter_test)# 初始化另一个默认配置的朴素贝叶斯分类器,并对TfidfVectorizer后的数据进行预测与准确性评估。
mnb_tfidf_filter = MultinomialNB()
mnb_tfidf_filter.fit(X_tfidf_filter_train, y_train)
print 'The accuracy of classifying 20newsgroups with Naive Bayes (TfidfVectorizer by filtering stopwords):', mnb_tfidf_filter.score(X_tfidf_filter_test, y_test)
y_tfidf_filter_predict = mnb_tfidf_filter.predict(X_tfidf_filter_test)# 对上述两个模型进行更加详细的性能评估。
from sklearn.metrics import classification_report
print classification_report(y_test, y_count_filter_predict, target_names = news.target_names)
print classification_report(y_test, y_tfidf_filter_predict, target_names = news.target_names)
The accuracy of classifying 20newsgroups using Naive Bayes (CountVectorizer by filtering stopwords): 0.863752122241
The accuracy of classifying 20newsgroups with Naive Bayes (TfidfVectorizer by filtering stopwords): 0.882640067912precision    recall  f1-score   supportalt.atheism       0.85      0.89      0.87       201comp.graphics       0.62      0.88      0.73       250comp.os.ms-windows.misc       0.93      0.22      0.36       248
comp.sys.ibm.pc.hardware       0.62      0.88      0.73       240comp.sys.mac.hardware       0.93      0.85      0.89       242comp.windows.x       0.82      0.85      0.84       263misc.forsale       0.90      0.79      0.84       257rec.autos       0.91      0.91      0.91       238rec.motorcycles       0.98      0.94      0.96       276rec.sport.baseball       0.98      0.92      0.95       251rec.sport.hockey       0.92      0.99      0.95       233sci.crypt       0.91      0.97      0.93       238sci.electronics       0.87      0.89      0.88       249sci.med       0.94      0.95      0.95       245sci.space       0.91      0.96      0.93       221soc.religion.christian       0.87      0.94      0.90       232talk.politics.guns       0.89      0.96      0.93       251talk.politics.mideast       0.95      0.98      0.97       231talk.politics.misc       0.84      0.90      0.87       188talk.religion.misc       0.91      0.53      0.67       158avg / total       0.88      0.86      0.85      4712precision    recall  f1-score   supportalt.atheism       0.86      0.81      0.83       201comp.graphics       0.85      0.81      0.83       250comp.os.ms-windows.misc       0.84      0.87      0.86       248
comp.sys.ibm.pc.hardware       0.78      0.88      0.83       240comp.sys.mac.hardware       0.92      0.90      0.91       242comp.windows.x       0.95      0.88      0.91       263misc.forsale       0.90      0.80      0.85       257rec.autos       0.89      0.92      0.90       238rec.motorcycles       0.98      0.94      0.96       276rec.sport.baseball       0.97      0.93      0.95       251rec.sport.hockey       0.88      0.99      0.93       233sci.crypt       0.85      0.98      0.91       238sci.electronics       0.93      0.86      0.89       249sci.med       0.96      0.93      0.95       245sci.space       0.90      0.97      0.93       221soc.religion.christian       0.70      0.96      0.81       232talk.politics.guns       0.84      0.98      0.90       251talk.politics.mideast       0.92      0.99      0.95       231talk.politics.misc       0.97      0.74      0.84       188talk.religion.misc       0.96      0.29      0.45       158avg / total       0.89      0.88      0.88      4712

参考
网络资源及书本《python 机器学习实战——从零开始通往Kaggle竞赛之路》第三章
代码名称:Chapter_3.1.1.1.ipynb
整书百度网盘地址:https://pan.baidu.com/s/1hpVqUTngF1r7qQlGUJ720g

这篇关于python学习 文本特征提取(三) CountVectorizer TfidfVectorizer 朴素贝叶斯分类性能测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/478989

相关文章

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

Python装饰器之类装饰器详解

《Python装饰器之类装饰器详解》本文将详细介绍Python中类装饰器的概念、使用方法以及应用场景,并通过一个综合详细的例子展示如何使用类装饰器,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录1. 引言2. 装饰器的基本概念2.1. 函数装饰器复习2.2 类装饰器的定义和使用3. 类装饰

Python 交互式可视化的利器Bokeh的使用

《Python交互式可视化的利器Bokeh的使用》Bokeh是一个专注于Web端交互式数据可视化的Python库,本文主要介绍了Python交互式可视化的利器Bokeh的使用,具有一定的参考价值,感... 目录1. Bokeh 简介1.1 为什么选择 Bokeh1.2 安装与环境配置2. Bokeh 基础2

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模