数据分析基于朴素贝叶斯的书籍评价信息分类

#-*-coding:utf-8-*-
import  pandas  as  pd
import jieba
from sklearn.feature_extraction.text import CountVectorizer
data=pd.read_csv('./data.csv',encoding='ansi')
#确定特征值与目标
feature=data.loc[:,'内容 ']
target=data.loc[:,'评价']
#将特征值与目标值转化为数值类型
data.loc[data.loc[:,"评价"]=='好评','评价']=0
data.loc[data.loc[:,"评价"]=='差评','评价']=1
#将object转化为int类型
data.loc[:,'评价']=data.loc[:,'评价'].astype('int')
#转化特征值为数值型
content_list=[]
for  tmp  in data.loc[:,'内容 ']:
    res=jieba.cut(tmp,cut_all=False)
    #组装分词
    res_str=','.join(res)
    content_list.append(res_str)
#print(content_list)
#处理停用词
stop_words=[]
with  open('./stopwords.txt',encoding='utf-8')as  f:
    lines=f.readlines()
    for  line in lines:
        line_obj=line.strip()#去除空格
        stop_words.append(line_obj)
#去除重复的停用词
stop_words=list(set(stop_words))
print(stop_words)
#进行统计词数
con_vet=CountVectorizer(stop_words=stop_words)
#统计分词
X=con_vet.fit_transform(content_list)
#获取分词结果
names=con_vet.get_feature_names()
print(names)
#print(X.toarray())
#将特征值与目标值组成完整的数据
import  numpy  as  np

new_data=np.concatenate((X.toarray(),data.loc[:,'评价'].values.reshape((-1,1))),axis=1)
#数组拼接concatenate
print(new_data)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值