How to decompose a time series?

How to decompose a time series?

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('', parse_dates=['date']).set_index('date')

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

Now our dataset is ready.

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


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

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.

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.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

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.

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.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

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.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.