python - 如何为 LSTM 和 Keras 构建面板数据?
问题描述
我试图弄清楚如何构建我的数据集并构建 X 和 y 以便它可以与 Keras 的 Stacked LSTM 一起用于序列分类。
我有面板数据,我试图预测分类。我不完全确定如何理解时间步长或如何根据我的面板数据正确制作数据的形状。
# Libraries
from keras.models import Sequential
from keras.layers import LSTM, Dense
import numpy as np
import pandas as pd
# Here is an example of my data
df = pd.read_csv('https://raw.githubusercontent.com/rocketfish88/democ/master/sample2.csv')
df
# Contains a handful of features, a target, year, and id of the observation
id year x1 x2 x3 y
0 A 2015 1 1 1 1
1 A 2016 2 2 2 1
2 A 2017 3 3 3 2
3 A 2018 4 4 4 2
4 B 2015 1 1 1 3
5 B 2016 2 2 2 2
6 B 2017 3 3 3 1
7 B 2018 4 4 4 1
8 C 2015 1 1 1 2
9 C 2016 2 2 2 2
10 C 2017 3 3 3 3
11 C 2018 4 4 4 2
Keras.io 通过示例提供以下内容:
data_dim = 16
timesteps = 8
num_classes = 10
# expected input data shape: (batch_size, timesteps, data_dim)
model = Sequential()
model.add(LSTM(32, return_sequences=True,
input_shape=(timesteps, data_dim))) # returns a sequence of vectors of dimension 32
model.add(LSTM(32, return_sequences=True)) # returns a sequence of vectors of dimension 32
model.add(LSTM(32)) # return a single vector of dimension 32
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
# Generate dummy training data
x_train = np.random.random((1000, timesteps, data_dim))
y_train = np.random.random((1000, num_classes))
# Generate dummy validation data
x_val = np.random.random((100, timesteps, data_dim))
y_val = np.random.random((100, num_classes))
model.fit(x_train, y_train,
batch_size=64, epochs=5,
validation_data=(x_val, y_val))
对于如何获取我的数据集并将其转换为(大小、时间步长、维度)的正确形状,我相当迷茫
我很感激任何帮助!
解决方案
推荐阅读
- python - twine 在哪里寻找证书?
- javascript - Bootstrap-Vue - 使子组件在 b-modal 组件中工作
- python - 在Python中返回矩阵单行元素之和的函数
- java - Spring Form - 使用日期和时间作为输入类型,分配给 LocalDate 和 LocalTime 变量。类型不匹配问题
- unity3d - Oculus Go APK 上的 360 度视频故障
- python - 通过 Django-Filer 而不是重定向下载文件
- security - 如何对 p12 密码进行 John Ripper 攻击(教育目的)?
- javascript - 复制 highcharts 类型
- python - 查找列表中最长的连续正数字符串
- javascript - 仅在主页上加载的脚本出现在其他路由上