Data Science

What, When & How of Scatterplot Matrix in Python

In this post, you will learn about some of the following in relation to scatterplot matrix. Note that scatter plot matrix can also be termed as pairplot. Later in this post, you would find Python code example in relation to using scatterplot matrix / pairplot (seaborn package).

  • What is scatterplot matrix?
  • When to use scatterplot matrix / pairplot?
  • How to use scatterplot matrix in Python?

What is Scatterplot Matrix?

Scatter plot matrix is a matrix (or grid) of scatter plots where each scatter plot in the grid is created between different combinations of variables. In other words, scatter plot matrix represents bi-variate or pairwise relationship between different combinations of variables while laying them in grid form. Here is a sample scatter plot matrix created using Sklearn Iris dataset.

Fig 1. Scatter plot matrix / pairplot for Sklearn Iris Dataset

Scatter plot matrix is also referred to as pair plot as it consists of scatter plots of different variables combined in pairs. In above matrix of scatter plots, pay attention to some of the following:

  • Diagonally from top left to right, the plots represent univariate distribution of data for the variable in that column.
  • Other plots represent the pairwise scatter plots between sepal length and petal length.

Here is another representation of pair plots comprising three different variables.

Fig 2. Pairwise relationships between three different variables in SKlearn IRIS datasets

When to use Scatterplot Matrix / Pairplot?

Scatterplot matrix can be used when you would like to assess some of the following:

  • Features correlation: Assess pairwise relationships between three or more variables. This is important to understand relationships between different features when building machine learning model
  • Multicollinearity: Assess the collinearity / multi-collinearity by analyzing the correlation between two or more variables. Recall that multi-collinearity can result in two or more predictor variables that might be providing the same information about the response variable thereby leading to unreliable coefficients of the predictor variables (especially for linear models).
  • Data is linearly separable?: Assess whether the data is linearly separable or not. The data which is linearly separable can be separated using a linear line. The data which isn’t linearly separable would need to be applied with kernel methods. Thus, it may help determine machine learning algorithm one would want to use.

One can analyse the pairwise relationship at several stages of machine learning model pipeline including some of the following:

  • Data analysis
  • Before and after feature transformations
  • Feature engineering
  • Feature selection

Scatterplot matrix Python Code Example

In this section, the usage of seaborn package’s pairplot method is represented. By default, the pairplot function creates a grid of Axes such that each numeric variable in data is shared in the y-axis across a single row and in the x-axis across a single column. Here is the sample code representing pairplot:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import datasets
#
# Load iris dataset
#
iris = datasets.load_iris()
#
# Create dataframe using IRIS dataset
#
df = pd.DataFrame(iris.data)
df.columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
df['class'] = iris.target
#
# Create pairplot of all the variables with hue set to class
#
sns.pairplot(df, hue='class')  
plt.show()
Fig 3. Scatter plot matrix / pairplot of all variables with hue parameter

Pay attention to the usage of hue parameter which is passed categorical variable and used to map plot aspects to different colors. It is also possible to show a subset of variables or plot different variables on the rows and columns. Usage of vars parameter helps plot only a subset of variables as shown in the code below. The plots in fig1 and fig 2 represents usage of subset of variables for pairplot.

sns.pairplot(df, hue='class', vars=['sepal_length', 'sepal_width', 'petal_length'])   
plt.show()

References

Conclusions

Here are some learning from this post:

  • Use scatter plot matrix or pairplot for assessing pairwise or bi-variate relationship between different predictor variables
  • Use scatter plot matrix or pairplot for analyzing the multicollinearity between predictor variables
  • Use scatter plot matrix or pairplot for assessing whether the data is linearly separable or otherwise.
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

3 weeks ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

4 weeks ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

1 month ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

1 month ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

1 month ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

1 month ago