Theory of Intelligence
4.4 Prepare the Data•Concept 1 of 5

Introduction

lightbulb

Book reference: Géron, Hands-On Machine Learning, Ch. 2 ("Prepare the Data for Machine Learning Algorithms"). Written in our own words.

Most ML algorithms are fussy eaters. They want numbers only, no missing values, and often features on similar scales. Real data rarely look like that. Preparing the data — often called preprocessing — is where a big share of real project time goes.

A key principle: write the preparation steps as code, not as manual edits. Then you can:

  • apply exactly the same steps to the test set and to future data in production;
  • try different options easily (e.g. median vs mean filling);
  • rerun everything when new data arrive.

In scikit-learn, each preparation step is a transformer: an object that learns something from the training data with fit() (like a median) and applies it with transform(). Many have fit_transform() to do both at once.

First, separate the features from the labels:

PYTHON
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split

df = fetch_california_housing(as_frame=True).frame
train_set, test_set = train_test_split(df, test_size=0.2, random_state=42)
X_train = train_set.drop(columns="MedHouseVal")   # features
y_train = train_set["MedHouseVal"].copy()         # labels

Discuss theoretical concepts, mathematical formulas, and architectural foundations.

Supports inline code with `backticks`
No comments yet. Be the first to start the discussion!