数据分析基于朴素贝叶斯的书籍评价信息分类
生活随笔
收集整理的這篇文章主要介紹了
数据分析基于朴素贝叶斯的书籍评价信息分类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#-*-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 npnew_data=np.concatenate((X.toarray(),data.loc[:,'評價'].values.reshape((-1,1))),axis=1)
#數組拼接concatenate
print(new_data)
總結
以上是生活随笔為你收集整理的数据分析基于朴素贝叶斯的书籍评价信息分类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据分析数据标准化
- 下一篇: jieba 分词的三种模式