How to decompose a time series?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

# How to decompose a time series?

This recipe helps you decompose a time series

## Recipe Objective

Time series decomposition involves thinking of a series as a combination of level, trend, seasonality, and noise components.

So this recipe is a short example on how to decompose a time series. Let's get started.

## Step 1 - Import the library

``` import numpy as np import pandas as pd import matplotlib.pyplot as plt from statsmodels.tsa.seasonal import seasonal_decompose ```

Let's pause and look at these imports. Numpy and pandas are general ones. Here matplotlib.pyplot will help us in plotting. statsmodels.tsa.seasonal comes handy while analysing patterns.

## Step 2 - Setup the Data

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

Here, we have used one time series data from github. Also, we have set our index to date.

## Step 3 - Understanding Trends

``` result = seasonal_decompose(df, model='additive') ```

We have simply created an object of seasonal_decompose to understand our model with additive feature.

## Step 4 - Printing results

``` print(result.trend) print(result.seasonal) print(result.resid) print(result.observed) ```

We are priting the decomposition of our dataset in here

## Step 5 - Visualizing

``` result.plot() plt.show() ```

Finally, we have tried to visualize trends in one go.

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

##### Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

##### Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

##### Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

##### German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

##### Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

##### NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

##### Census Income Data Set Project - Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based on census data.

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

##### Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

##### Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.