python机器学习机器学习——文本情感分析(英文文本情感分析)文本情感分析(英文文本情感分析)
本人机器学习课程的小作业,记录一下,希望可以帮到一些小伙伴。
项目介绍,给一段英文文本(英文影评评论)来预测情感是正向还是负向
模型使用的是LSTM+RNN。
代码包括数据处理,模型训练,对新数据做出预测,并将预测结果(正向情感)保存到result.txt中
软件:anaconda3
一一.数据集介绍数据集介绍
数据集链接: https://pan.baidu.com/s/1oIXkaL_SL9GSN3S56ZwvWQ
提取码: qgtg
训练集labeledTrainData.tsv(24500条带标签的训练数据)
id sentiment review 分别表示:每段文本的唯一ID,情感色彩类别标签,待分析的文本数据。
“, ” “)
return re.sub(strip_special_chars, “”, string.lower())
# #生成索引矩阵,得到24500*250的索引矩阵
# ids=np.zeros((24500,maxSeqLength),dtype=’int32′)
# #print(ids.shape) #输出结果为(24500,250)
# fileCounter=0
# for pf in range(0,len(df)):
# #print(pf)
# indexCounter=0
# cleanedLine=cleanSentences(df[‘review’][pf])
# split=cleanedLine.split()
# for word in split:
# try:
# #print(‘111’)
# ids[fileCounter][indexCounter]=wordsList.index(word)
# except ValueError:
# ids[fileCounter][indexCounter]=399999
# indexCounter=indexCounter+1
# if indexCounter>=maxSeqLength:
# break
# fileCounter=fileCounter+1
# print(‘down1’)
# np.save(‘C:/NLP/idsMatrix’,ids)
#上述注释后,将生成的索引矩阵保存到idsMatrix.npy文件中。避免了每次都要生成索引矩阵
ids=np.load(‘C:/NLP/idsMatrix.npy’)
print(ids.shape)
#辅助函数
from random import randint
def getTrainBatch():
labels=[] arr=np.zeros([batchSize,maxSeqLength])
i=0
for i in range(0,32):
j=0
while j<1:
评论1