Aidan L.
Aidan L.

Reputation: 89

Time Series Prediction | Training the Model

So I loaded and preprocessed my data for time-series prediction. I've created a model, but now I am not sure how to actually train it.

Here is the code:

import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib as plt

# Loading Data
df = pd.read_csv("testdata.csv", header=0, parse_dates=[
                 0], names=['Month', 'People'], index_col=0)

print(df)
print(df.shape)

# Preprocessing
log_df_People = np.log(df.People)
print(log_df_People)
log_df_People_diff = log_df_People - log_df_People.shift()
print(log_df_People_diff)
log_df_People_diff.dropna(inplace=True)

# Creating the Model
model = tf.keras.Sequential()
model.add = tf.keras.layers.LSTM(100, activation="relu", input_shape=(2,))
model.add = tf.keras.layers.Dropout(rate=0.2)
model.add = tf.keras.layers.Dense(1, activation='relu')
model.compile(optimizer='adam', loss='mean_absolute_error',
              metrics=['accuracy'])

# Training the Model?

I did some research, but there isn't exactly an in depth tutorial on how to specifically train a model for time-series prediction.

Upvotes: 0

Views: 151

Answers (1)

Code Pope
Code Pope

Reputation: 5449

It is not clear how your dataframe looks like and why you log it. But here I will show you how you can use LSTM to train a model for prediction. Let's imagine the following is your data:

df = pd.DataFrame({'People':[10,12,11,13,15,18]})

Then you do log for some reason:

log_df_People = np.log(df.People)

Then you shift like this:

import tensorflow as tf
X = log_df_People.to_numpy()[:-1]
Y = log_df_People.shift(-1).to_numpy()[:-1]

Then you create your model:

model = tf.keras.Sequential()
model.add = tf.keras.layers.LSTM(100, activation="relu", input_shape=(2,))
model.add = tf.keras.layers.Dropout(rate=0.2)
model.add = tf.keras.layers.Dense(1, activation='relu')
model.compile(optimizer='adam', loss='mean_absolute_error',
              metrics=['accuracy'])

Finally you train your model for a number of epochs:

model.fit(X,Y,epochs=100)

But generally you should think about using sliding windows to make predictions, but this would require much more description.

Upvotes: 3

Related Questions