Introduction
We have a framed problem, a clean training set and a preprocessing pipeline. Now comes the part most people think of as "machine learning": training models. It is actually the easy part — thanks to all the preparation, each model is just a few lines of code.
The process:
- Train a few quick models and compare them fairly with cross-validation.
- Shortlist the 2–5 most promising.
- Fine-tune their hyperparameters.
- Evaluate the final model once on the test set.
PYTHON
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
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, y_train = train_set.drop(columns="MedHouseVal"), train_set["MedHouseVal"]
X_test, y_test = test_set.drop(columns="MedHouseVal"), test_set["MedHouseVal"]