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.

## 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.

## 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:

1 2 3 4 |
Time, Observation day1, obs1 day2, obs2 day3, obs3 |

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

1 2 3 4 5 |
Input, Output ?, obs1 obs1, obs2 obs2, obs3 obs3, ? |

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:

1 2 |
from pandas import read_csv series = read_csv('daily-births.csv', header=0, index_col=0) |

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:

- Line Plots.
- Histograms and Density Plots.
- Box and Whisker Plots by year or quarter.
- Heat Maps.
- Lag Plots or Scatter Plots.
- Autocorrelation Plots.

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

1 2 3 4 5 |
from pandas import read_csv from matplotlib import pyplot series = read_csv('shampoo-sales.csv', header=0, index_col=0) series.plot() pyplot.show() |

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:

1 2 3 |
# persistence model def model_persistence(x): return x |

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:

1 2 3 4 5 |
from sklearn.metrics import mean_squared_error from math import sqrt predictions = [] actual = series.values[1:] rmse = sqrt(mean_squared_error(actual, predictions)) |

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:

1 2 |
model = AR(dataset) model_fit = model.fit() |

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

1 |
prediction = model_fit.predict(start=len(dataset), end=len(dataset)) |

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:

1 2 |
model = ARIMA(dataset, order=(0,1,0)) model_fit = model.fit() |

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

1 |
outcome = model_fit.forecast()[0] |

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:

- Load and explore the dataset.
- Visualize the dataset.
- Develop a persistence model.
- Develop an autoregressive model.
- Develop an ARIMA model.
- 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.

## Summary

**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.

Hi

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

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

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?

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

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 ?

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

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)?

thanks

joseph

Not at this stage, perhaps in the future.

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

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.

I have blog posts on each, you can use the search or start here:

https://machinelearningmastery.com/start-here/#timeseries

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?

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.

This lessons are wonderful, thanks. I have a dude about a problem.

I have a 4 time series dataset.

1 dataset with data for hour. For last month

1 dataset with data for last 12 months. Data for day.

1 dataset with data per month. 3 last years.

How can i use this dataset?

Thanks.

What problem are you having exactly?

In this example, our dataset is dayN, obs.

In a real case, I have a few datasets like:

diary_dataset_last30days

day1, obs

day2, obs

monthly_dataset_actual_year

month1, obs

month2, obs

month3, obs

years_dataset_last5_years

year1, obs

year2, obs

year3, obs

I have data from days, hour too, week, years. What can i do to use this data for forecasting?

What do you think?

Perhaps prototype some models and see?

Hi Jason,

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

In this Lesson 03: Data Visualization, I am trying to practice plotting BoxPlot.

——–

But got this error : TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of ‘Index’

——–

While running the below code :

from pandas import DataFrame

from pandas import Grouper

series = pd.read_csv(‘shampoo-sales.csv’, header=0, index_col=0, parse_dates=True, squeeze=True)

groups = series.groupby(Grouper(freq=’M’))

years = DataFrame()

for name, group in groups:

years[name.year] = group.values

years.boxplot()

plt.show()

——–

Hope you can help. Thanks a lot.

Perhaps confirm that the dates/index was parsed correctly.