Last Updated on

Time series forecasting is an important area of machine learning that is often neglected.

It is important because there are so many prediction problems that involve a time component. These problems are neglected because it is this time component that makes time series problems more difficult to handle.

In this post, you will discover time series forecasting.

After reading this post, you will know:

- Standard definitions of time series, time series analysis, and time series forecasting.
- The important components to consider in time series data.
- Examples of time series to make your understanding concrete.

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.

## Time Series

A normal machine learning dataset is a collection of observations.

For example:

1 2 3 |
observation #1 observation #2 observation #3 |

Time does play a role in normal machine learning datasets.

Predictions are made for new data when the actual outcome may not be known until some future date. The future is being predicted, but all prior observations are almost always treated equally. Perhaps with some very minor temporal dynamics to overcome the idea of “*concept drift*” such as only using the last year of observations rather than all data available.

A time series dataset is different.

Time series adds an explicit order dependence between observations: a time dimension.

This additional dimension is both a constraint and a structure that provides a source of additional information.

A time series is a sequence of observations taken sequentially in time.

— Page 1, Time Series Analysis: Forecasting and Control.

For example:

1 2 3 |
Time #1, observation Time #2, observation Time #3, observation |

## Describing vs. Predicting

We have different goals depending on whether we are interested in understanding a dataset or making predictions.

Understanding a dataset, called *time series analysis*, can help to make better predictions, but is not required and can result in a large technical investment in time and expertise not directly aligned with the desired outcome, which is forecasting the future.

In descriptive modeling, or time series analysis, a time series is modeled to determine its components in terms of seasonal patterns, trends, relation to external factors, and the like. … In contrast, time series forecasting uses the information in a time series (perhaps with additional information) to forecast future values of that series

— Page 18-19, Practical Time Series Forecasting with R: A Hands-On Guide.

### Time Series Analysis

When using classical statistics, the primary concern is the analysis of time series.

Time series analysis involves developing models that best capture or describe an observed time series in order to understand the underlying causes. This field of study seeks the “*why*” behind a time series dataset.

This often involves making assumptions about the form of the data and decomposing the time series into constitution components.

The quality of a descriptive model is determined by how well it describes all available data and the interpretation it provides to better inform the problem domain.

The primary objective of time series analysis is to develop mathematical models that provide plausible descriptions from sample data

— Page 11, Time Series Analysis and Its Applications: With R Examples

### Time Series Forecasting

Making predictions about the future is called extrapolation in the classical statistical handling of time series data.

More modern fields focus on the topic and refer to it as time series forecasting.

Forecasting involves taking models fit on historical data and using them to predict future observations.

Descriptive models can borrow for the future (i.e. to smooth or remove noise), they only seek to best describe the data.

An important distinction in forecasting is that the future is completely unavailable and must only be estimated from what has already happened.

The purpose of time series analysis is generally twofold: to understand or model the stochastic mechanisms that gives rise to an observed series and to predict or forecast the future values of a series based on the history of that series

— Page 1, Time Series Analysis: With Applications in R.

The skill of a time series forecasting model is determined by its performance at predicting the future. This is often at the expense of being able to explain why a specific prediction was made, confidence intervals and even better understanding the underlying causes behind the problem.

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

## Components of Time Series

Time series analysis provides a body of techniques to better understand a dataset.

Perhaps the most useful of these is the decomposition of a time series into 4 constituent parts:

**Level**. The baseline value for the series if it were a straight line.**Trend**. The optional and often linear increasing or decreasing behavior of the series over time.**Seasonality**. The optional repeating patterns or cycles of behavior over time.**Noise**. The optional variability in the observations that cannot be explained by the model.

All time series have a level, most have noise, and the trend and seasonality are optional.

The main features of many time series are trends and seasonal variations … another important feature of most time series is that observations close together in time tend to be correlated (serially dependent)

— Page 2, Introductory Time Series with R

These constituent components can be thought to combine in some way to provide the observed time series. For example, they may be added together to form a model as follows:

1 |
y = level + trend + seasonality + noise |

Assumptions can be made about these components both in behavior and in how they are combined, which allows them to be modeled using traditional statistical methods.

These components may also be the most effective way to make predictions about future values, but not always.

In cases where these classical methods do not result in effective performance, these components may still be useful concepts, and even input to alternate methods.

## Concerns of Forecasting

When forecasting, it is important to understand your goal.

Use the Socratic method and ask lots of questions to help zoom in on the specifics of your predictive modeling problem. For example:

**How much data do you have available and are you able to gather it all together?**More data is often more helpful, offering greater opportunity for exploratory data analysis, model testing and tuning, and model fidelity.**What is the time horizon of predictions that is required? Short, medium or long term?**Shorter time horizons are often easier to predict with higher confidence.**Can forecasts be updated frequently over time or must they be made once and remain static?**Updating forecasts as new information becomes available often results in more accurate predictions.**At what temporal frequency are forecasts required?**Often forecasts can be made at a lower or higher frequencies, allowing you to harness down-sampling, and up-sampling of data, which in turn can offer benefits while modeling.

Time series data often requires cleaning, scaling, and even transformation.

For example:

**Frequency**. Perhaps data is provided at a frequency that is too high to model or is unevenly spaced through time requiring resampling for use in some models.**Outliers**. Perhaps there are corrupt or extreme outlier values that need to be identified and handled.**Missing**. Perhaps there are gaps or missing data that need to be interpolated or imputed.

Often time series problems are real-time, continually providing new opportunities for prediction. This adds an honesty to time series forecasting that quickly flushes out bad assumptions, errors in modeling and all the other ways that we may be able to fool ourselves.

## Examples of Time Series Forecasting

There is almost an endless supply of time series forecasting problems.

Below are 10 examples from a range of industries to make the notions of time series analysis and forecasting more concrete.

- Forecasting the corn yield in tons by state each year.
- Forecasting whether an EEG trace in seconds indicates a patient is having a seizure or not.
- Forecasting the closing price of a stock each day.
- Forecasting the birth rate at all hospitals in a city each year.
- Forecasting product sales in units sold each day for a store.
- Forecasting the number of passengers through a train station each day.
- Forecasting unemployment for a state each quarter.
- Forecasting utilization demand on a server each hour.
- Forecasting the size of the rabbit population in a state each breeding season.
- Forecasting the average price of gasoline in a city each day.

I expect that you will be able to relate one or more of these examples to your own time series forecasting problems that you would like to address.

## Summary

In this post, you discovered time series forecasting.

Specifically, you learned:

- About time series data and the difference between time series analysis and time series forecasting.
- The constituent components that a time series may be decomposed into when performing an analysis.
- Examples of time series forecasting problems to make these ideas concrete.

Do you have any questions about time series forecasting or about this post?

Ask your questions in the comments below.

Hi Jason,

Is there a paper for this – Forecasting utilization demand on a server each hour? Would you be able to link it here?

-Amit

There may be Amit, you can search here: http://scholar.google.com

dear my friend

i’ve to greatly thank @JasonBtownlee for his useful information and code.

here is my time series prediction by bi-LSTM code,

http://www.mathworks.com/matlabcentral/fileexchange/69506-time-series-prediction

best wishes

Abolfazl Nejatian

Well done!

Each line represents one customer – the first column contains unique customer

identifier and the second column contains indices of the day when customer have visited the

mall. The day with index 1 is a Monday (e. g. 7th is a Sunday, 8th is again a Monday). Indices

are within a range of 1 to 1001 (which is equal to 143 full weeks). The task is to predict the first

day of the next visit (week 144). For example, if customer will visit mall on Wednesday, then

prediction should be equal to 3:

0: Customer will not visit on the next week 1: Monday

2: Tuesday

3: Wednesday

4: Thursday 5: Friday

6: Saturday 7: Sunday

Can you plz give a soluation on python

You can get started with time series forecasting here:

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

Hi Sunil, have you got a solution to the above your problem.

“Time series forecasting is an important area of machine learning that is often neglected.”

Time series analysis is an area in itself. Many time series analysts will be offended by this sentence. No pun intended.

Maybe, but in the realm of machine learning time series is just a subset for analyzing and forecasting. If this were another type of blog, not machinelearningmastery, then maybe take offense.

Thanks for the feedback. Indeed Leo, time series is neglected when discussing machine learning. It is left out of most books.

Thank you for this post. Interesting read.

Can LSTMs be used for time series prediction? What performance could one expect relative to other neuron models and architecture? (e.g. RNN, CNN)

Yes, LSTMs can be used for time series, but they can be tricky to configure.

I plan to do some large studies on LSTMs vs CNN and other methods in time series soon.

Have you done anything on LSTMs vs CNN and other methods in time series…. please let me know… I am eagerly waiting for it.

Yes, I have found LSTMs to be poor at time series forecasting.

HI Jason,

Very good post!

I am new to your blog and just come to this post. Since this reply is 2 years old, may I ask do you still hold this view: I have found LSTMs to be poor at time series forecasting?

Why I am asking is that I am in wireless telecom industry. Our team wish to explore topics such to forecast the number of users in a base station [a network device that connects your cell phone to the backend], or forecast the data volume in Mega bytes in a base station.

So far LSTM is our favorite candidate. Seeing your this reply, I am wondering if our team is on the right track.

Looking forward to your reply.

Yes. Vanilla LSTMs are poor at time sreies forecasting. I have a ton of results to show this. More here:

https://machinelearningmastery.com/suitability-long-short-term-memory-networks-time-series-forecasting/

CNNs are often better. CNN+LSTM better again, and ConvLSTMs are very good. I cover these here:

https://machinelearningmastery.com/deep-learning-for-time-series-forecasting/

Hi, Jason,

I’m also a new visitor in your blog. Firstly I want to thank for your sharing. And I want to ask you a question about whether the CNN/RNN/LSTM can be used as trajectory predictions (especially the moving targets) ?

At last, can we think of the trajectory as a time series?

Looking forward to your reply.

I don’t see why not. Perhaps try modeling a time series framing of the problem and see how you go?

Nowadays I see that MARS (actually “earth” in R) is the strongest algorithm to forecast time series data.

Thanks for the tip Kleyn. I can see why it would be powerful, it’s a great method.

Hi Jason,

I was wondering whether to specialize in time series or machine learning. What do you think which of these has a brighter future for someone interested in research in one of these two fields?

Anthony

Sorry, I am not in touch with academia any longer.

There are a lot of problems where machine learning can be helpful in industry and some of those problems are time series.

Very nice article Jason. Do you think time series data analysis is as important as the machine learning problems in the industry?

Yes I do Johno, there are many problems with time series and unfortunately, many ML practitioners are unaware of standard methods like ARIMA.

Do you have an introductory tutorial on time series analysis?

Hi Raphael,

I have many posts on time series, you can see them all here:

http://machinelearningmastery.com/category/time-series/

Also, try the search feature at the top of the page.

Hi Jason,

Can you plz tell me from where I can find experiments like this “Forecasting the average price of gasoline in a city each day”?

Hi Pooja, I have not seen this specific example,

You could collect the data and create the models, perhaps starting with one city and one year of data, then scaling up from there.

Hello Jason,

How do I predict increase/decrease in gas price based on historical data of different years using forecasting in machine learning?

This post might help:

http://machinelearningmastery.com/work-time-series-forecast-project/

Hi Jason

I want to do a study to compare which one is better when it comes to forecasting between time series models and machine learning models.

How can you advise me to go about this study?

This project might help as a template:

http://machinelearningmastery.com/time-series-forecast-study-python-monthly-sales-french-champagne/

Hello Jason,

Do you have a tutorial on multi-input LSTM in keras?

There will be one on the blog soon.

Hello Jason,

I am a novice at this so forgive a potentially silly question.

If is have a set of time series data with an associated value

eg timestamp, value

where the value and possibly the time between certain values equates to an event, is it possible to use this knowledge to train the system so that when I run a model against an unprocessed data set I can detect the occurrence of the event in the second data set?

Hope this rambling makes sense.

Thank you

Nick

If I understand your problem description, then yes I believe so.

You will need to prepare your historical data with clear examples of event/no-event.

Thank you Jason, Would I need to use Analysis rather than forecasting ?

regards

Nick

You can call “better understanding your problem” analysis if you wish.

I just want to share a link about time series analysis which I found useful.

http://www.itl.nist.gov/div898/handbook/pmc/section4/pmc4.htm

Thanks for sharing Dhineshkumar.

hi,Jason:

I have read your post “What Is Time Series Forecasting?” 。I know little about Time Series Forecasting before. So, I am confused after read this post. I wonder what is the difference between Time Series Forecasting and other ML problems ,such as regression, classification?

The addition of a time dimension which imposes an order dependence between observations.

This dependence must be respected in the classification/regression formulation of the problem.

Ok, I got it. I can always get inspiration from you, thanks again, Jason！

No problem.

Hi Jason,

After reading and working through some of your tutorials I´m not sure what the most promising strategy could be to classify a known set of time series data. I have multi feature time series data being labeled or classified (2 possible states).

When getting the data of a new time step I would like to classify whether it is state one or two. My first idea was to use a solution as shown in your pima-diabetes-tutorial because it looks more like an classification problem to me.

After going on reading through the different tutorials I found a lot stuff concerning time series prediction/forecast and the analysis of a time range within the time series – but my point is not looking into the future.

I think that my problem relates to something in-between, doesn´t it? Is there a tutorial you would expect to fit the best to this question or is the solution for this question covered by the different way´s of predicting time series data?

Regards,

Oliver.

I would suggest modeling it as sequence classificaiton, not too much unlike classifying a sequence of words in a movie review as positive or negative.

Try MLPs and LSTMs.

Jason – In this book did you use LSTM for time-series-forecasting in any of the chapters?

I have to use NN for time-series forecasting. Will this book help ?

My time series book focuses on linear models such as AR and ARIMA. No LSTMs.

Hello Jason,

I am working as a supply chain supervisor and i have been given the task of reducing inventory levels at many warehouses. Until now I am just taking the avg sales of previous months and based on this i am forecasting the next month’s sales. I take this data as a basis to maintain inventory levels but this method is proving to be inaccurate. Which is the best time series forecasting method suitable to forecast sales? Can you please provide the procedure to implement this method.

No one can recommend a best method to you, it is unknown/intractable. You will need to discover what works best for your specific data.

I would recommend starting right here:

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

Hello Jason, I’m working on a project for predicting a specific tournament. I have data consists of date (date of match), home_team (constant data because I’m predicting a specific team, let’s say it’s “teamA” for now), away_team (the opponent of “teamA”), win (status of teamA’s win, 1 being a win and 0 being a loss). The data has holes because this “teamA” doesn’t always participate in every tournament. Is this still valid data for time series? or is there a better method of predicting?

I would recommend modeling the problem using a rating algorithm. The Elo algorithm is a good place to start:

https://en.wikipedia.org/wiki/Elo_rating_system

Hello Jason， I have some question for you. If I use pregnant weight sequence to predict fetal weight, weight of pregnant belongs to time series， What kind of time series problems do these time-series problems belong to? Thanks！

Perhaps this post will help:

https://machinelearningmastery.com/sequence-prediction/

Hello Jason, I’m working on a project for analysis EMG signals. I want ask that if I want to predict EMG signals, what kind of tools should be used(NAR or LSTM or something else)? Does deep neural network’s performance are better than normal neural network? Thanks

Perhaps start with an MLP and compare results to an LSTM.

Great article!

Judson Smith

Founder/CEO

Acclaimed Labs – Fully Automated Data Forecasting

Thanks.

Hi sir ,

i have some yearly time series data , to forecast coming years value ,and i m using neural network model. i m new learner in this feild

but the problem for me is how train the data such as how to convert into lags to make the ready appplying the model . i will be your thankful , waiting for your precious guidings

This tutorial will help prepare the lags:

https://machinelearningmastery.com/convert-time-series-supervised-learning-problem-python/

Hi Jason,

Thanks for the excellent post, I am working on a project and I’m not sure it I need time series analysis or not – I am predicting a customers likelihood to purchase, right now this prediction is as of ‘now’ based on data from the previous day – I want to refine this further by predicting this value over 3 days, or 10 days – what would be the normal approach to do this? Does this make it time series analysis, or not?

Thanks for your advice,

Ben

Perhaps brainstorm a few different ways to frame the problem, then prototype each to discover what works well with your specific dataset?

If anyone is interested – we’ve just launched an automated time series forecasting platform leveraging deep learning. You can learn about the platform here: https://www.oneclick.ai/solutions/time-series/ and sign up for a free test account to kick the tires if interested.

Thanks for sharing Michael.

Hi Jason,

Thanks for the very informative blog.

I recently started working on a problem, In which it collects some environment variables (temperature, humidity,noise,co2)from the sensors in a building, and tries to predict the occupancy(number of people), By co-modelling with the other environmental variables.

Based on all the values of the environmental variables and number of people, I am detecting the Anomalies if occurs any.

I am looking this problem also as an example of time series forecasting. Could you please clarify.

Thanks

Yes, it could be a time series classification or time series classification problem.

This post might help:

https://machinelearningmastery.com/how-to-predict-room-occupancy-based-on-environmental-factors/

Hi Jason,

I’ve a requirement of sales forecasting for a year to do it in R with machine learning . What is the best method of doing it. I’ve data of month wise sales with different dimensions (ex) region, customer & material wise . whether i have to pass data as a whole to train the model or combination of dimensions have to pass.

Kindly suggest me

This is a common question that I answer here:

https://machinelearningmastery.com/faq/single-faq/do-you-have-material-on-time-series-in-r

I have been asked to forecast user consumption of a good in a particular geographic area on a month to month basis. I have no ML background. Do you feel that this sort of future forecasting is different enough from something like the stock market to make using ML a reasonable approach?

Hi Nate, I have some suggestions here that might help:

https://machinelearningmastery.com/faq/single-faq/how-to-develop-forecast-models-for-multiple-sites

This might be a good place to get started:

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

Well I will give it a try but I don’t really see what is fundamentally different from trying to forecast consumption of a good vs trying to predict the stock market. I bring that up because you yourself feel that predicting the stock market is not a good use of your time and I don’t want to spend my time taking a new job if I am only going to spin my wheels. I guess the fact that you are linking means you find the problem to be of value?

I have no idea if your dataset is predictable, but you can find out really quickly if you fit some linear models and they out-perform a naive/persistence model.

E.g. you demonstrate that something is predictable, but if you fail you beat a naive forecast, you don’t demonstrate the inverse, that it’s not predictable. It’s simply not predictable yet, or not yet known to be unpredictable.

hi jason,

my question is time series prediction(problem) we can apply forecasting model. if my problem is time series classification ,can we apply forecasting model ? is that possible

Yes.

thanks a lot jason….is there any link for that

Yes, you can learn about time series classification here:

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

Excellent job, Jason!

Always simple and direct to the point.

I wonder if you have any material on trend estimation.

Kind regards,

Luis Fabiano

Thanks.

Yes, right here:

https://machinelearningmastery.com/time-series-trends-in-python/

Hi Jason,

Thanks for blog.

I want to find “Expected life time of a customer” in telecom.

What features help me to predict.

I have all transaction data (Voice,SMS,voucher,Data) and demographic details and audit trail details.

Could you tell me what date feature help me to find “Expected Life Time” of a customer … It will help me to calculate “CLV(customer Life Time value)”.

Perhaps try modeling it as a survival analysis?

https://en.wikipedia.org/wiki/Survival_analysis