1. 程式人生 > >12.03作業

12.03作業

要點:

理解樸素貝葉斯演算法

理解機器學習演算法建模過程

理解文字常用處理流程

理解模型評估方法

 垃圾郵件分類
資料準備:
用csv讀取郵件資料,分解出郵件類別及郵件內容。
對郵件內容進行預處理:去掉長度小於3的詞,去掉沒有語義的詞等
嘗試使用nltk庫:
pip install nltk
nltk.download
不成功:就使用詞頻統計的處理方法
訓練集和測試集資料劃分
from sklearn.model_selection import train_test_split

from nltk.corpue import stopwords
stops=stopwords('english')
stops
tokens
=[token for tokens if token not in stops] ' '.join(tokens)
text

#pip install nltk
#nltk.download
from sklearn.model_selection import train_test_split
import nltk
from nltk.stem import WordNetLemmatizer
#lemmatizer=WordNetLemmatizer()
#lemmatizer.lemmatize('leaves')
#垃圾郵件分類
text='''Yes i think so. I am in office but my lap is in room i think thats on for the last few days. I didnt shut that down'''

import
nltk from nltk.stem import WordNetLemmatizer #lemmatizer=WordNetLemmatizer() #lemmatizer.lemmatize('leaves') #預處理 def preprocessing(text): #text=text.decode("utf-8") tokens=[word for sent in nltk.sent_tokenize(text) for word in nltk.word_tokenize(sent)] stops=stopwords.words('english') tokens
=[token for token in tokens if token not in stops] tokens=[token.lower() for token in tokens if len(token)>=3] lmtzr=WordNetLemmatizer() tokens=[lmtzr.lemmatize(token) for token in tokens] preprocessed_text=' '.join(tokens) return preprocessed_text text #讀取資料集 import csv #用csv讀取郵件資料,分解出郵件類別及郵件內容 file_path = r'C:\Users\Administrator\Desktop\SMSSpamCollectionjsn.txt' sms = open(file_path,'r',encoding = 'utf-8') sms_data = [] sms_label = [] csv_reader = csv.reader(sms,delimiter='\t') for line in csv_reader: sms_label.append(line[0]) sms_data.append(line[1]) sms.close() #按0.7:0.3比例分為訓練集和測試集 import numpy as np sms_data=np.array(sms_data) sms_label=np.array(sms_label) from sklearn.model_selection import train_test_split x_train,x_test,y_train,y_test = train_test_split(sms_data,sms_label,test_size=0.3,random_state=0,stratify=sms_label) #訓練集,測試集 #將其向量化 from sklearn.feature_extraction.text import TfidfVectorizer vectorizer = TfidfVectorizer(min_df = 2,ngram_range=(1,2),stop_words='english',strip_accents='unicode',norm='l2') X_train = vectorizer.fit_transform(x_train) X_test = vectorizer.transform(x_test) #樸素貝葉斯分類群 from sklearn.naive_bayes import MultinomialNB clf=MultinomialNB().fit(X_train,y_train) y_nb_pred=clf.predict(X_test) #分類結果顯示 from sklearn.metrics import confusion_matrix from sklearn.metrics import classification_report print(y_nb_pred.shape,y_nb_pred)#x_test預測結果 print('nb_confusion_matrik:') cm=confusion_matrix(y_test,y_nb_pred)#混淆矩陣 print(cm) print('nb_classification_report:') cr=classification_report(y_test,y_nb_pred)#主要分類指標的文字報告 print(cr) feature_names=vectorizer.get_feature_names()#出現過的單詞列表 coefs=clf.coef_#先驗概率 P(x_i|y),6034 feature_log_prob_ intercept=clf.intercept_#P(y),class_log_prior_:array,shape(n_classes,) coefs_with_fns=sorted(zip(coefs[0],feature_names))#對數概率P(x_i|y)與單詞x_i對映 n=10 top=zip(coefs_with_fns[:n],coefs_with_fns[:-(n+1):-1]) for(coef_1,fn_1),(coef_2,fn_2) in top: print('\t%.4f\t%-15s\t\t%.4f\t%-15s'%(coef_1,fn_1,coef_2,fn_2)) sms_label print(len(x_train),len(x_test)) print(X_train.shape,X_test.shape) x_train X_train a=X_train.toarray() a for i in range(1000): for j in range(5984): if a[i,j]!=0: print(i,j,a[i,j]) vectorizer.get_feature_names()[1610]