期末大作业

import jieba

path=r'"E:\中文数据清理\147\"'
 
with open(r'E:\中文数据清理\stopsCN.txt',encoding='utf-8')as f:
    stopword=f.read().split('\n')




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("分类结果:\n",classification_report(y_predict,y_test))




import collections
# 统计测试集和预测集的各类新闻个数
testCount = collections.Counter(y_test)
predCount = collections.Counter(y_predict)
print('实际:',testCount,'\n', '预测', predCount)
 
# 建立标签列表,实际结果列表,预测结果列表,
nameList = list(testCount.keys())
testList = list(testCount.values())
predictList = list(predCount.values())
x = list(range(len(nameList)))
print("新闻类别:",nameList,'\n',"实际:",testList,'\n',"预测:",predictList)

 

转载于:https://www.cnblogs.com/yulinzzz/p/10166744.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值