码迷,mamicode.com
首页 > 其他好文 > 详细

13-垃圾邮件分类2

时间:2020-05-23 18:50:37      阅读:52      评论:0      收藏:0      [点我收藏+]

标签:文本   ann   from   关系   ssi   数据预处理   word   读取   ict   

1.读取

2.数据预处理

# 词性还原
def get_wordnet_pos(treebank_tag):
if treebank_tag.startswith(‘J‘):
return nltk.corpus.wordnet.ADJ
elif treebank_tag.startswith(‘V‘):
return nltk.corpus.wordnet.VERB
elif treebank_tag.startswith(‘N‘):
return nltk.corpus.wordnet.NOUN
elif treebank_tag.startswith(‘R‘):
return nltk.corpus.wordnet.ADV
else:
return

3.数据划分—训练集和测试集数据划分

from sklearn.model_selection import train_test_split

x_train,x_test, y_train, y_test = train_test_split(data, target, test_size=0.2, random_state=0, stratify=y_train)

4.文本特征提取

sklearn.feature_extraction.text.CountVectorizer

https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html?highlight=sklearn%20feature_extraction%20text%20tfidfvectorizer

sklearn.feature_extraction.text.TfidfVectorizer

https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html?highlight=sklearn%20feature_extraction%20text%20tfidfvectorizer#sklearn.feature_extraction.text.TfidfVectorizer

from sklearn.feature_extraction.text import TfidfVectorizer

tfidf2 = TfidfVectorizer()

观察邮件与向量的关系

向量还原为邮件


from sklearn.feature_extraction.text import TfidfVectorizer

vectorizer = TfidfVectorizer()

X_train = vectorizer.fit_transform(x_train)
X_test = vectorizer.transform(x_test)

print(X_train.toarray().shape)
print(X_test.toarray().shape)

4.模型选择

from sklearn.naive_bayes import GaussianNB

from sklearn.naive_bayes import MultinomialNB

说明为什么选择这个模型?

5.模型评价:混淆矩阵,分类报告

from sklearn.metrics import confusion_matrix

confusion_matrix = confusion_matrix(y_test, y_predict)

说明混淆矩阵的含义

from sklearn.metrics import classification_report

说明准确率、精确率、召回率、F值分别代表的意义

 

 

6.比较与总结

如果用CountVectorizer进行文本特征生成,与TfidfVectorizer相比,效果如何?

13-垃圾邮件分类2

标签:文本   ann   from   关系   ssi   数据预处理   word   读取   ict   

原文地址:https://www.cnblogs.com/201706120196y/p/12943434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!