基于keras 的 python情感分析案例IMDB影评情感分析

(来源-魏贞原老师的深度学习一书)

情感分析是自然语言处理中很重要的一个方向,目的是让计算机理解文本中包含的情感信息。在这里将通过IMDB(互联网电影资料库)收集的对电影评论的数据集,分析某部电影是一部好电影还是一部不好的电影,借此研究情感分析问题。

1.导入数据

为了便于在模型训练中使用数据集,在keras提供的数据集将单词转化成整数,这个整数代表单词在整个数据集中的流行程度。

导入数据之后,将训练数据集和评估数据集合并,并查看相关统计信息,如中值和标准差,结果通过箱线图和直方图展示,代码如下:

from keras.datasets import imdb
import numpy as np
from matplotlib import pyplot as plt
(x_train, y_train), (x_validation, y_validation) = imdb.load_data()

#合并训练数据集和评估数据集
x = np.concatenate((x_train , x_validation), axis=0)
y = np.concatenate((y_train , y_validation), axis=0)

print('x shape is %s , y shape is %s' % (x.shape, y.shape))
print('Classes: %s' % np.unique(y))

print('Total words: %s' % len(np.unique(np.hstack(x))))

result = [len(word) for word in x]
print('Mean: %.2f words (STD: %.2f)' % (np.mean(result), np.std(result)))

#图形表示
plt.subplot(121)
plt.boxplot(result)
plt.subplot(122)
plt.hist(result)
plt.show()

猜你喜欢

转载自blog.csdn.net/bysjlwdx/article/details/84935889