Module 4: Exploratory Data Analysis (EDA)
Module 4: Exploratory Data Analysis (EDA)
Introduction to Data Science โ Week 4
Understanding your data through summary statistics and visualization
Module Overview
Exploratory Data Analysis (EDA) is the process of investigating a dataset to discover
patterns, spot anomalies, test hypotheses, and check assumptions. It's the bridge between
cleaning data and modeling it. This module covers the core tools of EDA: descriptive
statistics, distributions, correlations, and grouping โ all practiced hands-on with pandas
and matplotlib.
Learning Objectives
By the end of this module, students will be able to:
- Explain the purpose and process of exploratory data analysis.
- Compute and interpret measures of center (mean, median, mode).
- Compute and interpret measures of spread (variance, standard deviation, IQR).
- Visualize distributions and identify skewness and outliers.
- Compute and interpret correlations, while avoiding causal claims.
- Use pandas
groupby and aggregation to compare subgroups.
Lesson 1: What Is EDA and Why Do It?
Exploratory Data Analysis (EDA) is an approach to analyzing data
that emphasizes open-ended investigation: looking at the data from many angles, checking
assumptions, and letting the data suggest hypotheses. It was popularized by statistician
John Tukey in the 1970s.
๐ต๏ธ Think of it like detective work: After collecting and cleaning your
clues (Modules 2 and 3), EDA is where you lay them all out on the table, look for patterns,
and form hypotheses. It's exploration, not confirmation.
What EDA Helps You Do
- Understand the shape and distribution of each variable.
- Spot outliers and data quality issues you missed during cleaning.
- Identify relationships between variables.
- Check assumptions before applying statistical tests or models.
- Form better questions for deeper analysis.
๐ก Key Idea: EDA is iterative. You'll go back and forth between
summary statistics, visualizations, and subgroup analyses โ each step informing the next.
Lesson 2: Measures of Center
Measures of center describe the "typical" value of a variable.
| Measure |
How It's Calculated |
Best For |
Weakness |
| Mean |
Sum of all values รท number of values |
Symmetric data with no outliers |
Sensitive to outliers |
| Median |
The middle value when sorted |
Skewed data or data with outliers |
Ignores the actual values of most data points |
| Mode |
The most frequent value |
Categorical data or discrete values |
Can be multiple modes; unstable in small samples |
Example: The Effect of an Outlier
Consider these incomes (in thousands): 30, 35, 40, 45, 50, 500
- Mean = 116.7 (misleading!)
- Median = 42.5 (more representative)
โ๏ธ Think of it like a seesaw: The mean is the balance point. A very heavy
person (an outlier) will pull the balance point toward them โ even though most people are on
the other side. The median stays near the middle because it only cares about position, not
magnitude.
๐ก Key Idea: If mean and median are close, the data is roughly symmetric.
If they're far apart, the data is skewed โ and the median is usually more trustworthy.
Lesson 3: Measures of Spread
Measures of spread describe how spread out the values are.
| Measure |
How It's Calculated |
Best For |
Weakness |
| Range |
Max โ Min |
Quick rough estimate |
Extremely sensitive to outliers |
| Variance |
Average squared distance from the mean |
Statistical formulas |
Units are squared, so hard to interpret |
| Standard deviation |
Square root of the variance |
Same units as the data; interpretable |
Sensitive to outliers |
| IQR (interquartile range) |
Q3 โ Q1 (75th percentile โ 25th percentile) |
Robust measure of spread |
Ignores extreme values (which can be a strength) |
๐ฏ Think of it like a dartboard: Small standard deviation = darts clustered
near the bullseye. Large standard deviation = darts scattered across the board. Same average
target โ very different throwers!
โ ๏ธ Watch Out! A standard deviation of 0 means all values are identical.
A standard deviation equal to (or larger than) the mean often signals highly skewed data
or outliers.
Lesson 4: Distributions and Outliers
Shapes of Distributions
- Symmetric: Left and right halves look roughly the same (e.g., heights in a population).
- Skewed right: Long tail on the right (e.g., income, house prices).
- Skewed left: Long tail on the left (e.g., age at retirement).
- Bimodal: Two peaks (e.g., heights of men and women combined).
Detecting Outliers
Two common rules of thumb:
- IQR method: Any value below Q1 โ 1.5รIQR or above Q3 + 1.5รIQR is often considered an outlier.
- Z-score method: Any value more than 3 standard deviations from the mean.
๐ก Key Idea: Outliers are not automatically errors. Investigate before
removing. Some of the most valuable insights come from understanding why a value is unusual.
Lesson 5: Correlation
Correlation measures the strength and direction of a linear
relationship between two variables. It's usually expressed as r, ranging
from โ1 to +1:
| Value of r |
Interpretation |
| +1 |
Perfect positive relationship |
| +0.5 to +0.7 |
Moderate to strong positive relationship |
| โ0.3 to +0.3 |
Weak or no linear relationship |
| โ0.5 to โ0.7 |
Moderate to strong negative relationship |
| โ1 |
Perfect negative relationship |
โ ๏ธ The Golden Rule: Correlation does not imply causation.
Ice cream sales and drowning rates are correlated (both rise in summer), but ice cream does
not cause drowning. The hidden cause is temperature.
When Correlation Is Misleading
- Nonlinear relationships: r only measures linear relationships. A U-shaped pattern can have r โ 0.
- Outliers: A single extreme point can dramatically change r.
- Simpson's paradox: A trend can reverse when you group the data differently.
๐ Think of it like umbrellas: When it rains, more people carry umbrellas.
Does carrying an umbrella cause rain? No! The rain causes both. That's correlation
without causation.
Lesson 6: Grouping and Aggregation
A powerful EDA technique is comparing subgroups of your data. The pandas groupby
operation makes this easy.
Example: Sales by Region
import pandas as pd
# Load cleaned data
data = pd.read_csv("clean_sales.csv")
# Average sales by region
data.groupby("region")["sales"].mean()
# Multiple statistics at once
data.groupby("region")["sales"].agg(["mean", "median", "std", "count"])
region
North 1250.4
South 980.2
East 1105.8
West 1320.1
Name: sales, dtype: float64
Cross-Tabulation (for categorical variables)
# Count of purchases by gender and product category
pd.crosstab(data["gender"], data["product_category"])
๐ก Key Idea: Grouping often reveals patterns hidden in the overall data.
A variable that looks uncorrelated overall may show a strong relationship within groups โ or
vice versa.
Lesson 7: Doing EDA in Practice
A simple EDA workflow:
- Inspect:
data.shape, data.info(), data.head().
- Summarize:
data.describe() for numeric columns.
- Check distributions: Histograms for each numeric column.
- Check categorical variables: Value counts and bar charts.
- Look for relationships: Scatter plots and correlation matrices.
- Investigate outliers: Box plots and IQR calculations.
- Group and compare: Use
groupby to find subgroup patterns.
- Document findings: Write down what you noticed, what surprised you, and what questions remain.
๐ก Key Idea: There's no single "correct" EDA path. The goal is to understand
the data well enough to choose the right next step โ whether that's a statistical test, a
model, or a better question.
Hands-On Lab: EDA on a Real Dataset
Goal: Practice EDA techniques on a cleaned dataset.
Instructions
- Use a cleaned dataset (your own from Module 3, or a well-known one like the Titanic dataset).
- Open it in a Jupyter Notebook or Google Colab.
- Perform a full EDA:
- Summary statistics for all numeric columns.
- Value counts for all categorical columns.
- Histograms for each numeric column โ describe the shape (symmetric, skewed, bimodal).
- Box plots to identify outliers.
- A correlation matrix for numeric variables โ interpret at least two correlations.
- At least two
groupby analyses comparing subgroups.
- Write a short EDA report (1โ2 pages) including:
- Key findings from your summaries.
- Any surprising patterns or anomalies.
- Any correlations you found โ and whether they might be causal.
- At least three follow-up questions you'd want to investigate.
Submission
Your Jupyter Notebook (with code and charts) + a 1โ2 page EDA report.
Assessment for Module 4
- Quiz (10 questions): Covers measures of center and spread, distributions, correlation, and grouping.
- Lab: Graded on completeness, correctness of interpretations, and quality of the EDA report.
- Participation: Contribution to the group activity and discussion prompts.
Key Terms
- EDA โ exploratory data analysis.
- Mean โ the arithmetic average.
- Median โ the middle value when data is sorted.
- Mode โ the most frequent value.
- Variance โ average squared distance from the mean.
- Standard deviation โ the square root of variance.
- IQR โ interquartile range (Q3 โ Q1).
- Skewness โ the asymmetry of a distribution.
- Correlation โ the strength and direction of a linear relationship.
- Groupby โ a pandas operation to split data into groups and compute summary statistics.
- Crosstab โ a table showing counts across two categorical variables.
Discussion Prompts
- Why might a median be more useful than a mean when analyzing income data?
- What does it mean if the mean and median are very different?
- Can you think of a real-world example where correlation is confused with causation in the news?
- What are the dangers of removing outliers without investigating them first?
- What's one pattern you'd expect to see in a dataset about your own life (e.g., screen time, sleep, study hours)?
Further Reading
- John Tukey, Exploratory Data Analysis (the original book)
- Wes McKinney, Python for Data Analysis โ chapters on summary statistics and data aggregation
- pandas documentation โ "Group by: split-apply-combine"
- Any introductory statistics textbook โ chapters on descriptive statistics and correlation
- Kaggle Learn: "Data Visualization" and "Feature Engineering" micro-courses
๐ก Instructor Note: EDA is where students begin to develop data intuition
โ the ability to glance at statistics and charts and notice when something looks "off."
Emphasize that EDA is not about confirming what you already think; it's about letting the data
surprise you. Encourage students to share surprising findings from their own labs.