Before You Start
- Required knowledge
- Pandas filtering and aggregation; basic descriptive statistics.
- Libraries used
- Matplotlib, NumPy, pandas
- Dataset and source
- Embedded 20-row synthetic churn CSV, read with StringIO; includes missing values and a deliberately leaky column.
- Expected output
- A data-quality and EDA report covering distributions, missingness, class balance, slices, and leakage.
Lesson Videos
Exploratory Data Analysis (EDA) β Theory & Concepts
Read this before attempting the exercises. EDA is the first step in any ML project. These notes cover systematic approaches to understanding your data.
1. Why EDA Matters
Before building any model, you must understand your data:
- βDistribution of features (normal? skewed? bimodal?)
- βMissing values and their patterns (MCAR, MAR, MNAR)
- βOutliers that could distort models
- βCorrelations between features and the target
- βData quality issues (duplicates, inconsistent formats)
Interview Tip: At FAANG, the first question in an ML design interview is often "how would you explore the data?" A structured EDA approach shows maturity.
2. Univariate Analysis
Examine each feature individually:
# Numerical features
df['age'].describe() # count, mean, std, min, 25%, 50%, 75%, max
df['age'].hist(bins=50) # distribution shape
df['age'].skew() # >0 right-skewed, <0 left-skewed
# Categorical features
df['country'].value_counts() # frequency counts
df['country'].value_counts(normalize=True) # proportions
What to look for:
- βSkewness β may need log transform for linear models
- βOutliers β values beyond 3Ο or IQR Γ 1.5
- βZero-inflated β lots of zeros (common in revenue, counts)
- βCardinality β how many unique values (affects encoding strategy)
3. Bivariate Analysis
Examine relationships between pairs of features:
# Numerical vs Numerical
df[['age', 'revenue']].corr() # Pearson correlation
df.plot.scatter(x='age', y='revenue') # scatter plot
# Numerical vs Categorical
df.groupby('country')['revenue'].mean() # mean revenue by country
df.boxplot(column='revenue', by='country') # distribution comparison
# Categorical vs Categorical
pd.crosstab(df['country'], df['purchased']) # contingency table
FAANG Gotcha: Pearson correlation only measures linear relationships. Two features can have zero correlation but strong nonlinear dependence.
4. Missing Data Patterns
# Overview
df.isna().sum().sort_values(ascending=False)
# Missing data heatmap pattern
import seaborn as sns
sns.heatmap(df.isna(), cbar=False)
Types of missingness:
- βMCAR (Missing Completely At Random) β safe to drop
- βMAR (Missing At Random) β missingness depends on observed data
- βMNAR (Missing Not At Random) β missingness depends on the missing value itself
Interview Tip: If income is missing more often for low-income users, that's MNAR β dropping those rows biases your model.
5. Correlation Analysis
# Full correlation matrix
corr = df.select_dtypes(include='number').corr()
sns.heatmap(corr, annot=True, cmap='coolwarm', center=0)
What to look for:
- βHigh correlation between features (>0.8) β multicollinearity, may need to drop one
- βCorrelation with target β features with near-zero correlation may be useless
- βUnexpected correlations β data leakage red flag
6. Outlier Detection
# IQR method
Q1 = df['revenue'].quantile(0.25)
Q3 = df['revenue'].quantile(0.75)
IQR = Q3 - Q1
outliers = df[(df['revenue'] < Q1 - 1.5*IQR) | (df['revenue'] > Q3 + 1.5*IQR)]
# Z-score method
from scipy import stats
z_scores = np.abs(stats.zscore(df['revenue']))
outliers = df[z_scores > 3]
Key decision: Remove outliers? Cap them (winsorize)? Keep them? Depends on whether they're errors or genuine extreme values.
7. Feature Engineering Insights from EDA
EDA directly informs feature engineering:
| EDA Finding | Feature Engineering Action |
|---|---|
| Skewed distribution | Log transform |
| High cardinality categorical | Target encoding or hash encoding |
| Missing values with pattern | Add "is_missing" binary feature |
| Date column | Extract day_of_week, month, is_weekend |
| Two correlated features | Create ratio or difference feature |
8. Systematic EDA Checklist
- βShape & types:
df.shape,df.dtypes,df.info() - βSummary stats:
df.describe(),df.describe(include='object') - βMissing values:
df.isna().sum(), pattern analysis - βDistributions: histograms for numerics, value_counts for categoricals
- βCorrelations: heatmap, scatter plots for top pairs
- βTarget analysis: class balance, target distribution
- βOutliers: IQR or z-score check on key features
- βDuplicates:
df.duplicated().sum()
Now you're ready to attempt the exercises. Switch to the Exercise tab and start coding!
Ready to practice?
EDA Lab + Homework (Student)
Goal: practice high-signal EDA like you would in a FAANG ML interview or on-call investigation.
Rules:
- βWork top-to-bottom
- βDon't hardcode outputs
- βPrefer concise, high-signal plots