EDA
Intermediate 14 min read EDA

Bivariate Analysis

The relationships that become features

Pairing variables reveals relationships: scatter/hexbins for two numerics, box/violin for numeric-by-category, correlation and crosstabs for categorical pairs — the raw material of feature ideas.

scattercorrelationcrosstabboxplot by grouphexbinjointplot
Prerequisites: Univariate Analysis
Before we start

What you will learn

  • Choose the right pair plot per variable types
  • Read correlation with a scatter's eyes
  • Build crosstabs for categorical relations
Introduction

What is this?

Bivariate analysis pairs two columns. Numeric×numeric: scatter, hexbin for density, and the Pearson correlation. Numeric×categorical: box/violin per group, group means + error bars. Categorical×categorical: crosstabs and stacked bars. Each pairing produces signals that become modeling hypotheses.

Motivation

Why do you need it?

  • Scatter reveals non-linearity correlation scores will hide (r=0 for a U-shape).
  • Group differences by a categorical prompt encoding/grouping decisions.
  • Crosstabs expose interactions you'd never see univariately.
  • EDA here is where the 'interesting feature' lightbulb usually goes on.
Think about it

Mental model

Imagine

A dance partner pairing audit

Think of

Bivariate analysis

You dance with one partner to feel the chemistry. A numeric pair's chemistry is correlation + scatter; the categorical-box tells you how each group moves; the crosstab shows who shows up together in clusters.

Mechanics

How it works

  • Two numerics: sns.scatterplot(x, y, alpha=...) or sns.hexbin; compute df[['a','b']].corr().
  • Numeric × categorical: sns.boxplot(x='group', y='value') + group means.
  • Categorical × categorical: pd.crosstab(a, b, normalize='index') and % row shares.
  • sns.pairplot(df) renders the whole correlation grid at once for exploration.
  • Beware: a nonlinear U-relationship hides at correlation 0 — always eyeball the scatter.

Pair chart selection.

num × num
num × cat
cat × cat
many × many
Hypothesis
Hands-on

Examples

The three pairings

The three pairings
import pandas as pd, numpy as np
rng = np.random.default_rng(3)
x = rng.normal(size=2000)
y = x ** 2 + 0.1 * rng.normal(size=2000) # U-shape
print(pd.Series(x).corr(pd.Series(y))) # ~0 — hides the curve
df = pd.DataFrame({'group': rng.choice(['a','b'], 2000), 'v': rng.normal(size=2000)})
print(df.groupby('group')['v'].mean())
print(pd.crosstab(df['group'], rng.choice(['x','y'], 2000), normalize='index'))

The U-shape returns correlation ≈ 0 — a scatter would have shown it instantly. Box-per-group and normalized crosstab round out the picture for the other pair types.

Real world

In production

  • A pricing analyst scatters price vs purchases, spotting a quadratic sweet spot that r≈0 hid.
  • A churn team box-plots tenure by churn class, prompting a 'early-tenure' binary feature.
  • A recommender's crosstab of view×purchase by category reveals a cross-sell feature.
Guidelines

Best practices

  • Always pair corr() with a scatter — correlation without shape is blind.
  • Normalize crosstabs ('index') before comparing categories of different sizes.
  • Add alpha to scatters; a sea of black dots hides density.
Watch out

Common mistakes

  • Trusting a single correlation number for non-linear relations.
  • Imputing causation because a crosstab showed association.
  • Zero-variance hue columns producing empty groups in plots.
When things break

Debugging tips

  • correlation 0 in a plot that looks strongly curved? Compute x² as a feature and re-measure.
  • Group means look equal but distributions differ wildly → check the box plot, not the mean.
  • A crosstab with tiny cells → collapse rare categories or pooled estimates.
Interviews

Interview checkpoint

Practice

Hands-on lab

Pair Everything
30 min · intermediate
In progress
Objectives
  • Scatter + correlation on nonlinear data
  • Box-plot numeric by categorical groups
  • Normalized crosstabs for categorical pairs

Pair Everything

30 min · intermediate

0/5 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

Pearson correlation of a perfect U-shaped relationship is typically:

2

pd.crosstab(a, b, normalize='index') reports row proportions, enabling group comparison.

Go deeper

Official documentation