How to run predict on time series data?

Hi, noob here!

I am using this example: https://www.tensorflow.org/tutorials/structured_data/time_series
How can I use a 24h slice of test_df to predict the 25th hour? When I slice a 24 hour piece, scale it with scaler.transform() and feed it into lstm_model(), I get erratic results.
Can someone please provide me with an example?

Thank you very much!!

@pomputer Welcome to the Tensorflow Forum

Here’s a breakdown of steps to correctly predict the 25th hour using a 24-hour slice of test_df in the given time series tutorial:

Prepare the 24-hour slice :

import numpy as np
test_slice = test_df[-24:] # Select the last 24 hours
test_slice = np.expand_dims(test_slice, axis=0) # Add a batch dimension`

Scale the slice using the same scaler:

Pythonscaled_test_slice = scaler.transform(test_slice)

Reshape for LSTM input:

timesteps = 24
features = 1
scaled_test_slice = scaled_test_slice.reshape(1, timesteps, features)

Make the prediction:

Pythonprediction = lstm_model.predict(scaled_test_slice)

Inverse transform the prediction:

Pythonpredicted_value = scaler.inverse_transform(prediction)[0][0] # Extract the prediction

Also consider below hyperparameter tuning , scaling, batch norm, etc

  • Ensure you’re using the same scaler object that was used to scale the training data.
  • Verify the expected input shape of your lstm_model and reshape accordingly.
  • Add a batch dimension (even for single predictions) if your model expects it.
  • Check for inconsistencies between training and test data (e.g., missing values, different distributions).
  • If erratic results persist, consider overfitting and techniques like regularization or hyperparameter tuning.
  • Experiment with different model architectures, hyperparameters, and scaling techniques to find the best fit for your data.

Let us know if this helps!