Joh*_*tud 5 python panel-data 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
Run Code Online (Sandbox Code Playgroud)
# 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
Run Code Online (Sandbox Code Playgroud)
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))
Run Code Online (Sandbox Code Playgroud)
我对如何获取我的数据集并将其转换为(大小、时间步长、维度)的正确形状感到非常迷茫
我感谢任何帮助!
| 归档时间: |
|
| 查看次数: |
934 次 |
| 最近记录: |