Reader small image

You're reading from  Deep Learning for Time Series Cookbook

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781805129233
Edition1st Edition
Right arrow
Authors (2):
Vitor Cerqueira
Vitor Cerqueira
author image
Vitor Cerqueira

​Vitor Cerqueira is a time series researcher with an extensive background in machine learning. Vitor obtained his Ph.D. degree in Software Engineering from the University of Porto in 2019. He is currently a Post-Doctoral researcher in Dalhousie University, Halifax, developing machine learning methods for time series forecasting. Vitor has co-authored several scientific articles that have been published in multiple high-impact research venues.
Read more about Vitor Cerqueira

Luís Roque
Luís Roque
author image
Luís Roque

Luís Roque, is the Founder and Partner of ZAAI, a company focused on AI product development, consultancy, and investment in AI startups. He also serves as the Vice President of Data & AI at Marley Spoon, leading teams across data science, data analytics, data product, data engineering, machine learning operations, and platforms. In addition, he holds the position of AI Advisor at CableLabs, where he contributes to integrating the broadband industry with AI technologies. Luís is also a Ph.D. Researcher in AI at the University of Porto's AI&CS lab and oversees the Data Science Master's program at Nuclio Digital School in Barcelona. Previously, he co-founded HUUB, where he served as CEO until its acquisition by Maersk.
Read more about Luís Roque

View More author details
Right arrow

Analyzing correlation among pairs of variables

This recipe walks you through the process of using correlation to analyze a multivariate time series. This task is useful to understand the relationship among the different variables in the series and thereby understand its dynamics.

Getting ready

A common way to analyze the dynamics of multiple variables is by computing the correlation of each pair. You can use this information to perform feature selection. For example, when pairs of variables are highly correlated, you may want to keep only one of them.

How to do it…

First, we compute the correlation among each pair of variables:

corr_matrix = data_daily.corr(method='pearson')

We can visualize the results using a heatmap from the seaborn library:

import seaborn as sns
import matplotlib.pyplot as plt
sns.heatmap(data=corr_matrix,
            cmap=sns.diverging_palette(230, 20, as_cmap=True),
            xticklabels=data_daily.columns,
            yticklabels=data_daily.columns,
            center=0,
            square=True,
            linewidths=.5,
            cbar_kws={"shrink": .5})
plt.xticks(rotation=30)

Heatmaps are a common way of visualizing matrices. We pick a diverging color set from sns.diverging_palette to distinguish between negative correlation (blue) and positive correlation (red).

How it works…

The following figure shows the heatmap with the correlation results:

Figure 1.7: Correlation matrix for a multivariate time series

Figure 1.7: Correlation matrix for a multivariate time series

The corr() method computes the correlation among each pair of variables in the data_daily object. In this case, we use the Pearson correlation with the method='pearson' argument. Kendall and Spearman are two common alternatives to the Pearson correlation.

Previous PageNext Chapter
You have been reading a chapter from
Deep Learning for Time Series Cookbook
Published in: Mar 2024Publisher: PacktISBN-13: 9781805129233
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Vitor Cerqueira

​Vitor Cerqueira is a time series researcher with an extensive background in machine learning. Vitor obtained his Ph.D. degree in Software Engineering from the University of Porto in 2019. He is currently a Post-Doctoral researcher in Dalhousie University, Halifax, developing machine learning methods for time series forecasting. Vitor has co-authored several scientific articles that have been published in multiple high-impact research venues.
Read more about Vitor Cerqueira

author image
Luís Roque

Luís Roque, is the Founder and Partner of ZAAI, a company focused on AI product development, consultancy, and investment in AI startups. He also serves as the Vice President of Data & AI at Marley Spoon, leading teams across data science, data analytics, data product, data engineering, machine learning operations, and platforms. In addition, he holds the position of AI Advisor at CableLabs, where he contributes to integrating the broadband industry with AI technologies. Luís is also a Ph.D. Researcher in AI at the University of Porto's AI&CS lab and oversees the Data Science Master's program at Nuclio Digital School in Barcelona. Previously, he co-founded HUUB, where he served as CEO until its acquisition by Maersk.
Read more about Luís Roque