【阿旭机器学习实战】【27】贝叶斯模型:新闻分类实战----CounterVecorizer与TfidVectorizer构建特征向量对比
【阿旭机器学习实战】系列文章主要介绍机器学习的各种算法模型及其实战案例,欢迎点赞,关注共同学习交流。
本文介绍了新闻分类实战案例,并通过两种方法CounterVecorizer与TfidVectorizer构建特征向量,然后分别建模对比。
目录
1. 导入数据并查看信息
2. 使用CountVectorizer构建单词字典并建模预测
2.1 CountVectorizer用法示例
2.2 使用CountVectorizer进行特征向量转换
2.3 使用贝叶斯模型进行建模预测
3. 使用TfidfVectorizer进行特征向量转换并建模预测
3.1 TfidfVectorizer使用示例
3.2 对新闻数据进行TfidfVectorizer变换
3.3 进行建模与预测
3.4 去除停用词并进行建模与预测
1. 导入数据并查看信息
from sklearn.datasets import fetch_20newsgroupsfrom sklearn.model_selection import train_test_split# 加载新闻数据news = fetch_20newsgroups(subset='all')# data为一个列表,长度18846,每一个元素为一个新闻内容的字符串print(len(news.data))18846news.data"From: Mamatha Devineni Ratnam <mr47+@andrew.cmu.edu>\nSubject: Pens fans reactions\nOrganization: Post Office, Carnegie Mellon, Pittsburgh, PA\nLines: 12\nNNTP-Posting-Host: po4.andrew.cmu.edu\n\n\n\nI am sure some bashers of Pens fans are pretty confused about the lack\nof any kind of posts about the recent Pens massacre of the Devils. Actually,\nI ambit puzzled too and a bit relieved. However, I am going to put an end\nto non-PIttsburghers' relief with a bit of praise for the Pens. Man, they\nare killing those Devils worse than I thought. Jagr just showed you why\nhe is much better than his regular season stats. He is also a lot\nfo fun to watch in the playoffs. Bowman should let JAgr have a lot of\nfun in the next couple of games since the Pens are going to beat the pulp out of Jersey anyway. I was very disappointed not to see the Islanders lose the final\nregular season game. PENS RULE!!!\n\n"# news.target为目标分类对应的编号news.targetarray()# 目标标签名称有20个,因此一共分20类新闻len(news.target_names)20# 查看第一篇新闻属于什么类别print(news.target)print(news.target_names])10rec.sport.hockey2. 使用CountVectorizer构建单词字典并建模预测
CountVectorizer方法构建单词的字典,每个单词实例被转换为特征向量的一个数值特征,每个元素是特定单词在文本中出现的次数
2.1 CountVectorizer用法示例
from sklearn.feature_extraction.text import CountVectorizertexts=["pig bird cat","dog dog cat cat","bird fish bird", 'pig bird']cv = CountVectorizer()# 将文本向量化cv_fit=cv.fit_transform(texts)# 查看转换后的向量,会统计单词个数,并写在指定索引位置print(cv.get_feature_names()) # 获取单词序列print(cv_fit.toarray()) # 将文本变为向量['bird', 'cat', 'dog', 'fish', 'pig'][ ]2.2 使用CountVectorizer进行特征向量转换
cv = CountVectorizer()cv_data = cv.fit_transform(news.data)2.3 使用贝叶斯模型进行建模预测
from sklearn.model_selection import cross_val_score from sklearn.naive_bayes import MultinomialNBx_train,x_test,y_train,y_test = train_test_split(cv_data, news.target)mul_nb = MultinomialNB()train_scores = cross_val_score(mul_nb, x_train, y_train, cv=3, scoring='accuracy')test_scores = cross_val_score(mul_nb, x_test, y_test, cv=3, scoring='accuracy')print("train scores:", train_scores)print("test scores:", test_scores)train scores: test scores: 3. 使用TfidfVectorizer进行特征向量转换并建模预测
TfidfVectorizer使用了一个高级的计算方法,称为Term Frequency Inverse Document Frequency (TF-IDF)。IDF是逆文本频率指数(Inverse Document Frequency)。
TFIDF的主要思想是:如果某个词或短语在一篇文章中出现的频率TF高,并且在其他文章中很少出现,则认为此词或者短语具有很好的类别区分能力,适合用来分类。
它一个衡量一个词在文本或语料中重要性的统计方法。直觉上讲,该方法通过比较在整个语料库的词的频率,寻求在当前文档中频率较高的词。这是一种将结果进行标准化的方法,可以避免因为有些词出现太过频繁而对一个实例的特征化作用不大的情况(我猜测比如a和and在英语中出现的频率比较高,但是它们对于表征一个文本的作用没有什么作用)。
3.1 TfidfVectorizer使用示例
from sklearn.feature_extraction.text import TfidfVectorizer# 文本文档列表text = ["The quick brown fox jumped over the lazy dog.","The lazy dog.","The brown fox"]# 创建变换函数vectorizer = TfidfVectorizer()# 词条化以及创建词汇表vectorizer.fit(text)# 总结print(vectorizer.vocabulary_)print(vectorizer.idf_)# 编码文档vector = vectorizer.transform(])# 总结编码文档print(vector.shape)print(vector.toarray()){'the': 7, 'quick': 6, 'brown': 0, 'fox': 2, 'jumped': 3, 'over': 5, 'lazy': 4, 'dog': 1}(1, 8)[]3.2 对新闻数据进行TfidfVectorizer变换
# 创建变换函数vectorizer = TfidfVectorizer()# 词条化以及创建词汇表tfidf_data = vectorizer.fit_transform(news.data)3.3 进行建模与预测
x_train,x_test,y_train,y_test = train_test_split(tfidf_data, news.target)mul_nb = MultinomialNB()train_scores = cross_val_score(mul_nb, x_train, y_train, cv=3, scoring='accuracy')test_scores = cross_val_score(mul_nb, x_test, y_test, cv=3, scoring='accuracy')print("train scores:", train_scores)print("test scores:", test_scores)train scores: test scores: 3.4 去除停用词并进行建模与预测
def get_stop_words(): result = set() for line in open('stopwords_en.txt', 'r').readlines(): result.add(line.strip()) return result# 加载停用词stop_words = get_stop_words()# 创建变换函数vectorizer = TfidfVectorizer(stop_words=stop_words)# 词条化以及创建词汇表tfidf_data = vectorizer.fit_transform(news.data)x_train,x_test,y_train,y_test = train_test_split(tfidf_data,news.target)mul_nb = MultinomialNB(alpha=0.01)train_scores = cross_val_score(mul_nb, x_train, y_train, cv=3, scoring='accuracy')test_scores = cross_val_score(mul_nb, x_test, y_test, cv=3, scoring='accuracy')print("train scores:", train_scores)print("test scores:", test_scores)train scores: test scores:
通过对比发现使用 TfidVectorizer构建特征向量的建模效果要好于CounterVecorizer。同时去除停用词之后,模型准确率也会有较大的提升。
如果内容对你有帮助,感谢点赞+关注哦!
更多干货内容持续更新中…
页:
[1]