Time Series Forecasting with Python 7-Day Mini-Course

Last Updated on

From Developer to Time Series Forecaster in 7 Days.

Python is one of the fastest-growing platforms for applied machine learning.

In this mini-course, you will discover how you can get started, build accurate models and confidently complete predictive modeling time series forecasting projects using Python in 7 days.

This is a big and important post. You might want to bookmark it.

Discover how to prepare and visualize time series data and develop autoregressive forecasting models in my new book, with 28 step-by-step tutorials, and full python code.

Let’s get started.

  • Updated Apr/2019: Updated the links to datasets.
  • Updated Aug/2019: Updated data loading to use new API.

Time Series Forecasting with Python 7-Day Mini-Course
Photo by Raquel M, some rights reserved.

Who Is This Mini-Course For?

Before we get started, let’s make sure you are in the right place.

The list below provides some general guidelines as to who this course was designed for.

Don’t panic if you don’t match these points exactly, you might just need to brush up in one area or another to keep up.

  • You’re a Developer: This is a course for developers. You are a developer of some sort. You know how to read and write code. You know how to develop and debug a program.
  • You know Python: This is a course for Python people. You know the Python programming language, or you’re a skilled enough developer that you can pick it up as you go along.
  • You know some Machine Learning: This is a course for novice machine learning practitioners. You know some basic practical machine learning, or you can figure it out quickly.

This mini-course is neither a textbook on Python or a textbook on time series forecasting.

It will take you from a developer that knows a little machine learning to a developer who can get time series forecasting results using the Python ecosystem, the rising platform for professional machine learning.

Note: This mini-course assumes you have a working Python 2 or 3 SciPy environment with at least NumPy, Pandas, scikit-learn and statsmodels installed.

Mini-Course Overview

This mini-course is broken down into 7 lessons.

You could complete one lesson per day (recommended) or complete all of the lessons in one day (hardcore). It really depends on the time you have available and your level of enthusiasm.

Below are 7 lessons that will get you started and productive with machine learning in Python:

  • Lesson 01: Time Series as Supervised Learning.
  • Lesson 02: Load Time Series Data.
  • Lesson 03: Data Visualization.
  • Lesson 04: Persistence Forecast Model.
  • Lesson 05: Autoregressive Forecast Model.
  • Lesson 06: ARIMA Forecast Model.
  • Lesson 07: Hello World End-to-End Project.

Each lesson could take you 60 seconds or up to 30 minutes. Take your time and complete the lessons at your own pace. Ask questions and even post results in the comments below.

The lessons expect you to go off and find out how to do things. I will give you hints, but part of the point of each lesson is to force you to learn where to go to look for help on and about the Python platform for time series (hint, I have all of the answers directly on this blog, use the search feature).

I do provide more help in the early lessons because I want you to build up some confidence and inertia.

Post your results in the comments, I’ll cheer you on!

Hang in there, don’t give up.

Stop learning Time Series Forecasting the slow way!

Take my free 7-day email course and discover how to get started (with sample code).

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

Start Your FREE Mini-Course Now!

Lesson 01: Time Series as Supervised Learning

Time series problems are different to traditional prediction problems.

The addition of time adds an order to observations that both must be preserved and can provide additional information for learning algorithms.

A time series dataset may look like the following:

We can reframe this data as a supervised learning problem with inputs and outputs to be predicted. For example:

You can see that the reframing means we have to discard some rows with missing data.

Once it is reframed, we can then apply all of our favorite learning algorithms like k-Nearest Neighbors and Random Forest.

For more help, see the post:

Lesson 02: Load Time Series Data

Before you can develop forecast models, you must load and work with your time series data.

Pandas provides tools to load data in CSV format.

In this lesson, you will download a standard time series dataset, load it in Pandas and explore it.

Download the daily female births dataset in CSV format and save it with the filename “daily-births.csv“.

You can load a time series dataset as a Pandas Series and specify the header row at line zero, as follows:

Get used to exploring loaded time series data in Python:

  • Print the first few rows using the head() function.
  • Print the dimensions of the dataset using the size attribute.
  • Query the dataset using a date-time string.
  • Print summary statistics of the observations.

For more help, see the post:

Lesson 03: Data Visualization

Data visualization is a big part of time series forecasting.

Line plots of observations over time are popular, but there is a suite of other plots that you can use to learn more about your problem.

In this lesson, you must download a standard time series dataset and create 6 different types of plots.

Download the monthly shampoo sales dataset in CSV format and save it with the filename “shampoo-sales.csv“.

Now create the following 6 types of plots:

  1. Line Plots.
  2. Histograms and Density Plots.
  3. Box and Whisker Plots by year or quarter.
  4. Heat Maps.
  5. Lag Plots or Scatter Plots.
  6. Autocorrelation Plots.

Below is an example of a simple line plot to get you started:

For more help, see the post:

Lesson 04: Persistence Forecast Model

It is important to establish a baseline forecast.

The simplest forecast you can make is to use the current observation (t) to predict the observation at the next time step (t+1).

This is called the naive forecast or the persistence forecast and may be the best possible model on some time series forecast problems.

In this lesson, you will make a persistence forecast for a standard time series forecast problem.

Download the daily female births dataset in CSV format and save it with the filename “daily-births.csv“.

You can implement the persistence forecast as a single line function, as follows:

Write code to load the dataset and use the persistence forecast to make a prediction for each time step in the dataset. Note, that you will not be able to make a forecast for the first time step in the dataset as there is no previous observation to use.

Store all of the predictions in a list. You can calculate a Root Mean Squared Error (RMSE) for the predictions compared to the actual observations as follows:

For more help, see the post:

Lesson 05: Autoregressive Forecast Model

Autoregression means developing a linear model that uses observations at previous time steps to predict observations at future time step (“auto” means self in ancient Greek).

Autoregression is a quick and powerful time series forecasting method.

The statsmodels Python library provides the autoregression model in the AR class.

In this lesson, you will develop an autoregressive forecast model for a standard time series dataset.

Download the monthly shampoo sales dataset in CSV format and save it with the filename “shampoo-sales.csv“.

You can fit an AR model as follows:

You can predict the next out of sample observation with a fit AR model as follows:

You may want to experiment by fitting the model on half of the dataset and predicting one or more of the second half of the series, then compare the predictions to the actual observations.

For more help, see the post:

Lesson 06: ARIMA Forecast Model

The ARIMA is a classical linear model for time series forecasting.

It combines the autoregressive model (AR), differencing to remove trends and seasonality, called integrated (I) and the moving average model (MA) which is an old name given to a model that forecasts the error, used to correct predictions.

The statsmodels Python library provides the ARIMA class.

In this lesson, you will develop an ARIMA model for a standard time series dataset.

Download the monthly shampoo sales dataset in CSV format and save it with the filename “shampoo-sales.csv“.

The ARIMA class requires an order(p,d,q) that is comprised of three arguments p, d and q for the AR lags, number of differences and MA lags.

You can fit an ARIMA model as follows:

You can make a one-step out-of-sample forecast for a fit ARIMA model as follows:

The shampoo dataset has a trend so I’d recommend a d value of 1. Experiment with different p and q values and evaluate the predictions from resulting models.

For more help, see the post:

Lesson 07: Hello World End-to-End Project

You now have the tools to work through a time series problem and develop a simple forecast model.

In this lesson, you will use the skills learned from all of the prior lessons to work through a new time series forecasting problem.

Download the monthy car sales dataset in CSV format and save it with the filename “monthly-car-sales.csv“.

Split the data, perhaps extract the last 1 or 2 years to a separate file. Work through the problem and develop forecasts for the missing data, including:

  1. Load and explore the dataset.
  2. Visualize the dataset.
  3. Develop a persistence model.
  4. Develop an autoregressive model.
  5. Develop an ARIMA model.
  6. Visualize forecasts and summarize forecast error.

For an example of working through a project, see the post:

The End!
(Look How Far You Have Come)

You made it. Well done!

Take a moment and look back at how far you have come.

You discovered:

  • How to frame a time series forecasting problem as supervised learning.
  • How to load and explore time series data with Pandas.
  • How to plot and visualize time series data a number of different ways.
  • How to develop a naive forecast called the persistence model as a baseline.
  • How to develop an autoregressive forecast model using lagged observations.
  • How to develop an ARIMA model including autoregression, integration and moving average elements.
  • How to pull all of these elements together into an end-to-end project.

Don’t make light of this, you have come a long way in a short amount of time.

This is just the beginning of your time series forecasting journey with Python. Keep practicing and developing your skills.


How Did You Go With The Mini-Course?
Did you enjoy this mini-course?

Do you have any questions? Were there any sticking points?
Let me know. Leave a comment below.

Want to Develop Time Series Forecasts with Python?

Introduction to Time Series Forecasting With Python

Develop Your Own Forecasts in Minutes

...with just a few lines of python code

Discover how in my new Ebook:
Introduction to Time Series Forecasting With Python

It covers self-study tutorials and end-to-end projects on topics like:
Loading data, visualization, modeling, algorithm tuning, and much more...

Finally Bring Time Series Forecasting to
Your Own Projects

Skip the Academics. Just Results.

Click to learn more.

13 Responses to Time Series Forecasting with Python 7-Day Mini-Course

  1. Luca May 5, 2017 at 2:29 am #


    Thanks for so many articles in your blog. Really appreciate.

    I have a question that I see sometimes we use a fixed-parameter model (e.g. parameters in ARIMA model is always fixed), while other times use an iterative way to determine the model parameters in each iteration of a test data sample. Are there any differences or reasons behind that? and when fixed model is useful and when to use an iterative way?

    my understanding from the examples are: iterative way of modeling ARIMA seems more appropriate to seasonal and trending dataset, right?

    Thanks a lot

    • Jason Brownlee May 5, 2017 at 7:32 am #

      In general, I would suggest evaluating a suite of different models for a problem and see what works best.

  2. Gururaj August 13, 2017 at 12:21 pm #

    Thanks Jason for these helpful articles. I have a general question. When we have a very high number of sensors, are there simpler methods to model them over building a model for each sensor using its own time series data?

    If we have some intuition that we may find groups of sensors which may exhibit similar behaviour, is there a method to cluster them and validate, given the individual time series data?

    • Jason Brownlee August 14, 2017 at 6:23 am #

      Good question Gururaj, sorry I have not worked on consolidating sensor data, I can’t give you expert advice.

    • Aiswariya August 28, 2019 at 5:26 am #

      These articles are very helpful… I like to know whether it is possible to forecast the time(dependent) variable with date as independent variable. For example with the past data of my arrival at the office can I predict at what time I will be able to mark the attendance tomorrow ?

      • Jason Brownlee August 28, 2019 at 6:43 am #

        Date is not needed, we work from the variable directly with univariate forecasting.

  3. joseph September 10, 2017 at 4:02 pm #

    Thanks for the course.
    I intend on doing the course
    I would like to know:
    do you have anomaly detection course?
    are hidden markov models and recurrent nn fit this area(time series)?

    • Jason Brownlee September 11, 2017 at 12:05 pm #

      Not at this stage, perhaps in the future.

      • Prashant Gupta June 5, 2018 at 4:16 pm #

        Yes, LSTM (a type of rnn) has been using for a while for time series problems

  4. Johnny Castro September 25, 2018 at 8:20 pm #

    Hi Jason, I am really enjoying the course. You make it easy to learn ML faster than via other curricula.
    I wanted to ask where/if you have the answers to these lessons (using the same datsets).
    Specifically I am having trouble with “Lesson 03” (box plots and on) in grouping by year and quarter the data from “shampoo sales”.
    The function “TimeGrouper” has been deprecated. I am using “Grouper” but have been unable to replicate results.

  5. Mia Cloe September 27, 2018 at 9:04 pm #

    Hello. Thank you for creating this time series mini-course, I am learning a lot of things.
    One thing that I’m wondering is that how hard it is if someone tries to code ARIMA computation without using APIs such as statsmodel. Do people usually use APIs in time series forecasting?

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

      Yes, people usually use APIs. Coding from scratch is only a good idea if you want to learn how it works in more detail or you have special operational requirements.

Leave a Reply