# Pair Plots in Machine Learning

Harshit Ahluwalia 19 Mar, 2024

## Introduction

In the realm of data science, the initial step towards understanding and analyzing data involves a comprehensive exploratory data analysis (EDA). This process is pivotal for recognizing patterns, identifying anomalies, and establishing hypotheses. Among the myriad of tools available for EDA, pair plots stand out as a fundamental visualization technique that offers a multi-faceted view of the data. This article explores pair plots in machine learning and explains how to create them using Seaborn in Python. If you are confused about when to use which data visualization, then head on to this article.

## Definition of a Pair Plot and Its Purpose

A pair plot, also known as a scatterplot matrix, is a matrix of graphs that enables the visualization of the relationship between each pair of variables in a dataset. It combines both histogram and scatter plots, providing a unique overview of the dataset’s distributions and correlations. The primary purpose of a pair plot is to simplify the initial stages of data analysis by offering a comprehensive snapshot of potential relationships within the data.

## Importance of Pair Plots in Exploratory Data Analysis (EDA)

Pair plots play a crucial role in EDA by facilitating a quick, yet thorough, examination of how variables interact with each other. They enable data scientists to:

• Visualize distributions: Understand the distribution of single variables.
• Identify relationships: Observe linear or nonlinear relationships between variables.
• Detect anomalies: Spot outliers that may indicate errors or unique insights.

## Key Elements of a Pair Plot

At its core, a pair plot consists of:

• Histograms: Diagonal plots showing the distribution of a single variable.
• Scatter plots: Off-diagonal plots showing the relationship between two variables. These can reveal patterns, trends, and correlations.

These elements collectively provide a deep dive into the data, allowing for an immediate visual assessment of potential relationships.

## Feature Selection: Using Pair Plots to Identify Relevant Variables for Model Building

One of the most significant advantages of pair plots is their ability to aid in feature selection. By visually identifying variables that show strong relationships or distinct patterns, data scientists can prioritize these variables for model building. This not only enhances model accuracy but also optimizes computational efficiency by focusing on relevant features.

Pair plots are instrumental in uncovering:

• Trends: Linear or nonlinear relationships that suggest predictability.
• Clusters: Groups of data points that share similar characteristics, hinting at subpopulations within the dataset.
• Outliers: Data points that deviate significantly from other observations, which could be indicative of data entry errors or novel discoveries.
• Correlations: The strength and direction of relationships between variables.

## Create Your First Pair Plot

Creating a pair plot is straightforward with libraries such as Seaborn in Python. Here’s a simple guide:

Assigning a hue variable adds a semantic mapping and changes the default marginal plot to a layered kernel density estimate (KDE):

## Essentials Parameters of Seaborn Pairplot

Here are the most essential `seaborn.pairplot` parameters:

1. data: The dataset for plotting is structured as a pandas DataFrame where columns are variables and rows are observations.
2. hue: Categorical variable name in `data`. It colors data points differently based on the category, allowing for distinction between groups.
3. hue_order: The order of levels of the hue variable. It specifies the color order for the categorical distinction.
4. palette: Color palette for differentiating the levels of the hue variable. It determines the color scheme for plotting.
5. vars: List of variable names to plot. If not provided, all numeric columns are used.
6. x_vars, y_vars: Variables to be plotted on the x and y axes, respectively. Allows for specifying subsets of variables for plotting.
7. kind: Type of plot for off-diagonal elements. Common options include ‘scatter’ (default) and ‘reg’ (regression).
8. diag_kind: Plot type for the diagonal elements. ‘auto’ (default), ‘hist’ (histogram), or ‘KDE’. ‘None’ can be used to skip diagonal plotting.
9. markers: Marker styles for the scatterplot points are especially useful when the `hue` parameter is used. It can be a single marker format or a list specifying a different marker for each hue category.
10. height: Height (in inches) of each facet (plot) in the grid.
11. aspect: Aspect ratio of each facet, so that aspect * height equals the width of each facet in inches.
12. corner: If set to True, plots only the lower triangle of the pair grid, making the plot more concise.
13. dropna: Whether to drop missing values from the data before plotting. True removes missing values.
14. plot_kws: Dictionary of keyword arguments passed to the plotting function for the off-diagonal elements.
15. diag_kws: Dictionary of keyword arguments passed to the function used for diagonal elements.
16. grid_kws: Dictionary of keyword arguments passed to the `PairGrid` constructor, affecting the layout of the plots.
17. size: Deprecated; use `height` instead. It was previously used to set the height of the plots but has been replaced by the `height` parameter for consistency.

These parameters offer extensive customization for creating pair plots, enabling you to tailor the visualization precisely to your data analysis needs. Hope these definitions help you understand and apply Seaborn’s pair plotting capabilities effectively in Python.

Let’s do more modifications in the pair plot

We don’t want KDE plots. Is it possible to force marginal histograms? The answer is “YES”. Let’s see how to do it:

The `markers` parameter applies a style mapping on the off-diagonal axes. Currently, it will be redundant with the `hue` variable:

As with other figure-level functions, the size of the figure is controlled by setting the `height` of each individual subplot:

Set `corner=True` to plot only the lower triangle:

## Conclusion

Pair plots are a cornerstone in exploratory data analysis, providing a bird’s-eye view of the relationships within a dataset. By enabling quick identification of trends, clusters, and outliers, they serve as an invaluable tool for feature selection and hypothesis generation. Whether you’re a novice exploring data science or an experienced analyst, incorporating pair plots into your EDA toolkit can lead to more informed decisions and deeper insights. Moreover, creating pair plots for data visualization becomes very easy with Python libraries such as Seaborn. So go ahead, try them out, and let them reveal to you the narrative hidden within the data.

Harshit Ahluwalia 19 Mar, 2024