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:
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
MediumA 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.
- Replace it with a pipeline, a scaler then k-NN, fitted on the training rows.
- Print its test accuracy and judge the forecast with it.
Another company, day 20, 32, 7000, 9:
Test accuracy: 0.880
Flying day: noTry 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")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Supervised Learning
1Your First Model
Learning from ExamplesTrain and Test SetsFit and PredictScoring a ModelRecap: Seed Sorter2Nearest Neighbors
How k-NN DecidesChoosing kDecision BoundariesScaling for NeighborsRecap: Wine Neighbors5Measuring Classifiers
The Confusion MatrixPrecision and RecallThe F1 ScoreImbalanced ClassesROC CurvesRecap: Screening Test11Project: Penguin Species
Meet the PenguinsPrepare the DataChoose a ModelTune the WinnerShip the ModelPractice on your own: Online Python compiler