Menu
CoddyTech

Pipelines

Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 45 of 58.

Real tables arrive with gaps, words and mixed scales. This chapter builds one object that cleans them and predicts, a pipeline:

A diagram titled Raw columns in, predictions out. Numbers with gaps go down one lane, SimpleImputer fills the gaps, then StandardScaler. Words go down another, OneHotEncoder turns them into 0s and 1s. Both lanes meet at the model. A dashed box around the steps reads one pipeline: fit it, then predict

Start small: make_pipeline chains a scaler and a model:

from sklearn.pipeline import make_pipeline

pipe = make_pipeline(StandardScaler(), KNeighborsClassifier())
pipe.fit(X_train, y_train)
print(pipe.score(X_test, y_test))

For 200 test welds, strong or weak, this prints 0.86, against 0.74 unscaled.

fit learns the scaling from the training rows, then trains. Every later row gets scaled the same way, automatically.

challenge icon

Challenge

Medium

A balloon company decides each evening if tomorrow is a flying day. The starter scales by hand: the test accuracy is fine, but the forecast is never scaled, so it says yes to any weather.

  1. Replace it with a pipeline, a scaler then k-NN, fitted on the training rows.
  2. Print its test accuracy and judge the forecast with it.

Another company, day 20, 32, 7000, 9:

Test accuracy: 0.880
Flying day: no

Try it yourself

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline

# Past days at a balloon company: a flying day (1) or not (0)
days = pd.read_csv("balloons.csv")
X = days[["wind_kmh", "gusts_kmh", "visibility_m", "passengers"]].to_numpy()
y = days["flying_day"].to_numpy()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)

# Tomorrow's forecast: wind, gusts, visibility, passengers booked
day = np.array([[float(input()), float(input()), float(input()), float(input())]])

# Scaling by hand: the score looks fine, but the forecast is never scaled
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
model = KNeighborsClassifier()
model.fit(X_train_scaled, y_train)
print(f"Test accuracy: {model.score(X_test_scaled, y_test):.3f}")
if model.predict(day)[0] == 1:
    print("Flying day: yes")
else:
    print("Flying day: no")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Supervised Learning

Practice on your own: Online Python compiler