import csv# 读数据file_path = r'G:\PyCharm\SMSSpamCollectionjsn.txt'smsData = open(file_path,'r',encoding='utf-8')E_data = []E_target = []csv_reader = csv.reader(smsData,delimiter='\t')# 将数据分别存入数据列表和目标分类列表for line in csv_reader: E_data.append(line[1]) E_target.append(line[0])smsData.close()#数据预处理:处理数据E_data_clear = []# 把无意符号替换成空格for line in E_data: # 去掉无意义符号并按空格分词 for char in line: if char.isalpha() is False: # 不是字母,发生替换操作: newString = line.replace(char," ") tempList = newString.split(" ") # 将处理好后的一行数据追加到存放干净数据的列表 E_data_clear.append(tempList)# 去掉长度不大于3的词和没有语义的词Email_data_clear2 = []for line in E_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,E_target,test_size=0.3,random_state=0,stratify=E_target)# 将其 转化为向量from sklearn.feature_extraction.text import TfidfVectorizervectorizer = TfidfVectorizer()X_train = vectorizer.fit_transform(x_train)X_test = vectorizer.transform(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])
# 建立模型from sklearn.naive_bayes import GaussianNBclf = GaussianNB()module = clf.fit(X_train,y_train)y_predict = module.predict(X_test)# 输出模型分类from sklearn.metrics import classification_reportcr = classification_report(y_predict,y_test)print(cr)