博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
朴素贝叶斯应用:垃圾邮件分类
阅读量:5457 次
发布时间:2019-06-15

本文共 2003 字,大约阅读时间需要 6 分钟。

import csv# 读数据file_path = r'data\EmailData.txt'EmailData = open(file_path,'r',encoding='utf-8')Email_data = []Email_target = []csv_reader = csv.reader(EmailData,delimiter='\t')# 将数据分别存入数据列表和目标分类列表for line in csv_reader:    Email_data.append(line[1])    Email_target.append(line[0])EmailData.close()
# 把无意义的符号都替换成空格Email_data_clear = []for line in Email_data:    # line :'Go until jurong point, crazy.. Available only in bugis n great world la e buffet...'    # 每一行都去掉无意义符号并按空格分词    for char in line:        if char.isalpha() is False:            # 不是字母,发生替换操作:            newString = line.replace(char," ")    tempList = newString.split(" ")    # 将处理好后的一行数据追加到存放干净数据的列表    Email_data_clear.append(tempList)
# 去掉长度不大于3的词和没有语义的词Email_data_clear2 = []for line in Email_data_clear:    tempList = []    for word in line:        if word != '' and len(word) > 3 and word.isalpha():            tempList.append(word)    tempString = ' '.join(tempList)    Email_data_clear2.append(tempString)Email_data_clear = Email_data_clear2
# 将数据分为训练集和测试集from sklearn.model_selection import train_test_splitx_train,x_test,y_train,y_test = train_test_split(Email_data_clear2,Email_target,test_size=0.3,random_state=0,stratify=Email_target)
# 建立数据的特征向量from sklearn.feature_extraction.text import TfidfVectorizertfidf = TfidfVectorizer()
X_train = tfidf.fit_transform(x_train)X_test = tfidf.transform(x_test)
print(type(X_train),type(X_test))

# 观察向量import numpy as npX_train = X_train.toarray()X_test = X_test.toarray()X_train.shape

# 输出不为0的列for i in range(X_train.shape[0]):    for j in range(X_train.shape[1]):        if X_train[i][j] != 0:            print(i,j,X_train[i][j])

# 提取特征值tfidf.get_feature_names()[630:650]

#建立模型from sklearn.naive_bayes import GaussianNBgnb = GaussianNB()
module = gnb.fit(X_train,y_train)y_predict = module.predict(X_test)
# 输出模型分类的各个指标 from sklearn.metrics import classification_report
cr = classification_report(y_predict,y_test)print(cr)

转载于:https://www.cnblogs.com/GZCC-11-28/p/10037447.html

你可能感兴趣的文章
js实现文字无间断左右滚动和图片左右滚动
查看>>
题目11:软件工程等名词解释
查看>>
自己写平方根squareroot函数
查看>>
关于RTSP-Over-HTTP
查看>>
SQL SERVER 2005如何建立自动备份的维护计划
查看>>
深入剖析C#的多态
查看>>
SQL2008 用户'sa'登录失败(错误18456)图文解决方法
查看>>
json属性名必须加引号的讨论
查看>>
Winform--数据库链接(EF CodeFirst)
查看>>
TCP的发送缓冲区和接收缓冲区
查看>>
SQL Server的导出导入方式有
查看>>
Unity3D_(Shuriken粒子系统)制作简单的烟花爆炸效果
查看>>
3. Longest Substring Without Repeating Characters
查看>>
织梦添加搜索功能
查看>>
JDK的安装和环境变量配置
查看>>
jmeter学习记录--05--Beanshell2
查看>>
HDU1402 HDU4609 FFT快速DFT
查看>>
DataGridView添加一行数据、全选、取消全选、清空数据、删除选中行
查看>>
抽象工厂模式
查看>>
数据库连接数使用情况监控
查看>>