准备数据:20news groups

你可以在github上下到该数据集:20newsbydate.tar.gz

然后找到dataset loader

打开twenty_newsgroups.py

将里面的部分代码修改为:


运行:

categories = ['alt.atheism', 'soc.religion.christian','comp.graphics', 'sci.med']
from sklearn.datasets import fetch_20newsgroups
twenty_train = fetch_20newsgroups(subset='train',categories=categories, shuffle=True, random_state=42)

这时会产生一个cache,以后都会调用它。


了解20news group

官方文档:

The 20 Newsgroups data set is a collection of approximately 20,000 newsgroup documents, partitioned (nearly) evenly across 20 different newsgroups. To the best of our knowledge, it was originally collected by Ken Lang, probably for his paper “Newsweeder: Learning to filter netnews,” though he does not explicitly mention this collection. The 20 newsgroups collection has become a popular data set for experiments in text applications of machine learning techniques, such as text classification and text clustering.

参数:

返回值:

返回的twenty_train是一个‘bunch’

你可以用类似字典的key或者对象的attribute一样访问它:


前几行数据:


twenty_train.target存的是整数array,对应着每个document的folder,这也正好是每个document的label。

这就是前10个document的folder(label)。


向量化

ML中,我们肯定要把文档向量化。

对于文档的处理,一般有几个简单的步骤:
1.分词
我就拿nltk库来举例了。


这就是把句子拆成词。

2.词干提取

looked 和looking我们认为是一样的,因为表达的意义是相同的。

3.词性归并

同样是简化单词的处理。

4.词性标注

词性表:


5.去除停用词
停用词就是在句子中常用的但对意思影响不大的词。
英文中的停用词大概有:

# A list of common english words which should not affect predictions
stopwords = ['a', 'about', 'above', 'across', 'after', 'afterwards', 'again', 'against', 'all', 'almost', 'alone','along', 'already', 'also', 'although', 'always', 'am', 'among', 'amongst', 'amoungst', 'amount','an', 'and', 'another', 'any', 'anyhow', 'anyone', 'anything', 'anyway', 'anywhere', 'are', 'around','as', 'at', 'back', 'be', 'became', 'because', 'become', 'becomes', 'becoming', 'been', 'before','beforehand', 'behind', 'being', 'below', 'beside', 'besides', 'between', 'beyond', 'bill', 'both','bottom', 'but', 'by', 'call', 'can', 'cannot', 'cant', 'co', 'con', 'could', 'couldnt', 'cry', 'de','describe', 'detail', 'did', 'do', 'does', 'doing', 'don', 'done', 'down', 'due', 'during', 'each', 'eg','eight', 'either', 'eleven', 'else', 'elsewhere', 'empty', 'enough', 'etc', 'even', 'ever', 'every', 'everyone','everything', 'everywhere', 'except', 'few', 'fifteen', 'fify', 'fill', 'find', 'fire', 'first', 'five', 'for','former', 'formerly', 'forty', 'found', 'four', 'from', 'front', 'full', 'further', 'get', 'give', 'go', 'had','has', 'hasnt', 'have', 'having', 'he', 'hence', 'her', 'here', 'hereafter', 'hereby', 'herein', 'hereupon','hers', 'herself', 'him', 'himself', 'his', 'how', 'however', 'hundred', 'i', 'ie', 'if', 'in', 'inc', 'indeed','interest', 'into', 'is', 'it', 'its', 'itself', 'just', 'keep', 'last', 'latter', 'latterly', 'least', 'less','ltd', 'made', 'many', 'may', 'me', 'meanwhile', 'might', 'mill', 'mine', 'more', 'moreover', 'most', 'mostly','move', 'much', 'must', 'my', 'myself', 'name', 'namely', 'neither', 'never', 'nevertheless', 'next', 'nine','no', 'nobody', 'none', 'noone', 'nor', 'not', 'nothing', 'now', 'nowhere', 'of', 'off', 'often', 'on', 'once','one', 'only', 'onto', 'or', 'other', 'others', 'otherwise', 'our', 'ours', 'ourselves', 'out', 'over', 'own','part', 'per', 'perhaps', 'please', 'put', 'rather', 're', 's', 'same', 'see', 'seem', 'seemed', 'seeming','seems', 'serious', 'several', 'she', 'should', 'show', 'side', 'since', 'sincere', 'six', 'sixty', 'so', 'some', 'somehow', 'someone', 'something', 'sometime', 'sometimes', 'somewhere', 'still', 'such', 'system','t', 'take', 'ten', 'than', 'that', 'the', 'their', 'theirs', 'them', 'themselves', 'then', 'thence', 'there','thereafter', 'thereby', 'therefore', 'therein', 'thereupon', 'these', 'they', 'thickv', 'thin', 'third', 'this','those', 'though', 'three', 'through', 'throughout', 'thru', 'thus', 'to', 'together', 'too', 'top', 'toward','towards', 'twelve', 'twenty', 'two', 'un', 'under', 'until', 'up', 'upon', 'us', 'very', 'via', 'was', 'we','well', 'were', 'what', 'whatever', 'when', 'whence', 'whenever', 'where', 'whereafter', 'whereas', 'whereby','wherein', 'whereupon', 'wherever', 'whether', 'which', 'while', 'whither', 'who', 'whoever', 'whole', 'whom','whose', 'why', 'will', 'with', 'within', 'without', 'would', 'yet', 'you', 'your', 'yours', 'yourself','yourselves']

直接用nltk中下载的停用词也是可以的。

CountVectorizer 和TfidfVectorizer都能完成以上工作,不过他们也是有区别的。


CountVectorizer与TfidfVectorizer

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizercv = CountVectorizer(stop_words='english')
tv = TfidfVectorizer(stop_words='english')a = "cat hat bat splat cat bat hat mat cat"
b = "cat mat cat sat"




fit_transform后返回的都是2×62\times62×6的object。不过数据类型一个是int64,一个是float64。这当然是合理的,因为CountVectorizer是靠数的,TfidfVectorizer是计算分数的。

我们先把稀疏矩阵转为list:

def matrix_to_list(matrix):matrix = matrix.toarray()return matrix.tolist()


print("tfidf_a  tfidf_b  count_a count_b   word")
print("-"*41)
for i in range(6):print("  {:.3f}    {:.3f}        {:}       {:}   {:}".format(tv_score_list[0][i],tv_score_list[1][i],cv_score_list[0][i],cv_score_list[1][i],cv.get_feature_names()[i]))


这就很清楚了:比如,bat在a中出现2词,在b中出现0词。

那么,tf_idf是怎么算的呢?
tf_idf计算过程

tf就是term frequency,就是一个term在一句话中的频率。
但是这并不能说明一个term的重要与否,比如一句话中有好多个is,那is就重要了吗?
这时就要idf(inverse document frequency)了。它是所有的文本数量去除以包含指定term的文本数量,再取对数值。比如你有100个文本,99个文本都有is,100除以99,再取对数,最终的数肯定很小了,这样就合理地把is的重要性冲淡了。

tf_idf最后的值是tf的值再乘上idf的值。

我这里给一个小例子(用nltk库):



这时候已经把document转为特征向量了。


某个单词(比如algorithm)的index的值对应的就是训练文本中的频率(出现的次数)。


先fit再transform和fit、transform一起做是一样的:


Naive Bayes Classifier

我们有高斯贝叶斯分类器,多项式贝叶斯分类器,伯努力贝叶斯分类器,这里,我们用多项式的。


训练好后我们得到一个classifier。

现在我们呢来检测一番:

docs_new = ['God is love', 'OpenGL on the GPU is fast']
X_new_counts = count_vect.transform(docs_new)
X_new_tfidf = tfidf_transformer.transform(X_new_counts)predicted = clf.predict(X_new_tfidf)for doc, category in zip(docs_new, predicted):print('%r => %s' % (doc, twenty_train.target_names[category]))

得到:

'God is love' => soc.religion.christian
'OpenGL on the GPU is fast' => comp.graphics

当然pipeline可以一口气做完:

from sklearn.pipeline import Pipeline
text_clf = Pipeline([('vect', CountVectorizer()),('tfidf', TfidfTransformer()),('clf', MultinomialNB()),
])text_clf.fit(twenty_train.data,twenty_train.target)


在test上进行模型评估


import numpy as np
twenty_test = fetch_20newsgroups(subset='test',categories=categories, shuffle=True, random_state=42)
docs_test = twenty_test.data
predicted = text_clf.predict(docs_test)
np.mean(predicted == twenty_test.target)

最后的准确率是83%。


当然,我们也可以自己亲自处理数据,而不用sklearn提供的向量化。

数据:https://github.com/resuscitate/20news_group

代码:

import os
import string
import numpy as np
import matplotlib.pyplot as plt
from sklearn import model_selection
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import classification_report#X存的是文件名和文本内容(tuple)
#Y存的是X对应的类别
X = []
Y = []
for category in os.listdir('20_newsgroups'):for document in os.listdir('20_newsgroups/'+category):with open('20_newsgroups/'+category+'/'+document, "r",encoding='gbk',errors='ignore') as f:X.append((document,f.read()))Y.append(category)#划分训练集和测试集
X_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, Y, test_size=0.25, random_state=0)#停用词
stopwords = ['a', 'about', 'above', 'across', 'after', 'afterwards', 'again', 'against', 'all', 'almost', 'alone','along', 'already', 'also', 'although', 'always', 'am', 'among', 'amongst', 'amoungst', 'amount','an', 'and', 'another', 'any', 'anyhow', 'anyone', 'anything', 'anyway', 'anywhere', 'are', 'around','as', 'at', 'back', 'be', 'became', 'because', 'become', 'becomes', 'becoming', 'been', 'before','beforehand', 'behind', 'being', 'below', 'beside', 'besides', 'between', 'beyond', 'bill', 'both','bottom', 'but', 'by', 'call', 'can', 'cannot', 'cant', 'co', 'con', 'could', 'couldnt', 'cry', 'de','describe', 'detail', 'did', 'do', 'does', 'doing', 'don', 'done', 'down', 'due', 'during', 'each', 'eg','eight', 'either', 'eleven', 'else', 'elsewhere', 'empty', 'enough', 'etc', 'even', 'ever', 'every', 'everyone','everything', 'everywhere', 'except', 'few', 'fifteen', 'fify', 'fill', 'find', 'fire', 'first', 'five', 'for','former', 'formerly', 'forty', 'found', 'four', 'from', 'front', 'full', 'further', 'get', 'give', 'go', 'had','has', 'hasnt', 'have', 'having', 'he', 'hence', 'her', 'here', 'hereafter', 'hereby', 'herein', 'hereupon','hers', 'herself', 'him', 'himself', 'his', 'how', 'however', 'hundred', 'i', 'ie', 'if', 'in', 'inc', 'indeed','interest', 'into', 'is', 'it', 'its', 'itself', 'just', 'keep', 'last', 'latter', 'latterly', 'least', 'less','ltd', 'made', 'many', 'may', 'me', 'meanwhile', 'might', 'mill', 'mine', 'more', 'moreover', 'most', 'mostly','move', 'much', 'must', 'my', 'myself', 'name', 'namely', 'neither', 'never', 'nevertheless', 'next', 'nine','no', 'nobody', 'none', 'noone', 'nor', 'not', 'nothing', 'now', 'nowhere', 'of', 'off', 'often', 'on', 'once','one', 'only', 'onto', 'or', 'other', 'others', 'otherwise', 'our', 'ours', 'ourselves', 'out', 'over', 'own','part', 'per', 'perhaps', 'please', 'put', 'rather', 're', 's', 'same', 'see', 'seem', 'seemed', 'seeming','seems', 'serious', 'several', 'she', 'should', 'show', 'side', 'since', 'sincere', 'six', 'sixty', 'so', 'some', 'somehow', 'someone', 'something', 'sometime', 'sometimes', 'somewhere', 'still', 'such', 'system','t', 'take', 'ten', 'than', 'that', 'the', 'their', 'theirs', 'them', 'themselves', 'then', 'thence', 'there','thereafter', 'thereby', 'therefore', 'therein', 'thereupon', 'these', 'they', 'thickv', 'thin', 'third', 'this','those', 'though', 'three', 'through', 'throughout', 'thru', 'thus', 'to', 'together', 'too', 'top', 'toward','towards', 'twelve', 'twenty', 'two', 'un', 'under', 'until', 'up', 'upon', 'us', 'very', 'via', 'was', 'we','well', 'were', 'what', 'whatever', 'when', 'whence', 'whenever', 'where', 'whereafter', 'whereas', 'whereby','wherein', 'whereupon', 'wherever', 'whether', 'which', 'while', 'whither', 'who', 'whoever', 'whole', 'whom','whose', 'why', 'will', 'with', 'within', 'without', 'would', 'yet', 'you', 'your', 'yours', 'yourself','yourselves']#去除停用词后,构造一个单词与单词数量对应的字典
vocab = {}
for i in range(len(X_train)):word_list = []for word in X_train[i][1].split():word_new  = word.strip(string.punctuation).lower()if (len(word_new)>2)  and (word_new not in stopwords):  if word_new in vocab:vocab[word_new]+=1else:vocab[word_new]=1            #画出单词数与频率的图像,以此决定临界值(为了选feature)
num_words = [0 for i in range(max(vocab.values())+1)]
freq = [i for i in range(max(vocab.values())+1)]
for key in vocab:num_words[vocab[key]]+=1
plt.plot(freq,num_words)
plt.axis([1, 10, 0, 20000])
plt.xlabel("Frequency")
plt.ylabel("No of words")
plt.grid()
plt.show()#临界值
cutoff_freq = 80num_words_above_cutoff = len(vocab)-sum(num_words[0:cutoff_freq])
print("Number of words with frequency higher than cutoff frequency({}) :".format(cutoff_freq),num_words_above_cutoff)#选取大于临界值的feature
features = []
for key in vocab:if vocab[key] >=cutoff_freq:features.append(key)#训练集的feature矩阵
X_train_dataset = np.zeros((len(X_train),len(features)))for i in range(len(X_train)):word_list = [ word.strip(string.punctuation).lower() for word in X_train[i][1].split()]for word in word_list:if word in features:X_train_dataset[i][features.index(word)] += 1#测试集的feature矩阵
X_test_dataset = np.zeros((len(X_test),len(features)))for i in range(len(X_test)):word_list = [ word.strip(string.punctuation).lower() for word in X_test[i][1].split()]for word in word_list:if word in features:X_test_dataset[i][features.index(word)] += 1clf = MultinomialNB()
#训练数据
clf.fit(X_train_dataset,Y_train)
#对测试集做预测
Y_test_pred = clf.predict(X_test_dataset)sklearn_score_train = clf.score(X_train_dataset,Y_train)
print("Sklearn's score on training data :",sklearn_score_train)
sklearn_score_test = clf.score(X_test_dataset,Y_test)
print("Sklearn's score on testing data :",sklearn_score_test)
print("Classification report for testing data :-")print(classification_report(Y_test, Y_test_pred))
print(np.mean(Y_test==Y_test_pred))#86%的准确率

朴素贝叶斯案例之text classification相关推荐

  1. 六、朴素贝叶斯案例分析

    1.朴素贝叶斯案例分析 朴素贝叶斯案例分析的内容有: 项目概述:屏蔽社区留言板的侮辱性言论 项目实战:朴素贝叶斯案例的实现 数据集信息 朴素贝叶斯案例的数据包含6条样本,具体有3个正样本和3个负样本, ...

  2. 机器学习实战4.2 朴素贝叶斯案例:屏蔽社区留言板的侮辱性言论

    机器学习实战4.2 朴素贝叶斯案例:屏蔽社区留言板的侮辱性言论 参考地址:https://cuijiahua.com/blog/2017/11/ml_4_bayes_1.html 一.引子 很久没更新 ...

  3. python人工智能——机器学习——分类算法-朴素贝叶斯算法对新闻进行分类案例

    朴素贝叶斯案例流程 1.加载20类新闻数据,并进行分割 2.生成文章特征词 3.朴素贝叶斯estimator流程进行预估 代码 from sklearn.datasets import fetch_2 ...

  4. 朴素贝叶斯、精确率与召回率、交叉验证

    朴素贝叶斯.精确率与召回率.交叉验证 一.朴素贝叶斯 (1)朴素贝叶斯的原理 (2)朴素贝叶斯公式的使用 二.朴素贝叶斯API (1)朴素贝叶斯案例 (2)朴素贝叶斯总结 三.分类模型的评估 (1)混 ...

  5. ML算法基础——分类算法(朴素贝叶斯)

    文章目录 朴素贝叶斯算法 1.概率基础 2.朴素贝叶斯介绍 3.朴素贝叶斯算法案例 3.1 sklearn朴素贝叶斯实现API 3.2 sklearn-20类新闻分类 3.3 朴素贝叶斯案例流程 4. ...

  6. (视频+图文)机器学习入门系列-第4章 朴素贝叶斯

    机器学习入门系列,黄海广老师主讲.本站将持续更新,ppt.代码.课后习题见文末. 本系列的目录 01.引言 02.回归 03.逻辑回归 04.朴素贝叶斯 05.机器学习实践 06.机器学习库Sciki ...

  7. 【机器学习】从一个风控案例讲起-古老而经典的朴素贝叶斯

    今天给大家带来的文章,关于朴素贝叶斯的,一个古老而经典的算法,充分的理解有利于对风控特征或者识别的开拓新的思路. 一.从一个案例讲起 假如我们的目标是判断邮件是否是垃圾邮件,邮件内容是[代开增值税发票 ...

  8. python机器学习案例系列教程——文档分类器,朴素贝叶斯分类器,费舍尔分类器

    全栈工程师开发手册 (作者:栾鹏) python数据挖掘系列教程 github地址:https://github.com/626626cdllp/data-mining/tree/master/Bay ...

  9. ML之NB:朴素贝叶斯Naive Bayesian算法的简介、应用、经典案例之详细攻略

    ML之NB:朴素贝叶斯Naive Bayesian算法的简介.应用.经典案例之详细攻略 目录 朴素贝叶斯Naive Bayesian算法的简介 1.朴素贝叶斯计算流程表述 2.朴素贝叶斯的优缺点 2. ...

最新文章

  1. H2DBEngine——Driver的设计与实现
  2. vue 多页面iframe不刷新_Vue中iframe保持活动状态(不刷新)
  3. Appium_pytest fixture的使用
  4. Android中Http网络请求库框架Volley和Asnyc-http的使用---第三方库学习笔记(一)
  5. SpringCloud(二) 服务注册到Eureka
  6. eplan 2.7安装过程中multikey黄色感叹号解决办法
  7. 多目标跟踪的评价指标
  8. 网络安全等级保护云计算安全防护技术体系设计
  9. Android移动端性能测试工具mobileperf
  10. 【无人机】【2008.09】用于小型无人机目标定位的轨迹优化
  11. 全球及中国代餐轻食市场发展现状与消费需求前景调研报告2022版
  12. 高数第七版_习题解答_极限练习解答(第二类重要极限的多元形式)
  13. 叩丁狼培训实战教程之Java的动态代理
  14. mysql 赋权_《MySQL数据库》MySQL用户赋权
  15. 31 Three.js的特殊光源镜头光晕(lens flare)
  16. Java 去除字符串中的空白字符
  17. Qt for iOS,Qt 与Objective C混合编程
  18. 成为专业程序员路上用到的各种优秀资料、神器及框架
  19. java 处理物料清单_ERP之物料清单(BOM)
  20. ArcGIS中的土地利用变化分析

热门文章

  1. JavaScript(the second day)
  2. 笨小孩--投资中的确定性
  3. ckeditor KindEditor eWebEditor WQeditor FreeTextbox Tinymce 几款在线编辑器的比较(附各版本demo下载地址)
  4. 蓝汛之TWS之间的通信:【篇】
  5. java调用扫描仪1
  6. 首创!用户级权限进程防杀 C++
  7. keyshot分辨率多少合适_keyshot电脑配置浅谈
  8. 计算机基础------操作系统
  9. 小程序滚动事件相关总结
  10. 【第十六篇】商城系统-认证系统构建