Machine Learning

Data Preparation For ML

From Raw Data to Model-Ready Data

Every algorithm from Module 3 onward expects input in a very specific shape: a numeric table (typically a matrix X of features and a vector y of labels), with n

JrCodex·5 min read

Jr Codex ML Notes

Level: Beginner–Intermediate Prerequisites: Module 1: Foundations of Machine Learning Time to complete: ~20 minutes


Table of Contents

  1. What "Model-Ready" Actually Means
  2. Why Algorithms Need Numbers
  3. The Preprocessing Pipeline, End to End
  4. Handling Missing Data — an ML-Specific Recap
  5. Building a Preprocessing Pipeline in scikit-learn
  6. Summary & Next Steps

1. What "Model-Ready" Actually Means

Every algorithm from Module 3 onward expects input in a very specific shape: a numeric table (typically a matrix X of features and a vector y of labels), with no missing values, no text categories left unencoded, and features on comparable scales. Getting from raw, real-world data to this state is the subject of this entire module.

Raw Data                              Model-Ready Data
─────────────────────────────         ─────────────────────────────
  {"city": "Boston",                    [1, 0, 0, 0,   0.42,  0.31]
   "income": 65000,                       (one-hot       (scaled  (scaled
   "age": 34,                              city)          income) age)
   "signup_date": "2024-01-15"}

  Mixed types, missing values,           Pure numbers, no missing
  inconsistent scales, text                values, comparable scales
─────────────────────────────         ─────────────────────────────

2. Why Algorithms Need Numbers

Every ML algorithm, underneath its specific mathematics (covered starting in Module 3), ultimately performs arithmetic — computing distances, dot products, sums of squared errors, or splits based on numeric thresholds. Text categories, dates, and missing values have no inherent mathematical meaning until they're converted.

# This is EXACTLY why:
#   "Boston" can't be fed directly into a distance calculation
#     (Module 4's KNN needs to compute NUMERIC distances between points)
#   A missing value can't be summed or averaged
#     (Module 3's linear regression needs a real NUMBER for every feature)
#   A date string like "2024-01-15" isn't directly usable
#     (needs to become a number, or several numbers — e.g. day-of-week,
#      days-since-reference-date, echoing the Data Science Notes'
#      feature engineering chapter)

3. The Preprocessing Pipeline, End to End

The Standard Order of Operations
─────────────────────────────────────────
  1. Handle missing values         (Section 4)
  2. Encode categorical variables    (Chapter 2)
  3. Engineer new features             (Chapter 4)
  4. Scale numeric features               (Chapter 3)
  5. Select/reduce features                  (Chapter 4)
  6. Address class imbalance                    (Chapter 5,
                                                   classification only)
─────────────────────────────────────────

This order matters — encoding categorical variables before handling their missing values, for instance, can silently create a spurious "missing" category that behaves inconsistently; scaling before encoding doesn't make sense at all, since encoded categorical columns often shouldn't be scaled the same way as genuinely continuous ones. Each chapter in this module covers one step in depth.


4. Handling Missing Data — an ML-Specific Recap

The Data Science Notes' Module 2, Chapter 4 covered missing data deletion and imputation strategy in depth (MCAR/MAR/MNAR, group-aware imputation) — that content applies directly here. Worth restating the ML-specific stakes:

import pandas as pd
from sklearn.impute import SimpleImputer
 
df = pd.DataFrame({
    "age": [25, None, 35, 28],
    "income": [50000, 62000, None, 71000],
})
 
# Simple imputation — fine as a baseline, always fit ONLY on training data
# (Module 1, Chapter 5's leakage warning applies directly here)
imputer = SimpleImputer(strategy="median")
df_imputed = pd.DataFrame(imputer.fit_transform(df), columns=df.columns)
print(df_imputed)
Why This Matters MORE for ML Than for General Analysis
─────────────────────────────────────────
  A Data Science EDA report can simply NOTE that 15% of a
  column is missing and discuss it qualitatively.

  An ML algorithm CANNOT run at all with missing values present
  (most implementations will raise an error outright) — a
  concrete DECISION about imputation is mandatory, not optional,
  before Module 3's algorithms can even be trained.
─────────────────────────────────────────

5. Building a Preprocessing Pipeline in scikit-learn

Rather than applying each transformation as a separate manual step (error-prone, and easy to accidentally leak test-set information per Module 1, Chapter 5), scikit-learn's Pipeline bundles preprocessing and modeling into one consistent, leak-safe object.

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
 
numeric_features = ["age", "income"]
categorical_features = ["city"]
 
numeric_transformer = Pipeline(steps=[
    ("imputer", SimpleImputer(strategy="median")),
    ("scaler", StandardScaler()),                          # Chapter 3
])
 
categorical_transformer = Pipeline(steps=[
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("encoder", OneHotEncoder(handle_unknown="ignore")),      # Chapter 2
])
 
preprocessor = ColumnTransformer(transformers=[
    ("num", numeric_transformer, numeric_features),
    ("cat", categorical_transformer, categorical_features),
])
 
full_pipeline = Pipeline(steps=[
    ("preprocessor", preprocessor),
    ("classifier", LogisticRegression()),      # Module 4
])
 
# full_pipeline.fit(X_train, y_train)     — fits EVERYTHING correctly,
#                                             with no leakage, in one call
# full_pipeline.predict(X_test)              — applies the SAME fitted
#                                                transformations to new data
Why a Pipeline Object Is the Professional Standard
─────────────────────────────────────────
  - GUARANTEES preprocessing is fit ONLY on training data
    (directly enforces Module 1, Chapter 5's anti-leakage rule)
  - Bundles preprocessing + model into ONE object — easier to
    save, deploy, and reuse consistently (a direct preview of
    Module 9's production concerns)
  - Makes cross-validation (Module 1, Ch.5) trivially correct —
    each fold refits the ENTIRE pipeline independently
─────────────────────────────────────────

This Pipeline structure is the backbone every remaining chapter in this module builds on — Chapters 2-5 each fill in one specific transformer used within it.


6. Summary & Next Steps

Key Takeaways

  • "Model-ready" data means a fully numeric table with no missing values, encoded categorical variables, and comparably scaled features — every algorithm in this curriculum assumes this shape.
  • The preprocessing pipeline has a standard order: handle missing values, encode categoricals, engineer features, scale, select features, then address class imbalance if needed.
  • Missing data handling is mandatory for ML (algorithms typically error out on missing values), not just a qualitative EDA note.
  • scikit-learn's Pipeline and ColumnTransformer bundle preprocessing and modeling into one leak-safe object — the professional standard over manual, error-prone step-by-step transformation.

Concept Check

  1. Why can't a categorical column like "city" be fed directly into most ML algorithms?
  2. Why does the order of preprocessing steps (missing values before encoding, etc.) matter?
  3. What specific problem does wrapping preprocessing and modeling in a single Pipeline object solve?

Next Chapter

Chapter 2: Encoding Categorical Variables


Jr Codex — 1-on-1 Personalized Coaching | Back to Module Index