将微博文本csv文件用LSTM模型进行训练,并用matplotlib绘制精度变化曲线的代码
时间: 2024-03-13 19:44:29 浏览: 61
微博csv文件-数据集
以下是将微博文本csv文件用LSTM模型进行训练,并用matplotlib绘制精度变化曲线的代码:
```python
import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, LSTM, Embedding
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
# 加载数据
data = pd.read_csv("weibo.csv", encoding='utf-8')
# 分离特征和标签
X = data['text'].values
y = data['label'].values
# 分割数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 构建词汇表
tokenizer = Tokenizer(num_words=10000)
tokenizer.fit_on_texts(X_train)
# 将文本转换为数字序列
X_train_seq = tokenizer.texts_to_sequences(X_train)
X_test_seq = tokenizer.texts_to_sequences(X_test)
# 填充序列
X_train_pad = pad_sequences(X_train_seq, maxlen=100)
X_test_pad = pad_sequences(X_test_seq, maxlen=100)
# 创建模型
model = Sequential()
model.add(Embedding(input_dim=10000, output_dim=100, input_length=100))
model.add(LSTM
阅读全文