How to forecast using moving averages for time series?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

# How to forecast using moving averages for time series?

This recipe helps you forecast using moving averages for time series

## Recipe Objective

The moving average (MA) method models the next step in the sequence as a linear function of the residual errors from a mean process at prior time steps. It is quite helpful for such such datset while making predictions.

So this recipe is a short example on how to predict using moving averages. Let's get started.

## Step 1 - Import the library

``` import numpy as np import pandas as pd from statsmodels.tsa.arima_model import ARMA ```

Let's pause and look at these imports. Numpy and pandas are general ones. Here statsmodels.tsa.arima_model is used to import ARMA library for building of model.

## Step 2 - Setup the Data

``` df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/a10.csv', parse_dates=['date']) df.head() ```

Here, we have used one time series data from github.

## Step 3 - Splitting Data

``` train_data = df[1:len(df)-12] test_data = df[len(df)-12:] ```

Here we have simply split data into size of 12 and rest elements

## Step 4 - Building moving average model

``` model = ARMA(train_data.value, order=(0, 1)) model_fitted = model.fit() ```

We can use the ARMA class to create an MA model and setting a zeroth-order AR model. We must specify the order of the MA model in the order argument.

## Step 5 - Making Predictions

``` predictions = model.predict(start=len(train_data), end=len(train_data) + len(test_data)-1) print(predictions) ```

Here, we have printed the coeffiecient of model and the predicted values. Predict function simply let's us predicting train dataset.

## Step 6 - Lets look at our dataset now

Once we run the above code snippet, we will see:

```Scroll down the ipython file to visualize the output.
```

#### Relevant Projects

##### Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

##### Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

##### House Price Prediction Project using Machine Learning
Use the Zillow dataset to follow a test-driven approach and build a regression machine learning model to predict the price of the house based on other variables.

##### Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

##### Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

##### Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

##### Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

##### Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

##### RASA NLU chatbot creation
The project will use rasa NLU for the Intent classifier, spacy for entity tagging, and mongo dB as the DB. The project will incorporate slot filling and context management and will be supporting the following intent and entities. Intents : product_info | ask_price|cancel_order Entities : product_name|location|order id The project will demonstrate how to generate data on the fly, annotate using framework and how to process those for different pieces of training as discussed above .

##### Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.