기말 숙제

4630 단어
import jieba

path=r'"E:\ \147\"'
 
with open(r'E:\ \stopsCN.txt',encoding='utf-8')as f:
    stopword=f.read().split('
') List01=[] List02=[] def read_text(name,start,end): for file in range(start,end): file = 'E:\\ \\147\\'+name+'\\'+str(file)+".txt" with open(file,'r',encoding='utf-8') as f: texts=f.read() target = name texts = "".join([text for text in texts if text.isalpha()]) texts = [text for text in jieba.cut(texts,cut_all=True) if len(text) >=2] texts = " ".join([text for text in texts if text not in stopword]) List01.append(target) List02.append(texts) read_text(" ",798977,798997) read_text(" ",256822,256842) read_text(" ",264410,264430) read_text(" ",644579,644599) List01 List02 from sklearn.model_selection import train_test_split x_train,x_test,y_train,y_test = train_test_split(List02,List01,test_size=0.2) from sklearn.feature_extraction.text import TfidfVectorizer vec = TfidfVectorizer() X_train = vec.fit_transform(x_train) X_test = vec.transform(x_test) from sklearn.naive_bayes import MultinomialNB from sklearn.model_selection import cross_val_score from sklearn.metrics import classification_report # mnb = MultinomialNB() module = mnb.fit(X_train, y_train) y_predict = module.predict(X_test) # 5 scores=cross_val_score(mnb,X_test,y_test,cv=5) print(" :",scores.mean()) print(" :
",classification_report(y_predict,y_test)) import collections # testCount = collections.Counter(y_test) predCount = collections.Counter(y_predict) print(' :',testCount,'
', ' ', predCount) # , , , nameList = list(testCount.keys()) testList = list(testCount.values()) predictList = list(predCount.values()) x = list(range(len(nameList))) print(" :",nameList,'
'," :",testList,'
'," :",predictList)

좋은 웹페이지 즐겨찾기