Data Preprocessing in ML — Stopping Silent Data Leakage
A credit model's 0.96 AUC crashed when mean imputation leaked future data.
20+ years shipping production ML systems and the infrastructure behind them. Drawn from code that ran under real load.
- ✓Solid grasp of fundamentals
- ✓Comfortable reading code examples
- ✓Basic production concepts
- Data preprocessing transforms raw, messy data into a clean format ML models can learn from
- Handles missing values via imputation (median, most_frequent) plus missing indicator columns
- Encodes categorical features: OneHotEncoder for nominal, OrdinalEncoder for ordinal
- Scales numerical features to prevent large-valued features from dominating distance metrics
- Splitting train/test BEFORE any fitting prevents data leakage — the #1 production bug
- Use scikit-learn Pipeline + ColumnTransformer to chain steps safely and reproducibly
Data preprocessing is the transformation of raw data into a clean, structured format that machine learning algorithms can actually learn from. It's not a 'nice to have' step — it's the difference between a model that generalizes to production data and one that silently memorizes noise.
The core problem preprocessing solves is that real-world data is never ready for training: it has missing values, inconsistent scales, non-numeric categories, and outliers that can dominate gradient updates or distance calculations. Skipping or mishandling any of these steps introduces data leakage — where information from outside the training set (like target statistics or future data) leaks into the model, giving you inflated validation scores and catastrophic production failures.
In the scikit-learn ecosystem, preprocessing is handled through dedicated transformers like SimpleImputer, OneHotEncoder, StandardScaler, and RobustScaler, which you chain together in a Pipeline to ensure every transformation is learned only on training data and applied identically to test data. The critical insight is that preprocessing is not a one-size-fits-all: missing value imputation must consider whether data is MCAR, MAR, or MNAR; categorical encoding depends on whether the feature has ordinal relationships; and scaling choice (standard vs. min-max vs. robust) depends on whether your algorithm assumes normally distributed features or is sensitive to outliers.
Tools like pandas, numpy, and scikit-learn are the standard stack, but when you need to scale to terabytes, you'd reach for Spark's VectorAssembler or Dask — though the principles remain identical.
Where preprocessing goes wrong most often is in the subtle leaks: using fit_transform on the entire dataset before train/test split, imputing missing values with the global mean (which uses test data), or encoding categories that don't appear in the training set. The rule of thumb: if you're touching the data before splitting, you're leaking.
When not to use heavy preprocessing? Tree-based models (XGBoost, LightGBM, Random Forest) are scale-invariant and handle missing values natively, so scaling and imputation are often unnecessary — but one-hot encoding still matters for categorical splits.
The real art is knowing which preprocessing steps your algorithm's math actually demands versus which are cargo-culted from a blog post.
Imagine you're baking a cake and your recipe calls for cups of flour, but someone gave you the flour in grams, some of it is wet, and a handful of raisins are still in the bag mixed in. Before you can bake anything, you have to fix the ingredients first. Data preprocessing is exactly that — cleaning, converting, and organising your raw data so a machine learning model can actually learn from it. Garbage in, garbage out.
Every ML tutorial starts with a clean, perfectly formatted dataset. Real life never does. In the real world, data comes from messy CSV exports, broken sensors, rushed data-entry clerks, and legacy databases that mix text and numbers in the same column. The gap between raw data and model-ready data is where most ML projects actually live — and die. Skipping preprocessing is the single biggest reason a model that looked great in a notebook performs terribly in production.
Preprocessing solves three fundamental problems: data your model can't read (wrong types, text categories), data your model misreads (wildly different scales that trick distance-based algorithms), and data that simply isn't there (missing values that silently corrupt your results). Each of these problems has a well-understood solution, but the order and method you choose matter enormously depending on your data and your model.
By the end of this article you'll be able to audit a raw dataset, choose the right strategy for missing values, encode categorical features correctly, scale numerical features without leaking information from your test set, and wire everything together in a reproducible scikit-learn Pipeline. You'll also know the three mistakes that trip up intermediate practitioners — not just beginners.
Data Preprocessing in ML — The Gatekeeper of Generalization
Data preprocessing is the systematic transformation of raw data into a clean, structured format that machine learning algorithms can consume. It’s not just about handling missing values or scaling features — it’s about preventing silent data leakage. Leakage occurs when information from the test set or future data inadvertently influences the training process, inflating performance metrics and causing models to fail in production. The core mechanic is to apply all transformations (imputation, scaling, encoding) strictly within each cross-validation fold, fitting only on the training split and then transforming the validation split.
In practice, preprocessing pipelines must be stateless and reproducible. For example, when using StandardScaler in Java with libraries like Smile or Tribuo, you fit the scaler on training data (computing mean and variance), then transform both training and test sets using those same parameters. A common mistake is to fit the scaler on the entire dataset before splitting — this leaks global statistics into every fold, making cross-validation scores artificially optimistic by 5–15%. The same principle applies to one-hot encoding, missing value imputation (e.g., mean imputation must use training-set mean), and feature selection.
Use data preprocessing in every supervised learning project, especially when data is heterogeneous or contains missing values. It matters most in high-stakes systems like fraud detection or medical diagnosis, where a 2% performance overestimate due to leakage can lead to deploying a model that fails silently on new data. The rule: treat preprocessing as part of the model, not as a separate data-cleaning step. Every transformation must be learned from training data and applied identically to new data at inference time.
Handling Missing Values — Why 'Just Drop Them' Is Usually Wrong
Missing data isn't random noise you can ignore. It's a signal. A missing income field in a loan application might mean the applicant refused to share it — which is itself predictive. Blindly dropping rows throws away that signal and shrinks your training set.
There are three strategies: deletion, imputation, and indicator flags. Deletion (dropping rows or columns) only makes sense when less than 5% of a column is missing AND missingness is truly random. Imputation replaces missing values with something plausible — the mean or median for numerical data, the most frequent value for categorical data, or a model-predicted value for high-stakes features.
The best practice for production is to combine imputation with a binary indicator column: a new column that says 'this value was missing' lets the model learn from the missingness pattern itself. Scikit-learn's SimpleImputer handles the replacement; you add the flag column manually before imputing.
Crucially, you must fit your imputer on training data only, then transform both train and test. Fitting on the full dataset leaks future information into your model — a subtle bug that inflates validation scores.
import pandas as pd import numpy as np from sklearn.impute import SimpleImputer from sklearn.model_selection import train_test_split # --- Simulate a messy real-world dataset --- np.random.seed(42) raw_data = pd.DataFrame({ 'age': [25, np.nan, 47, 31, np.nan, 52, 29, 40], 'salary': [48000, 72000, np.nan, 61000, 58000, np.nan, 43000, 95000], 'bought': [0, 1, 1, 0, 1, 1, 0, 1] # target label }) print("Raw data:") print(raw_data) print(f"\nMissing counts:\n{raw_data.isnull().sum()}") # --- Step 1: Add binary indicator columns BEFORE imputing --- # These columns tell the model WHERE data was missing — that pattern has meaning. for col in ['age', 'salary']: raw_data[f'{col}_was_missing'] = raw_data[col].isnull().astype(int) # --- Step 2: Split BEFORE fitting the imputer --- # Fitting on all data first would leak test-set statistics into training. features = raw_data.drop(columns='bought') labels = raw_data['bought'] X_train, X_test, y_train, y_test = train_test_split( features, labels, test_size=0.25, random_state=42 ) # --- Step 3: Fit imputer on TRAINING data only --- # strategy='median' is more robust to outliers than 'mean' numeric_cols = ['age', 'salary'] imputer = SimpleImputer(strategy='median') # fit_transform on train, transform-only on test X_train[numeric_cols] = imputer.fit_transform(X_train[numeric_cols]) X_test[numeric_cols] = imputer.transform(X_test[numeric_cols]) # NO fit here! print("\nTraining set after imputation:") print(X_train.round(1)) print("\nImputer medians learned from training data:", imputer.statistics_)
Encoding Categorical Features — Choosing Between Label, Ordinal, and One-Hot
Machine learning models are fundamentally mathematical. They multiply, add, and compare numbers. When your data has a column called 'City' with values like 'London', 'Paris', 'Tokyo', the model can't do anything with strings — you have to convert them.
The wrong choice here actively hurts your model. Label encoding assigns integers arbitrarily: London=0, Paris=1, Tokyo=2. That implies Tokyo > Paris > London mathematically, which is nonsense. Any model using arithmetic on those integers — linear regression, neural nets, SVMs — will learn a false relationship.
One-Hot Encoding (OHE) is the correct fix for nominal categories (no natural order). It creates a new binary column per category: is_London, is_Paris, is_Tokyo. No false ordering. The trade-off is that high-cardinality columns (e.g. 500 cities) explode your feature space — in that case, target encoding or embedding layers are better alternatives.
Ordinal encoding IS appropriate when the order genuinely matters: ['cold', 'warm', 'hot'] → [0, 1, 2] is correct because hot > warm > cold is real. Use OrdinalEncoder for these, not LabelEncoder (which is meant for target labels only).
Always handle unseen categories in your test set. A category that appears in production but wasn't in training will crash a naive encoder.
import pandas as pd from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder from sklearn.compose import ColumnTransformer # --- Dataset with two types of categorical columns --- clothing_data = pd.DataFrame({ 'city': ['London', 'Paris', 'Tokyo', 'London', 'Berlin', 'Paris'], 'size': ['small', 'large', 'medium', 'large', 'small', 'medium'], 'price_usd': [120, 85, 200, 115, 95, 90] }) print("Original data:") print(clothing_data) # 'city' is NOMINAL — no natural order, use One-Hot Encoding # 'size' is ORDINAL — small < medium < large, order is meaningful nominal_features = ['city'] ordinal_features = ['size'] numeric_features = ['price_usd'] # Define the correct ordering for the ordinal column explicitly size_order = [['small', 'medium', 'large']] preprocessor = ColumnTransformer(transformers=[ ('one_hot', OneHotEncoder(handle_unknown='ignore', sparse_output=False), nominal_features), ('ordinal', OrdinalEncoder(categories=size_order), ordinal_features), ('passthrough', 'passthrough', numeric_features) ]) # fit_transform on the full set here just for demonstration encoded_array = preprocessor.fit_transform(clothing_data) # Recover column names for readability ohe_feature_names = preprocessor.named_transformers_['one_hot'].get_feature_names_out(nominal_features) all_column_names = list(ohe_feature_names) + ordinal_features + numeric_features encoded_df = pd.DataFrame(encoded_array, columns=all_column_names) print("\nEncoded data:") print(encoded_df) print("\n'size' ordinal mapping — small=0, medium=1, large=2 (correct order preserved)")
Feature Scaling — Why Your Algorithm's Math Demands It
Picture two features: age (18–65) and annual salary (30,000–150,000). The salary values are 3,000x larger. Any algorithm that computes distances or uses gradient descent treats the salary as 3,000x more important — purely because of measurement units, not because it actually matters more.
This kills k-Nearest Neighbours (distances dominated by salary), SVMs, and gradient descent convergence in neural nets. Tree-based models like Random Forest and XGBoost are the exception — they split on thresholds and don't care about absolute scale.
Two scalers solve this in different ways. StandardScaler subtracts the mean and divides by standard deviation, producing a distribution centred at 0 with unit variance. Use it when your data is roughly Gaussian or when the algorithm assumes it (linear/logistic regression, PCA, SVMs).
MinMaxScaler compresses values into a fixed range, typically [0, 1]. Use it when you need bounded outputs — for example, feeding pixel values into a neural network, or when the algorithm explicitly requires [0,1] input. Its weakness: a single extreme outlier squashes all other values into a tiny range.
RobustScaler uses the median and interquartile range instead of mean and standard deviation. It's your best friend when data has significant outliers — a faulty sensor reading of 999999 won't ruin your entire scaling.
import numpy as np import pandas as pd from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler # --- Sensor readings dataset with one outlier row (row index 5) --- sensor_data = pd.DataFrame({ 'temperature_c': [22.1, 23.5, 21.8, 24.0, 22.9, 999.9, 23.1], # 999.9 is a broken sensor 'humidity_pct': [45, 50, 43, 55, 48, 47, 51 ] }) print("Original sensor readings:") print(sensor_data) print() # Fit each scaler independently on the same data for comparison standard_scaler = StandardScaler() minmax_scaler = MinMaxScaler() robust_scaler = RobustScaler() # uses median + IQR, ignores outliers effectively std_scaled = standard_scaler.fit_transform(sensor_data) minmax_scaled = minmax_scaler.fit_transform(sensor_data) robust_scaled = robust_scaler.fit_transform(sensor_data) # Package results for easy comparison result = pd.DataFrame({ 'temp_original': sensor_data['temperature_c'], 'temp_standard': std_scaled[:, 0].round(3), 'temp_minmax': minmax_scaled[:, 0].round(3), 'temp_robust': robust_scaled[:, 0].round(3), }) print("Scaling comparison (temperature column only):") print(result) print() print("Notice how MinMaxScaler crushes all normal readings to near-zero") print("because the outlier 999.9 dominates the range.") print("RobustScaler keeps normal readings well spread — outlier is still large but harmless.")
Wiring It All Together With a scikit-learn Pipeline
You've now got individual tools for missing values, encoding, and scaling. The temptation is to apply them manually one by one in a sequence of function calls. Don't. Manual preprocessing has two fatal flaws: you'll inevitably leak training statistics into your test set (because it's easy to forget to split first), and you can't reliably reproduce or deploy the same sequence.
Scikit-learn's Pipeline chains transformers and a final estimator into a single object. When you call pipeline.fit(X_train, y_train), every transformer is fit on training data only and then applied in sequence. When you call pipeline.predict(X_test), transformers are applied using the already-fitted parameters — no leakage possible.
ColumnTransformer lets you apply different preprocessing to different columns inside the same Pipeline step. Numeric columns get imputed then scaled; categorical columns get imputed then one-hot encoded. Everything stays in sync.
This pattern also makes deployment trivial. You save one pipeline object with joblib. You load it in production. You call predict on raw, unprocessed input. The pipeline handles everything. No separate preprocessing script to maintain.
import pandas as pd import numpy as np from sklearn.pipeline import Pipeline from sklearn.compose import ColumnTransformer from sklearn.impute import SimpleImputer from sklearn.preprocessing import StandardScaler, OneHotEncoder from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score import joblib # --- Realistic loan application dataset --- np.random.seed(0) loan_data = pd.DataFrame({ 'age': [34, np.nan, 28, 45, 52, 31, np.nan, 40, 37, 29], 'income': [52000, 81000, np.nan, 120000, 95000, 43000, 67000, np.nan, 58000, 74000], 'employment': ['employed', 'self-employed', 'employed', np.nan, 'employed', 'unemployed', 'employed', 'self-employed', np.nan, 'employed'], 'city': ['London', 'Manchester', 'London', 'Birmingham', 'London', 'Manchester', 'Birmingham', 'London', 'Manchester', 'London'], 'loan_approved': [1, 1, 0, 1, 1, 0, 1, 0, 0, 1] # target }) features = loan_data.drop(columns='loan_approved') labels = loan_data['loan_approved'] # Identify which columns need which treatment numeric_cols = ['age', 'income'] categorical_cols = ['employment', 'city'] X_train, X_test, y_train, y_test = train_test_split( features, labels, test_size=0.3, random_state=42 ) # --- Build the numeric sub-pipeline --- # Impute first (median is robust to outliers), then scale numeric_pipeline = Pipeline(steps=[ ('imputer', SimpleImputer(strategy='median')), ('scaler', StandardScaler()) ]) # --- Build the categorical sub-pipeline --- # Impute with most frequent value, then one-hot encode categorical_pipeline = Pipeline(steps=[ ('imputer', SimpleImputer(strategy='most_frequent')), ('encoder', OneHotEncoder(handle_unknown='ignore', sparse_output=False)) ]) # --- Combine into one ColumnTransformer --- preprocessor = ColumnTransformer(transformers=[ ('numeric', numeric_pipeline, numeric_cols), ('categorical', categorical_pipeline, categorical_cols) ]) # --- Full pipeline: preprocessing + model --- full_pipeline = Pipeline(steps=[ ('preprocessor', preprocessor), ('classifier', LogisticRegression(random_state=42, max_iter=500)) ]) # One call to fit — everything happens in the correct order, no leakage full_pipeline.fit(X_train, y_train) predictions = full_pipeline.predict(X_test) accuracy = accuracy_score(y_test, predictions) print(f"Test accuracy: {accuracy:.2f}") print(f"Predictions: {predictions}") print(f"Actual labels: {y_test.values}") # --- Save the entire pipeline for deployment --- # In production: load this file and call full_pipeline.predict(raw_input_df) joblib.dump(full_pipeline, 'loan_approval_pipeline.pkl') print("\nPipeline saved to loan_approval_pipeline.pkl") print("In production, load with: pipeline = joblib.load('loan_approval_pipeline.pkl')")
cross_val_score() instead of just the model. This guarantees that preprocessing is re-fit on each fold's training data, not on all the data before folding. Without this, cross-validation silently leaks, and your CV scores overestimate real-world performance. Pipeline makes this trivially safe.Outlier Detection and Treatment: When to Remove, Cap, or Transform
Outliers are data points that differ significantly from the rest. They can be genuine extreme values (e.g., a billionaire's income in a loan dataset) or errors (a sensor reading of 999.9°C). How you treat them depends on which case you're dealing with.
First, detect outliers. Common methods: Z-score (assumes normal distribution), IQR (robust, uses Q1-1.5IQR and Q3+1.5IQR), and domain-specific thresholds. For production, a combination works best: flag statistical outliers AND apply business rules (e.g., 'salary > $10M is impossible for our user base').
Once detected, you have three options. Remove: only when you're certain it's an error and you have enough data left. Cap (winsorize): replace outliers with the nearest non-outlier boundary — keeps the point but limits its influence. Transform: apply log or Box-Cox to reduce skew — makes the distribution more Gaussian and reduces outlier impact.
Never remove outliers blindly without understanding their origin. An outlier might be the most important data point — a fraud detection model must learn from extreme transaction amounts, not discard them.
import pandas as pd import numpy as np from scipy import stats # --- Simulated transaction amounts (some legitimate, some possible fraud) --- # Most transactions are < $10k, but a few are huge np.random.seed(42) transactions = pd.DataFrame({ 'amount': np.random.exponential(scale=3000, size=1000).round(2) }) # Inject a few genuine outliers (possible fraud) # - A $350,000 transfer # - A $500,000 transfer # - A -$5,000 (refund?) outlier_indices = [50, 200, 750] transactions.loc[50, 'amount'] = 350000 transactions.loc[200, 'amount'] = 500000 transactions.loc[750, 'amount'] = -5000 # negative value, likely error # --- Step 1: Detect outliers using IQR --- Q1 = transactions['amount'].quantile(0.25) Q3 = transactions['amount'].quantile(0.75) IQR = Q3 - Q1 lower_bound = Q1 - 1.5 * IQR upper_bound = Q3 + 1.5 * IQR outliers_iqr = transactions[(transactions['amount'] < lower_bound) | (transactions['amount'] > upper_bound)] print(f"IQR outlier bounds: lower={lower_bound:.2f}, upper={upper_bound:.2f}") print(f"Number of IQR outliers: {len(outliers_iqr)}") # --- Step 2: Option A — Cap outliers (winsorize) --- transactions['amount_capped'] = transactions['amount'].clip(lower=lower_bound, upper=upper_bound) # --- Step 3: Option B — Log transform (handle positive skew) --- # Shift to avoid log(0) or negative values transactions['amount_log'] = np.log1p(transactions['amount'].clip(lower=0)) # --- Step 4: Review the treated outliers --- print("\nSample of original amounts with capping:") sample = transactions.loc[outlier_indices, ['amount', 'amount_capped', 'amount_log']] print(sample) print("\nNote: The negative $5k is likely an error — should be investigated before any treatment.")
- Measurement errors — remove or cap; they corrupt training.
- Extreme truths — keep but transform; they contain signal.
- Always cross-reference with business logic before deciding.
- Log transform makes right-skewed data more normal-friendly.
Correlation Analysis — Your First Line of Defense Against Multicollinearity
Most juniors skip correlation analysis until their model starts behaving like a drunk uncle at a wedding — unstable coefficients, garbage feature importance, and a validation score that nosedives every time they retrain.
Correlation tells you which features are redundant. When two features have a Pearson correlation above 0.8, your linear model will start hallucinating importance. Regularised models like Ridge can compensate, but tree-based models? They'll just split on one and ignore the other, wasting compute.
The fix: generate a correlation matrix and pick a threshold — 0.7 for conservative pipelines, 0.85 if you're feeling lucky. Flag every pair above it. Then decide: drop one, or combine them into a composite feature (e.g., sum or ratio). Don't automate this blindly. Talk to your domain expert first.
Correlation is cheap to compute and tells you more about your data than any dashboard ever will. Run it before you scale, before you split, before you do anything else.
// io.thecodeforge — ml-ai tutorial import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt # Load your pre-cleaned dataset df = pd.read_csv('patient_readmissions_clean.csv') # Compute correlation matrix (exclude target) corr_matrix = df.drop('readmitted', axis=1).corr(numeric_only=True) # Find pairs above threshold high_corr_pairs = [] threshold = 0.8 for i in range(len(corr_matrix.columns)): for j in range(i): if abs(corr_matrix.iloc[i, j]) > threshold: col_i = corr_matrix.columns[i] col_j = corr_matrix.columns[j] high_corr_pairs.append((col_i, col_j, corr_matrix.iloc[i, j])) print(f"Found {len(high_corr_pairs)} high-correlation pairs:\n") for pair in high_corr_pairs: print(f" {pair[0]} <-> {pair[1]}: {pair[2]:.2f}")
Target Variable Distribution — Skew Is Not a Bug, It's a Design Constraint
You've cleaned the data, scaled the features, and your pipeline looks clean. Then your regression model outputs predictions that are all negative. Why? Because your target variable follows a log-normal distribution and you fed it to a model that assumes Gaussian residuals.
Before you touch any model, plot the target's histogram. If it's skewed — and most real-world targets are — you have three options: log-transform it, use a model that doesn't care about distribution (tree-based), or build a separate model for each quantile if you need extreme-value accuracy.
For classification, check class balance. A 95/5 split isn't a dataset problem — it's a business constraint. Oversample? Undersample? Use class weights? The answer depends on the cost of a false negative vs. a false positive. If you're detecting fraud, a 5% class is gold. If you're predicting churn, undersampling to 50/50 might destroy real-world calibration.
Plot the distribution. Understand its shape. Then decide how to handle it — don't let the default loss function make that call for you.
// io.thecodeforge — ml-ai tutorial import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from scipy.stats import skew, boxcox # Load data df = pd.read_csv('energy_consumption.csv') target = df['consumption_kwh'] # Compute skewness skew_val = skew(target.dropna()) print(f"Skewness: {skew_val:.2f}") # Plot original distribution fig, axes = plt.subplots(1, 2, figsize=(12, 4)) sns.histplot(target, bins=50, ax=axes[0]) axes[0].set_title(f'Original Distribution (Skew={skew_val:.2f})') # Log-transform if skewed if abs(skew_val) > 1: target_log = np.log1p(target) sns.histplot(target_log, bins=50, ax=axes[1], color='green') axes[1].set_title('Log-Transformed') plt.tight_layout() plt.show() print("Applied log1p transform. Rerun model with log-transformed target.") else: print("Target distribution is near-normal. Proceed with original.")
Data Engineering vs. Feature Engineering: Know Which Fight You're In
Most juniors blur these two into 'getting the data ready.' That's how pipelines rot. Data engineering is about infrastructure: ingestion, storage, deduplication, schema enforcement. It's batch jobs, streaming, and making sure the CSV actually has the 10 million rows the business promised. Feature engineering is about transforming that raw material into something a model can exploit: creating interaction terms, binning timestamps, extracting cyclical signals from hours of the day.
You don't optimize a feature-engineering step with Spark RDDs. You don't fix a schema mismatch with a polynomial feature. The confusion causes storage bloat and training-time nightmares. Production teams split these roles for a reason: data engineers build the pipes, ML engineers build the features. If you're solo, force yourself to define the boundary before writing a single line. Write the data contracts first. Then decide whether you're fixing a hole in the floor or polishing the floorboards.
// io.thecodeforge — ml-ai tutorial import pandas as pd from sklearn.base import BaseEstimator, TransformerMixin # Data engineering: raw ingestion & schema enforcement def load_fraud_data(path): dtype_spec = { 'user_id': 'int64', 'timestamp': 'datetime64[ns]', 'amount': 'float32', 'merchant': 'category' } df = pd.read_csv(path, dtype=dtype_spec, parse_dates=['timestamp']) assert df['amount'].isna().sum() < 0.01 * len(df), "Too many null amounts" return df # Feature engineering: model-facing transformation class TimeCycleEncoder(BaseEstimator, TransformerMixin): def fit(self, X, y=None): return self def transform(self, X): hours = X['timestamp'].dt.hour X['hour_sin'] = np.sin(2 * np.pi * hours / 24) X['hour_cos'] = np.cos(2 * np.pi * hours / 24) return X.drop(columns=['timestamp'])
Target Variable Distribution: Skew Is Not a Bug, It's a Design Constraint
Your model learns from the distribution it sees. If your target is skewed — say, 1% fraud, 99% legitimate — a model that predicts 'not fraud' every time hits 99% accuracy and learns nothing. Skew isn't a data quality problem; it's a modeling constraint that dictates everything downstream: loss functions, evaluation metrics, sampling strategies.
Before you touch a single preprocessing step, log-transform your regression target or compute the class ratio for classification. If the skew ratio exceeds 10:1, you're in a whole different game. Use stratified splits. Switch from accuracy to precision-recall or log-loss. Consider resampling only after you've confirmed your baseline can't handle it. And never — never — blindly apply SMOTE without understanding whether your minority class is clean signal or measurement noise. Skew tells you where the model needs to work harder. Pay attention.
// io.thecodeforge — ml-ai tutorial import numpy as np import pandas as pd from scipy.stats import skew from sklearn.model_selection import StratifiedKFold # Load and inspect target distribution df = pd.read_csv('transactions.csv') y = df['is_fraud'] ratio = y.value_counts(normalize=True) print(f"Class ratio: {ratio.to_dict()}") print(f"Skewness (raw): {skew(y):.3f}") # If ratio > 10:1, force stratified splitting if ratio.max() / ratio.min() > 10: print("Heavy skew detected — using stratified CV") cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42) for train_idx, val_idx in cv.split(df, y): # ... train inside this split pass else: print("Moderate skew — standard CV is fine")
ETL vs ELT in Python — Why Order Matters for ML Pipelines
Extract, Transform, Load (ETL) and Extract, Load, Transform (ELT) differ only in when transformation happens, but that shift changes your preprocessing strategy entirely. In ETL, you clean and shape data before storing it — good for small-to-medium datasets where you control the schema upfront. ELT loads raw data first and transforms it on read; ideal for massive datasets where raw storage is cheap and transformation is deferred to query time. For ML preprocessing, ETL suits classical scikit-learn pipelines: you extract from CSV or API, impute missing values, encode categories, scale features, then load into a clean Parquet table. ELT matches cloud-native workflows: load raw JSON into a data lake, then run Spark or SQL transformations only when training begins. Choose ETL when you need reproducibility and fast iteration. Choose ELT when you handle terabytes and want schema flexibility. Neither is universally better — pick based on data volume and infrastructure constraints.
// io.thecodeforge — ml-ai tutorial import pandas as pd # ETL: transform before storage df = pd.read_csv('raw_data.csv') df = df.dropna(subset=['target']) df['category'] = df['category'].astype('category').cat.codes df.to_parquet('clean_data.parquet') # ELT: load raw, transform on read raw = pd.read_parquet('raw_data.parquet') from sklearn.preprocessing import LabelEncoder encoder = LabelEncoder() raw['cat_encoded'] = encoder.fit_transform(raw['category'])
Iterative Improvements — Why Perfection Is the Enemy of Deployed ML
Most data preprocessing fails because teams try to build the perfect pipeline before seeing a single prediction. An iterative approach flips this: ship a minimal viable preprocessing step, get model output, then refine. Start with dropping rows with missing values and a basic one-hot encoder. Train a baseline model — even a dumb one. Measure its errors and ask: does missing value imputation improve this specific failure? Does scaling help this tree-based model? Each iteration targets one bottleneck. Use a tracking tool (MLflow, Weights & Biases) to log preprocessing choices and their impact on validation metrics. The key insight: preprocessing is not a one-time feast — it's an adaptive loop. Feature engineering, outlier handling, and encoding strategies should evolve as you see more data and edge cases. Avoid premature optimization. A pipeline with 80% correctness deployed today beats a 95% correct one next month. The loop itself teaches you which transformations actually matter for your problem.
// io.thecodeforge — ml-ai tutorial def preprocess_v1(df): return df.dropna() def preprocess_v2(df): df['age'].fillna(df['age'].median(), inplace=True) return pd.get_dummies(df, columns=['city']) # Track iterations import mlflow with mlflow.start_run(): train_data = preprocess_v1(train_raw) score = train_model(train_data) mlflow.log_param('preprocess_version', 'v1') mlflow.log_metric('rmse', score)
4. Support Vector Machines (SVM)
Support Vector Machines are fundamentally about finding the decision boundary that maximizes the margin between classes. Why does margin matter? A maximum-margin hyperplane is more robust to noise and small perturbations in the data, reducing generalization error. SVM achieves this by focusing only on the "support vectors" — the data points closest to the decision boundary. For non-linear data, the kernel trick (RBF, polynomial) projects patterns into higher-dimensional space without explicit computation, making classification possible. In preprocessing, SVM is highly sensitive to feature scales: features with larger ranges will dominate the margin calculation. Always apply StandardScaler or MinMaxScaler before training. Outliers are especially damaging because they can become support vectors and warp the boundary. For high-dimensional sparse data, linear SVM performs well with minimal preprocessing, but dense non-linear data demands careful scaling and outlier handling.
// io.thecodeforge — ml-ai tutorial from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from sklearn.svm import SVC from sklearn.model_selection import train_test_split import numpy as np X, y = np.random.rand(100, 4), np.random.randint(0, 2, 100) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) pipe = Pipeline([ ('scaler', StandardScaler()), ('svm', SVC(kernel='rbf', C=1.0, gamma='scale')) ]) pipe.fit(X_train, y_train) print(f"Accuracy: {pipe.score(X_test, y_test):.2f}")
5. k-Nearest Neighbors (k-NN)
k-NN is a lazy, non-parametric algorithm that classifies based on the majority vote of its k closest neighbors. Why is preprocessing critical here? Because k-NN relies entirely on distance metrics (Euclidean, Manhattan). Features with larger numerical ranges will dominate the distance calculation, making the algorithm effectively ignore smaller-scale but equally important variables. Standard scaling or min-max normalization is mandatory — not optional. Another often-overlooked aspect: the curse of dimensionality. As the number of features increases, distances become nearly uniform, making neighbor selection meaningless. For high-dimensional data, apply PCA or feature selection before k-NN. Outliers can also distort distances: a single extreme value can pull neighbors away from true clusters. Use Winsorization or robust scaling. Finally, choose k via cross-validation: small k risks overfitting, large k blurs class boundaries.
// io.thecodeforge — ml-ai tutorial from sklearn.neighbors import KNeighborsClassifier from sklearn.preprocessing import StandardScaler from sklearn.pipeline import Pipeline from sklearn.datasets import load_iris import numpy as np iris = load_iris() X, y = iris.data, iris.target pipe = Pipeline([ ('scaler', StandardScaler()), ('knn', KNeighborsClassifier(n_neighbors=5, metric='euclidean')) ]) pipe.fit(X, y) pred = pipe.predict(X[:3]) print(f"Predictions: {pred}")
8. Introduction to Ensemble Learning
Ensemble learning combines multiple models to produce a stronger predictor. Why does this work? Individual models make different errors; averaging or voting cancels out noise and reduces variance (bagging) or bias (boosting). The preprocessing requirements differ by ensemble type. For bagging (Random Forest), trees are robust to unscaled data and outliers — no scaling needed. However, one-hot encoding high-cardinality features can splinter splits, so consider target encoding instead. For boosting (XGBoost, LightGBM), missing values are handled natively, but outliers can still pull gradient updates. Capping extreme values helps. For stacking, ensure all base models are trained on the same preprocessed data; scale differently per model type if needed. A common production pitfall: using different preprocessing for training and validation in a stacking setup — always use a consistent pipeline across all folds.
// io.thecodeforge — ml-ai tutorial from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import cross_val_score from sklearn.datasets import make_classification import numpy as np X, y = make_classification(n_samples=100, n_features=5, random_state=42) rf = RandomForestClassifier(n_estimators=50, max_depth=5) scores = cross_val_score(rf, X, y, cv=5, scoring='accuracy') print(f"CV Accuracy: {scores.mean():.2f} (+/- {scores.std():.2f})")
The 2 a.m. Crash: When Missing Value Imputation Silently Bankrupted Predictions
- Preprocessing must be identical between training and production — use a Pipeline serialized with joblib.
- Always include missing-indicator columns — they carry information about the data generation process.
- Monitor feature statistics (missingness rate, mean, std) in production — if they drift, your pipeline assumptions may be violated.
python -c "import joblib; p=joblib.load('pipeline.pkl'); print(p.named_steps)" # check steps are presentpython -c "import pickle; import numpy as np; print(np.load('input_sample.npy')[:2])" # compare input shape and scalegrep 'ValueError.*unknown category' /var/log/app.log | tail -5python -c "from sklearn.preprocessing import OneHotEncoder; enc=OneHotEncoder(handle_unknown='ignore'); print('fix confirmed')" # test locallypython -c "import pandas as pd; prod=pd.read_parquet('production_data.parquet'); print(prod.describe())"python -c "from scipy import stats; print(stats.ks_2samp(train_data, prod_data))" # detect distribution shiftgrep 'MemoryError' /var/log/app.log | head -1 # identify which columnpython -c "import pandas as pd; data=pd.read_csv('train.csv'); print(data.nunique())" # find high-cardinality columnspython -c "import numpy as np; train=np.load('X_train.npy'); print(np.any(np.std(train, axis=0)==0))"python -c "import numpy as np; prod=np.load('X_prod.npy'); print(np.any(np.isnan(prod)))"| Aspect | StandardScaler | MinMaxScaler | RobustScaler |
|---|---|---|---|
| Formula | (x - mean) / std | (x - min) / (max - min) | (x - median) / IQR |
| Output range | Unbounded (~-3 to 3) | Exactly [0, 1] | Unbounded, centred on median |
| Outlier sensitivity | High — outliers shift mean and std | Very high — single outlier dominates range | Low — uses median and IQR, ignores tails |
| Best for | Gaussian data, PCA, linear models, SVMs | Neural nets needing bounded input, image pixels | Data with known outliers (sensors, finance) |
| Loses interpretability? | Yes — values no longer in original units | Partially — proportional but shifted | Yes — relative to median not mean |
| File | Command / Code | Purpose |
|---|---|---|
| handle_missing_values.py | from sklearn.impute import SimpleImputer | Handling Missing Values |
| encode_categorical_features.py | from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder | Encoding Categorical Features |
| compare_feature_scalers.py | from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler | Feature Scaling |
| full_preprocessing_pipeline.py | from sklearn.pipeline import Pipeline | Wiring It All Together With a scikit-learn Pipeline |
| outlier_handling.py | from scipy import stats | Outlier Detection and Treatment |
| CorrelationCheck.py | df = pd.read_csv('patient_readmissions_clean.csv') | Correlation Analysis |
| TargetDistCheck.py | from scipy.stats import skew, boxcox | Target Variable Distribution |
| distinguish_pipeline.py | from sklearn.base import BaseEstimator, TransformerMixin | Data Engineering vs. Feature Engineering |
| skew_check.py | from scipy.stats import skew | Target Variable Distribution |
| etl_vs_elt.py | df = pd.read_csv('raw_data.csv') | ETL vs ELT in Python |
| IterativePreprocessing.py | def preprocess_v1(df): | Iterative Improvements |
| svm_preprocessing.py | from sklearn.pipeline import Pipeline | 4. Support Vector Machines (SVM) |
| knn_preprocessing.py | from sklearn.neighbors import KNeighborsClassifier | 5. k-Nearest Neighbors (k-NN) |
| ensemble_preprocessing.py | from sklearn.ensemble import RandomForestClassifier | 8. Introduction to Ensemble Learning |
Key takeaways
Interview Questions on This Topic
Why must you fit your preprocessing transformers only on training data? What specifically goes wrong if you fit on the full dataset?
When would you choose RobustScaler over StandardScaler, and can you give a concrete industry example where the difference matters?
Why do tree-based models like Random Forest not require feature scaling, while logistic regression and SVMs do? What property of these algorithms explains this?
Explain how you would handle a categorical feature with 500 unique categories in a logistic regression model without causing a dimensionality explosion.
What's the difference between imputing missing values and just dropping rows with NaN? When would you choose one over the other?
Frequently Asked Questions
Split into train and test first, then in this order: handle missing values (imputation), encode categorical features, scale numerical features, and finally feed into your model. Splitting first is non-negotiable — every other step must be fit on training data only. Wrapping all steps in a scikit-learn Pipeline enforces this order automatically.
No. Tree-based models like Decision Trees, Random Forests, and XGBoost are scale-invariant because they split on value thresholds — the absolute magnitude of a feature doesn't affect which split is chosen. Scaling is essential for distance-based algorithms (k-NN, SVM), linear models (logistic/linear regression), and neural networks, where large-valued features dominate gradient updates or distance calculations.
Dropping rows is fast but wastes data and introduces bias if missingness isn't random — for example, if low-income people consistently skip the income field, dropping them removes a real pattern your model should learn. Imputation preserves all rows and, when combined with a 'was_missing' indicator column, actually lets the model learn from the fact that data was absent. Use deletion only when less than ~5% of a column is missing and you're confident the missingness is completely random.
One-hot encoding 500 categories creates 500 columns, which is often manageable with modern compute if you have enough data. To reduce dimensionality: use target encoding (replace each category with the mean target for that category, applied with cross-validation to avoid leakage), group rare categories into an 'Other' bucket, or use feature hashing. For neural networks, embedding layers are ideal. In scikit-learn, you can combine thresholding with OneHotEncoder: set min_frequency=0.05 to group rare categories.
Data leakage occurs when information from outside the training set influences the model — most commonly when preprocessors (imputers, scalers, encoders) are fit on the entire dataset before splitting. The fix: always call train_test_split first, then fit preprocessors only on X_train. Using Pipeline ensures this separation. Another form of leakage is target encoding using the full dataset — apply target encoding only within cross-validation folds.
Outlier detection and treatment should happen before scaling, because scaling can hide outliers (especially with StandardScaler where they skew the mean). Detect outliers on raw data, then decide: remove if you're sure it's an error; cap if it's extreme but real; then scale using RobustScaler if outliers remain. If you cap, do so before scaling so the boundaries are based on raw values.
LabelEncoder is designed for encoding the target variable (y) — it transforms a single column of labels to integers 0..n_classes-1. OrdinalEncoder is for input features (X) and can handle multiple columns with explicit ordering. Using LabelEncoder on input features is a common mistake because it alphabetically orders categories, implying a false ordinal relationship. Use LabelEncoder only for the target, OrdinalEncoder (with categories parameter) for ordinal features.
Yes, but it may not produce good results. StandardScaler assumes the data is roughly bell-shaped. If your data is highly skewed, StandardScaler will still center and scale it, but the resulting distribution will still be skewed and outliers can distort the mean and standard deviation. For skewed data, apply a log or Box-Cox transformation first, then scale. Or use RobustScaler which is less sensitive to distribution shape.
Use a scikit-learn Pipeline serialized with joblib or pickle. Never replicate preprocessing steps manually in a new script. Save the fitted pipeline after training: joblib.dump(pipeline, 'model_pipeline.pkl'). In production, load it back: pipeline = joblib.load('model_pipeline.pkl'). This guarantees identical transformations. Also pin library versions (scikit-learn, pandas, numpy) in your deployment environment.
fit() learns the parameters from the data (e.g., mean and std for StandardScaler). transform() applies the learned transformation to new data (e.g., standardizes values using the learned mean/std). fit_transform() is a convenience method that calls fit() then transform() on the same data — used on training data. Never call fit() on test data; only transform() using the parameters learned from training.
20+ years shipping production ML systems and the infrastructure behind them. Drawn from code that ran under real load.
That's ML Basics. Mark it forged?
10 min read · try the examples if you haven't