基于深度学习的情感分析代码
时间: 2023-12-06 22:04:38 浏览: 94
电商评论情感分析-基于深度学习LSTM算法实现的用户情感分析python源码(可爬取京东商城数据).zip
以下是一个基于深度学习的情感分析代码的示例,使用了Keras和Tensorflow作为主要工具:
```python
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD
from keras.utils import np_utils
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
# 加载数据
data = pd.read_csv('sentiment_data.csv')
# 将情感标签编码为数字
encoder = LabelEncoder()
encoder.fit(data['sentiment'])
data['sentiment'] = encoder.transform(data['sentiment'])
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(data['text'], data['sentiment'], test_size=0.2)
# 将文本转换为词向量
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
# 设置最大文本长度为1000
max_words = 1000
tokenizer = Tokenizer(num_words=max_words)
tokenizer.fit_on_texts(X_train)
X_train = tokenizer.texts_to_sequences(X_train)
X_test = tokenizer.texts_to_sequences(X_test)
# 填充序列以保证相同长度
maxlen = 100
X_train = pad_sequences(X_train, padding='post', maxlen=maxlen)
X_test = pad_sequences(X_test, padding='post', maxlen=maxlen)
# 将标签进行one-hot编码
num_classes = len(np.unique(y_train))
y_train = np_utils.to_categorical(y_train, num_classes)
y_test = np_utils.to_categorical(y_test, num_classes)
# 构建神经网络模型
model = Sequential()
model.add(Dense(512, input_shape=(maxlen,)))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
# 设置优化器和损失函数
sgd = SGD(lr=0.01)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
# 训练模型
batch_size = 32
epochs = 10
model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(X_test, y_test))
# 评估模型
score = model.evaluate(X_test, y_test, batch_size=batch_size)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
```
这个模型使用了一个Dense层和一个softmax激活函数来进行分类,同时使用了Dropout层来防止过拟合。训练数据使用了SGD优化器和交叉熵损失函数,同时使用了准确率作为评估指标。
阅读全文