資源簡介
1. 應用樸素貝葉斯算法,對Content 數據集進行分類
1)對數據進行清洗
2)基于給定的詞庫和停止詞,進行文本切詞
3)建立NB模型
代碼片段和文件信息
import?pandas?as?pd
#?讀入評論數據
evaluation?=?pd.read_excel(r‘Contents.xlsx‘)
#?查看數據前10行
print(evaluation.head(10))
#?運用正則表達式,將評論中的數字和英文去除
evaluation.Content?=?evaluation.Content.str.replace(‘[0-9a-zA-Z]‘‘‘)
evaluation.head()
#?導入第三方包
import?jieba
#?加載自定義詞庫
jieba.load_userdict(r‘all_words.txt‘)
#?讀入停止詞
with?open(r‘mystopwords.txt‘?encoding=‘UTF-8‘)?as?words:
????stop_words?=?[i.strip()?for?i?in?words.readlines()]
#?構造切詞的自定義函數,并在切詞過程中刪除停止詞
def?cut_word(sentence):
????words?=?[i?for?i?in?jieba.lcut(sentence)?if?i?not?in?stop_words]
????#?切完的詞用空格隔開
????result?=?‘?‘.join(words)
????return(result)
#?對評論內容進行批量切詞
words?=?evaluation.Content.apply(cut_word)
#?前5行內容的切詞效果
words[:5]
#?導入第三方包
from?sklearn.feature_extraction.text?import?CountVectorizer
#?計算每個詞在各評
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????701641??2019-08-25?09:01??all_words.txt
?????文件?????929935??2019-08-25?09:01??Contents.xlsx
?????文件??????99725??2019-08-25?09:01??mystopwords.txt
?????文件???????3050??2019-09-14?21:07??nbtest.py
-----------?---------??----------?-----??----
??????????????1734351????????????????????4
評論
共有 條評論