3. Data Preprocessing, Scikit-Learn Pipelines & Tuning

Sections 10-12 & 18-20: Train/Test Split, K-Fold CV, StandardScaler, OneHotEncoder, ColumnTransformer, GridSearchCV & SMOTE

Module 1: Data Preprocessing, Scaling & Encoding

Standardizing features, encoding categorical variables, and handling missing data without data leakage.

🇬🇧 English Definition

Feature scaling (StandardScaler, MinMaxScaler) and categorical encoding (OneHotEncoder, OrdinalEncoder) transform raw data into numeric matrices. Preprocessing parameters must be fit ONLY on training data.

🇮🇳 Hinglish Definition

Distance-sensitive models ke liye scaling zaroori hai. Preprocessors ko hamesha sirf training set par fit (sc.fit_transform) karna chahiye leakage rokne ke liye.

Core Concepts & Principles

  • StandardScaler: Z-score normalization centering mean at 0 and variance at 1.
  • OneHotEncoder: Converts nominal categories into numeric binary indicator columns.
  • Data Leakage Rule: Never fit scalers or imputers on validation or test sets.

🔍 Line-by-Line Code Breakdown

  1. Line 1-2: Import StandardScaler and OneHotEncoder from sklearn.preprocessing.
  2. Line 3-4: Fit scaler on X_train and transform X_test independently.

🎯 Interview Focus & Questions

  • Q: Why fit preprocessors only on training data?
    A: To prevent data leakage from the test/validation set into the training process.
💻 Practical AI/ML Code Example - Data Preprocessing, Scaling & Encoding
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.model_selection import train_test_split

// Leakage-safe preprocessing workflow
print("Preprocessing module ready.")
📤 Execution Output / Interpretation:
Preprocessing module ready.

Module 2: Scikit-Learn Pipelines & Hyperparameter Tuning

Encapsulating workflows with Pipeline and ColumnTransformer, tuning with GridSearchCV, and handling SMOTE imbalanced data.

🇬🇧 English Definition

Pipeline chains preprocessing and modeling into a single reproducible object preventing leakage. ColumnTransformer handles mixed data types. GridSearchCV evaluates hyperparameter combinations.

🇮🇳 Hinglish Definition

Pipeline preprocessing aur model ko bind karta hai taaki leakage na ho. ColumnTransformer numeric aur categorical columns ko alag transform karta hai.

Core Concepts & Principles

  • ColumnTransformer: Applies different transformers to numeric vs categorical column subsets.
  • GridSearchCV: Exhaustively searches parameter grids using cross-validation.
  • SMOTE: Generates synthetic minority samples for imbalanced datasets.

🔍 Line-by-Line Code Breakdown

  1. Line 1-3: Build ColumnTransformer with StandardScaler for numeric and OneHotEncoder for categorical columns.
  2. Line 4-5: Construct Pipeline combining ColumnTransformer and Classifier.

🎯 Interview Focus & Questions

  • Q: Why use Scikit-Learn Pipelines?
    A: Ensures reproducible workflows, prevents data leakage during cross-validation, and simplifies model deployment.
💻 Practical AI/ML Code Example - Scikit-Learn Pipelines & Hyperparameter Tuning
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder

// Reproducible Pipeline Construction
print("Pipeline & ColumnTransformer Ready.")
📤 Execution Output / Interpretation:
Pipeline & ColumnTransformer Ready.