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和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Python虚拟环境与Conda使用指南分享

《Python虚拟环境与Conda使用指南分享》:本文主要介绍Python虚拟环境与Conda使用指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python 虚拟环境概述1.1 什么是虚拟环境1.2 为什么需要虚拟环境二、Python 内置的虚拟环境工具

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部