惡意網(wǎng)站是怎么實現(xiàn)的seo優(yōu)化員
前言
🔥 優(yōu)質競賽項目系列,今天要分享的是
酒店評價的情感傾向分析
該項目較為新穎,適合作為競賽課題方向,學長非常推薦!
🧿 更多資料, 項目分享:
https://gitee.com/dancheng-senior/postgraduate
概述
本文基于7K條攜程酒店評價數(shù)據(jù)為文本數(shù)據(jù),將其導入到Keras的模型架構然后進行訓練出一個可用于實際場所預測情感的模型。
項目所需模塊
?
import tensorflow as tf
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
import tensorflow.keras as keras# 導入jieba分詞庫
import jieba
import re
數(shù)據(jù)
數(shù)據(jù)說明
7000多條攜程酒店評論數(shù)據(jù),5000多條正向評論,2000多條負向評論。
字段說明
- 評論數(shù)目(總體):7766
- 評論數(shù)目(正向):5322
- 評論數(shù)目(負向):2444
數(shù)據(jù)處理
?
# 讀取數(shù)據(jù)
data = pd.read_csv("/home/kesci/input/labelreview5456/ChnSentiCorp_htl_all.csv")
# 查看數(shù)據(jù)的前5項
data.head()
分詞處理
?
# 去除標點符號和數(shù)字
# 要去除標點符號和數(shù)字,常用的辦法就是使用正則表達式來處理,或者自行編寫遍歷替換函數(shù)# 模式串
patten = r"[!\"#$%&'()*+,-./:;<=>?@[\\\]^_`{|}~—!,。?·¥、《》···【】:" "''\s0-9]+"
re_obj = re.compile(patten)# 替換函數(shù)--去除標點符號和數(shù)字
def clear(text):return re_obj.sub('', text)# 將正則表達式替換函數(shù)應用于每一行
data["review"] = data["review"].apply(clear)
# 查看前5行替換結果data["review"][:5]
采用精簡處理,啟用HMM(隱式馬爾科夫網(wǎng)絡)處理
?
def cut_words(words):return jieba.lcut(words) # 使用lcut分詞#apply函數(shù)對series的每一行數(shù)據(jù)進行處理
data["review"] = data["review"].apply(cut_words)
data["review"][:5]
停用詞處理
?
# 使用 中文停用詞表 stop_words = "/home/kesci/work/stopwords-master/stopwords.txt"stop_list = [i.strip() for i in open(stop_words, encoding='utf-8').readlines()] #讀取停用詞列表def remove_stop(words): #移除停用詞函數(shù)texts = []for word in words: # 遍歷詞列表里的每一個詞if word not in stop_list: # 若不在停用詞列表中就將結果追加至texts列表中texts.append(word)return textsdata['review'] = data['review'].apply(remove_stop)# 查看前5行data["review"][:5]
樣本均衡
?
data["label"].value_counts().plot(kind = 'bar')
plt.text(0, 6000, str(data["label"].value_counts()[1]),ha = 'center', va = 'top')
plt.text(1, 3000, str(data["label"].value_counts()[0]),ha = 'center', va = 'top')
plt.ylim(0, 6500)
plt.title('正負樣本的個數(shù)')
plt.show()
從柱狀圖可以看出,該數(shù)據(jù)集共7766條數(shù)據(jù),其中正樣本(label = 1)共有5322條,負樣本(label = 0)共有2444條,沒有重復數(shù)據(jù)
顯然樣本存在嚴重的不均衡問題,這里考慮兩種樣本均衡的策略
(1)欠采樣,正負樣本各2000條,一共4000條
(2)過采樣,正負樣本各3000條,一共6000條
為減少計算量和對比兩種均衡策略的效果,這里采用先把整體數(shù)據(jù)進行處理,再做樣本均衡采樣
?
def get_balanced_words(size,positive_comment=data[data['label'] == 1],negtive_comment=data[data['label'] == 0]):word_size = size // 2#獲取正負評論數(shù)num_pos = positive_comment.shape[0]num_neg = negtive_comment.shape[0]# 當 正(負)品論數(shù)中<采樣數(shù)量/2 時,進行上采樣,否則都是下采樣;# 其中pandas的sample方法里的repalce參數(shù)代表是否進行上采樣,默認不進行balanced_words = pd.concat([positive_comment.sample(word_size,replace=num_pos < word_size,random_state=0),negtive_comment.sample(word_size,replace=num_neg < word_size,random_state=0)])# 打印樣本個數(shù)print('樣本總數(shù):', balanced_words.shape[0])print('正樣本數(shù):', balanced_words[data['label'] == 1].shape[0])print('負樣本數(shù):', balanced_words[data['label'] == 0].shape[0])print('')return balanced_words
建立多層感知機分類模型
可以看到共有四層:平坦層共有1600個神經(jīng)元,平坦層在這里可以看作為輸入層。隱藏層共有256個神經(jīng);輸出層只有1個神經(jīng)元。全部必須訓練的超參數(shù)有474113個,通常超參數(shù)數(shù)值越大,代表此模型越復雜,需要更多時間進行訓練。
訓練模型
網(wǎng)絡檢測率以及檢測結果
?
input_text = """去之前會有擔心,因為疫情,專門打了電話給前臺,前臺小哥哥好評,耐心回答,打消了我的顧慮,nice!! 看得出有做好防疫情清潔消毒工作,前臺登記反復詢問,確保出行軌跡安全,體溫測量登記,入住好評,選了主題房,設計是我喜歡的.總之下次有需要還是會自住或推薦!!"""predict_review(input_text)result : 正面評價!
至此,對攜程酒店評價的情感傾向分析,以建立一個簡單的多層感知器模型結束,由于文章所限,后續(xù)的模型優(yōu)化以及與其他深度學習的模型的比較就不進行簡述,有興趣的同學可以留意學長后續(xù)文章。謝謝各位同學!
最后
🧿 更多資料, 項目分享:
https://gitee.com/dancheng-senior/postgraduate