4 Common Machine Learning Data Transforms for Time Series Forecasting

Last Updated on

Time series data often requires some preparation prior to being modeled with machine learning algorithms.

For example, differencing operations can be used to remove trend and seasonal structure from the sequence in order to simplify the prediction problem. Some algorithms, such as neural networks, prefer data to be standardized and/or normalized prior to modeling.

Any transform operations applied to the series also require a similar inverse transform to be applied on the predictions. This is required so that the resulting calculated performance measures are in the same scale as the output variable and can be compared to classical forecasting methods.

In this post, you will discover how to perform and invert four common data transforms for time series data in machine learning.

After reading this post, you will know:

  • How to transform and inverse the transform for four methods in Python.
  • Important considerations when using transforms on training and test datasets.
  • The suggested order for transforms when multiple operations are required on a dataset.

Discover how to build models for multivariate and multi-step time series forecasting with LSTMs and more in my new book, with 25 step-by-step tutorials and full source code.

Let’s get started.

4 Common Machine Learning Data Transforms for Time Series Forecasting

4 Common Machine Learning Data Transforms for Time Series Forecasting
Photo by Wolfgang Staudt, some rights reserved.


This tutorial is divided into three parts; they are:

  1. Transforms for Time Series Data
  2. Considerations for Model Evaluation
  3. Order of Data Transforms

Transforms for Time Series Data

Given a univariate time series dataset, there are four transforms that are popular when using machine learning methods to model and make predictions.

They are:

  • Power Transform
  • Difference Transform
  • Standardization
  • Normalization

Let’s take a quick look at each in turn and how to perform these transforms in Python.

We will also review how to reverse the transform operation as this is required when we want to evaluate the predictions in their original scale so that performance measures can be compared directly.

Are there other transforms you like to use on your time series data for modeling with machine learning methods?
Let me know in the comments below.

Need help with Deep Learning for Time Series?

Take my free 7-day email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

Download Your FREE Mini-Course

Power Transform

A power transform removes a shift from a data distribution to make the distribution more-normal (Gaussian).

On a time series dataset, this can have the effect of removing a change in variance over time.

Popular examples are the log transform (positive values) or generalized versions such as the Box-Cox transform (positive values) or the Yeo-Johnson transform (positive and negative values).

For example, we can implement the Box-Cox transform in Python using the boxcox() function from the SciPy library.

By default, the method will numerically optimize the lambda value for the transform and return the optimal value.

The transform can be inverted but requires a custom function listed below named invert_boxcox() that takes a transformed value and the lambda value that was used to perform the transform.

A complete example of applying the power transform to a dataset and reversing the transform is listed below.

Running the example prints the original dataset, the results of the power transform, and the original values (or close to it) after the transform is inverted.

Difference Transform

A difference transform is a simple way for removing a systematic structure from the time series.

For example, a trend can be removed by subtracting the previous value from each value in the series. This is called first order differencing. The process can be repeated (e.g. difference the differenced series) to remove second order trends, and so on.

A seasonal structure can be removed in a similar way by subtracting the observation from the prior season, e.g. 12 time steps ago for monthly data with a yearly seasonal structure.

A single differenced value in a series can be calculated with a custom function named difference() listed below. The function takes the time series and the interval for the difference calculation, e.g. 1 for a trend difference or 12 for a seasonal difference.

Again, this operation can be inverted with a custom function that adds the original value back to the differenced value named invert_difference() that takes the original series and the interval.

We can demonstrate this function below.

Running the example prints the original dataset, the results of the difference transform, and the original values after the transform is inverted.

Note, the first “interval” values will be lost from the sequence after the transform. This is because they do not have a value at “interval” prior time steps, therefore cannot be differenced.


Standardization is a transform for data with a Gaussian distribution.

It subtracts the mean and divides the result by the standard deviation of the data sample. This has the effect of transforming the data to have mean of zero, or centered, with a standard deviation of 1. This resulting distribution is called a standard Gaussian distribution, or a standard normal, hence the name of the transform.

We can perform standardization using the StandardScaler object in Python from the scikit-learn library.

This class allows the transform to be fit on a training dataset by calling fit(), applied to one or more datasets (e.g. train and test) by calling transform() and also provides a function to reverse the transform by calling inverse_transform().

A complete example is applied below.

Running the example prints the original dataset, the results of the standardize transform, and the original values after the transform is inverted.

Note the expectation that data is provided as a column with multiple rows.


Normalization is a rescaling of data from the original range to a new range between 0 and 1.

As with standardization, this can be implemented using a transform object from the scikit-learn library, specifically the MinMaxScaler class. In addition to normalization, this class can be used to rescale data to any range you wish by specifying the preferred range in the constructor of the object.

It can be used in the same way to fit, transform, and inverse the transform.

A complete example is listed below.

Running the example prints the original dataset, the results of the normalize transform, and the original values after the transform is inverted.

Considerations for Model Evaluation

We have mentioned the importance of being able to invert a transform on the predictions of a model in order to calculate a model performance statistic that is directly comparable to other methods.

Additionally, another concern is the problem of data leakage.

Three of the above data transforms estimate coefficients from a provided dataset that are then used to transform the data. Specifically:

  • Power Transform: lambda parameter.
  • Standardization: mean and standard deviation statistics.
  • Normalization: min and max values.

These coefficients must be estimated on the training dataset only.

Once estimated, the transform can be applied using the coefficients to the training and the test dataset before evaluating your model.

If the coefficients are estimated using the entire dataset prior to splitting into train and test sets, then there is a small leakage of information from the test set to the training dataset. This can result in estimates of model skill that are optimistically biased.

As such, you may want to enhance the estimates of the coefficients with domain knowledge, such as expected min/max values for all time in the future.

Generally, differencing does not suffer the same problems. In most cases, such as one-step forecasting, the lag observations are available to perform the difference calculation. If not, the lag predictions can be used wherever needed as a proxy for the true observations in difference calculations.

Order of Data Transforms

You may want to experiment with applying multiple data transforms to a time series prior to modeling.

This is quite common, e.g. to apply a power transform to remove an increasing variance, to apply seasonal differencing to remove seasonality, and to apply one-step differencing to remove a trend.

The order that the transform operations are applied is important.

Intuitively, we can think through how the transforms may interact.

  • Power transforms should probably be performed prior to differencing.
  • Seasonal differencing should be performed prior to one-step differencing.
  • Standardization is linear and should be performed on the sample after any nonlinear transforms and differencing.
  • Normalization is a linear operation but it should be the final transform performed to maintain the preferred scale.

As such, a suggested ordering for data transforms is as follows:

  1. Power Transform.
  2. Seasonal Difference.
  3. Trend Difference.
  4. Standardization.
  5. Normalization.

Obviously, you would only use the transforms required for your specific dataset.

Importantly, when the transform operations are inverted, the order of the inverse transform operations must be reversed. Specifically, the inverse operations must be performed in the following order:

  1. Normalization.
  2. Standardization.
  3. Trend Difference.
  4. Seasonal Difference.
  5. Power Transform.

Further Reading

This section provides more resources on the topic if you are looking to go deeper.





In this post, you discovered how to perform and invert four common data transforms for time series data in machine learning.

Specifically, you learned:

  • How to transform and inverse the transform for four methods in Python.
  • Important considerations when using transforms on training and test datasets.
  • The suggested order for transforms when multiple operations are required on a dataset.

Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.

Develop Deep Learning models for Time Series Today!

Deep Learning for Time Series Forecasting

Develop Your Own Forecasting models in Minutes

…with just a few lines of python code

Discover how in my new Ebook:
Deep Learning for Time Series Forecasting

It provides self-study tutorials on topics like: CNNs, LSTMs,
Multivariate Forecasting, Multi-Step Forecasting and much more…

Finally Bring Deep Learning to your Time Series Forecasting Projects

Skip the Academics. Just Results.

Click to learn more.

30 Responses to 4 Common Machine Learning Data Transforms for Time Series Forecasting

  1. Grace Wang September 13, 2018 at 7:33 pm #

    Thank you so much. I want to know how to use Yeo-Johnson transform, because my data contains negative values.

    • Jason Brownlee September 14, 2018 at 6:35 am #

      Sorry, I don’t have a worked example at this stage.

  2. mk December 29, 2018 at 1:18 pm #

    If I split own dataset into 6 parts by signal processing,each part use Normalization.How to make sure scale.

    • Jason Brownlee December 30, 2018 at 5:36 am #

      Perhaps prepare any data scaling on training data only.

  3. mk December 29, 2018 at 1:46 pm #

    If I have new datas out of train and test,how to deal with new datas as input for model.

    • Jason Brownlee December 30, 2018 at 5:36 am #

      Prepare new data in the same way as the training dat prior to using it as input for making a prediction.

      • mk December 31, 2018 at 11:40 pm #

        How to find my reply?I can‘t find my reply in the Website after shutting down Browser.

    • Firas August 8, 2019 at 11:25 am #

      use the sane aproach and coeffeciebt that you have already utlized it to pre-processing training set data

  4. Tharanga March 14, 2019 at 4:07 pm #

    dear sir,

    sir i used 2014-2019 data set to train a ML time series model.can i use that model for forecast new data set.

    thank you.

  5. Ahmed May 4, 2019 at 9:17 am #

    I am quite confused about PT and Standard Scaling. Power Transform will (or at least should) give the same result as Standard Scaling as the primary goal for both of them is to make data follow the gaussian distribution.

    What is the point of doing PT -> Standardization? Seems like they are essentially the same thing

    • Ahmed May 4, 2019 at 9:52 am #


      I figured it out. Thank you.

    • Jason Brownlee May 5, 2019 at 6:20 am #

      Not quite, power transform will shift the shape of the Gaussian, standardization will only force the data to a standard Gaussian – it could still be skewed.

  6. SamFlynn May 16, 2019 at 2:10 am #

    Hi Jason,
    Could trend differencing make results worse working with LSTM

    I difference and then standardize the data in one case and the other is only standardizing.



    • Jason Brownlee May 16, 2019 at 6:34 am #

      It may or may not help. Try modeling with and without differencing and compare results.

  7. ami May 24, 2019 at 5:55 pm #

    Hi Jason,
    I am confused about invert_difference.
    We apply difference (data, interval) which data=TRAINING SET. After fitting we have a model in “difference” form. So, we need to convert them to the original data for training, validation and test sets (CALCULATED BY MODEL) to compare with observed data. We can use
    invert_difference(orig_data, diff_data, interval) which
    diff_data are available but we don’t have orig_data for all three sets! In fact, we invert the diff to calculate the original data!
    Could you please explain it? Thanks

    • Jason Brownlee May 25, 2019 at 7:44 am #

      es, the differencing can be inverted on forecasts by propagating the inversion from training data through to test data, e.g. the last real observation from training will help to invert the first forecast observation.

  8. Leen June 26, 2019 at 4:22 am #

    Hello Jason,

    Regarding standardization, if we have X_train, y_train, X_test, y_test:

    1) if the problem is time series forecasting, and we have standardized X_train and X_test prior to fitting, should we inverse-standardize the training again prior to forecasting (after model.fit() and exactly before model.predict()) ? or should we inverse-standardize ONLY the forecasted values?

    2) should we consider standardizing y_train and y_test ? or is it useless ? I have been reading around some codes here and there on the internet, and I find examples of both.

    Your feedback is highly appreciated! Thanks in advance.

    • Jason Brownlee June 26, 2019 at 6:46 am #

      Scaling should be inverted on the predictions prior to estimating model performance.

      We don’t need the inputs and the choice to invert or not invert them does not effect model skill.

      If you’re unsure of whether to perform transform, then evaluate the model with and without it and compare skill.

  9. Gizo August 12, 2019 at 4:47 am #

    Thank you very much, Jason.

    I am using a multivariate data for multistep time series prediction using python.
    can i follow the same procedure as univariate? or use any other procedure?


    • Jason Brownlee August 12, 2019 at 6:40 am #

      Perhaps try and compare?

      If there is dependency between the variates, then modeling them together will result in better performance.

  10. Gizo August 12, 2019 at 3:57 pm #

    thanks for your reply

    i found an error on this code, (my data has 5 variables)

    series = Series.from_csv(‘sample_hourly_data.csv’, header=0)
    dataframe = DataFrame(series.values)
    dataframe.columns = [‘P2P’, ‘IM’, ‘VoIP’, ‘Streaming’, ‘SNS’]
    x = sqrt(dataframe.columns)

    ValueError: Length mismatch: Expected axis has 1 elements, new values have 5 elements

    • Jason Brownlee August 13, 2019 at 6:06 am #

      The error suggests the data and the columns in your example do not match.

  11. Elsa August 25, 2019 at 1:56 pm #

    Jason, do you have any recommendations for time series classification’s data preparation? Thank you.

    • Jason Brownlee August 26, 2019 at 6:09 am #

      Yes, I have a few posts on the topic. Perhaps compare results with and without scaling the inputs, and with and without making the inputs stationary.

      • Elsa August 26, 2019 at 10:08 am #

        Thank you for your advice, Jason.

Leave a Reply