最近和小伙伴一起研究文本的情感分类,所以做一个记录。
一、背景:
商品的评论信息,好评与差评约各20000条。
二、目标:
根据现有的样本做一个分类器。
三、方案:
方案一:情感类词库,进行线性求和。
方案二:句向量+传统机器学习分类器。
方案三:句向量+DNN分类器
方案四:词向量+lstm分类器
直接上代码:此次更新的代码为方案二与三,其他后续更新
# import modules & set up logging
import gensim,logging
import pandas as pd #导入Pandas
import numpy as np #导入Numpy
import jieba #导入结巴分词
from __future__ import absolute_import #导入3.x的特征函数
from __future__ import print_function
neg=pd.read_excel('neg.xls',header=None,index=None)
pos=pd.read_excel('pos.xls',header=None,index=None) #读取训练语料完毕
pos['mark']=1
neg['mark']=0 #给训练语料贴上标签
pn=pd.concat([pos,neg],ignore_index=True) #合并语料
cw = lambda x: list(jieba.cut(x)) #定义分词函数
pn['words'] = pn[0].apply(cw)
sentences = pn['words']
model = gensim.models.Word2Vec(sentences,size=64,window=5, min_count=0,workers=4)
idx = range(len(pn))
np.random.shuffle(idx)
pn = pn.loc[idx]
def sen2vec(pd_word):
gg=[]
for i in pd_word.values:
a=np.zeros(64)
n=0
for j in i:
for k in j:
a += model[k]
n=n+1
gg.append(a/n)
return gg
pn['s2v']=sen2vec(pn.iloc[:,[2]])
from sklearn.svm import SVC,LinearSVC,NuSVC
from sklearn.naive_bayes import MultinomialNB,BernoulliNB
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import roc_auc_score
from sklearn.svm import SVC,LinearSVC,NuSVC
from sklearn.ensemble import GradientBoostingClassifier
train_num = 15000
#按keras的输入要求来生成数据
x = np.array(list(pn['s2v']))
y = np.array(list(pn['mark']))
y = y.reshape((-1,1)) #调整标签形状
def model_score(modelname,x,y,train_num):
model = modelname
model.fit(x[:train_num],y[:train_num])
y_predict = model.predict(x[train_num:])
(acc,roc) = accuracy_score(y[train_num:],y_predict),roc_auc_score(y[train_num:],y_predict)
return (acc,roc)
print('BernoulliNB`s accuracy is %f, auc_score is %f' % model_score(BernoulliNB(),x,y,train_num))
print('LogisticRegression`s accuracy is %f, auc_score is %f' % model_score(LogisticRegression(),x,y,train_num))
print('RandomForestClassifier`s accuracy is %f, auc_score is %f' % model_score(RandomForestClassifier(),x,y,train_num))
print('LinearSVC`s accuracy is %f, auc_score is %f' % model_score(LinearSVC(),x,y,train_num))
print('GradientBoostingClassifier`s accuracy is %f, auc_score is %f' % model_score(GradientBoostingClassifier(),x,y,train_num))
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, Embedding,Flatten
from keras.layers import LSTM,GRU,Conv2D,MaxPooling2D,Conv1D,GlobalAveragePooling1D,MaxPooling1D
from keras.optimizers import SGD
#MLP for binary classification
model = Sequential()
model.add(Dense(64,input_dim=64,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit(x[:train_num],y[:train_num],epochs=100,batch_size=32)
score =model.evaluate(x[train_num:],y[train_num:],batch_size=16)
score
结果如下:
BernoulliNB`s accuracy is 0.658640, auc_score is 0.658055
LogisticRegression`s accuracy is 0.767731, auc_score is 0.767476
RandomForestClassifier`s accuracy is 0.808190, auc_score is 0.808713
LinearSVC`s accuracy is 0.774120, auc_score is 0.773867
GradientBoostingClassifier`s accuracy is 0.786405, auc_score is 0.786367
dnn`s accuracy is 0.824570024
结论,相同情况下:
1、可以看到传统的机器学习随机森林较为明显。
2、dnn网络的话表现还是比较好的。