Data Science Pipelines: EDA, Feature Scaling, OLAP Schemas & SQL Window Functions
Preventing train-test data leakage, Z-score vs Min-Max vs Robust IQR scaling, MCAR/MAR/MNAR missingness, Star vs Snowflake OLAP cube operations, and SQL ROW_NUMBER / RANK / DENSE_RANK.
1. Feature Engineering & The Cardinal Sin of Data Leakage
Train-Test Preprocessing Leakage: Always compute , imputation medians, target encodings, and PCA eigenvectors on the Training Split ONLY (or inside each fold of -Fold Cross-Validation), then apply scaler.transform(X_test)! Fitting a scaler or oversampler (SMOTE) on the full dataset before splitting leaks test distribution statistics and inflates validation metrics!
| Missing Data Mechanism | Definition | Safe Handling Strategy |
|---|---|---|
| MCAR (Missing Completely at Random) | Missingness independent of both observed & unobserved values | Listwise deletion is unbiased (loses power); median/mean imputation |
| MAR (Missing at Random) | Missingness depends only on observed covariates (e.g. age/role) | Multiple Imputation (MICE), k-NN / iterative model imputation |
| MNAR (Missing Not at Random) | Missingness depends on the missing value itself (e.g. high earners hiding income) | Add explicit is_missing indicator flag + domain modeling |
2. Data Warehousing: Star vs Snowflake Schema & OLAP Operations
- Star Schema: Central denormalized numeric Fact Table surrounded by 1-hop Dimension Tables (in 2NF/denormalized) fewer
JOINs, faster analytical OLAP queries! - Snowflake Schema: Normalizes dimension hierarchies into 3NF sub-tables saves storage & avoids update anomalies, but slower multi-join reads.
- OLAP Cube Operations: Roll-Up (aggregate up hierarchy: City Country), Drill-Down (finer granularity: Year Quarter), Slice (fix dimension, e.g.
Year = 2026), Dice (sub-cube range across dimensions), Pivot (rotate axes).
3. SQL Analytical Window Functions (ROW_NUMBER vs RANK vs DENSE_RANK)
Suppose salaries in a department are [90, 80, 80, 70] ordered DESC:
SELECT emp_name, salary,
ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rn,
RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rnk,
DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS drnk
FROM employees;
ROW_NUMBER():1, 2, 3, 4(unique sequential integers, breaks ties arbitrarily)RANK():1, 2, 2, 4(ties share rank, skips3)DENSE_RANK():1, 2, 2, 3(ties share rank, never skips — always useDENSE_RANK() = Nfor "Nth highest distinct salary"!)
Practice Questions on This Chapter (3)
A dataset feature has quartiles , , and . Under Tukey’s rule, any observation strictly above which upper fence is flagged as an outlier?
For the ordered score column [100, 95, 95, 90], what sequence of integers is returned by SQL RANK() OVER (ORDER BY score DESC) vs DENSE_RANK() OVER (ORDER BY score DESC)?
Which step in a Data Science pipeline introduces train-test data leakage and produces overly optimistic validation scores?