CodeBlanks Logo Icon CodeBlanks

Training model

Train a KNN model.

A model with a pipeline

Instead of managing scaling and the model separately, we make a single object with make_pipeline. The fit call learns the scaler only on training data; predict automatically applies the same transformation to the test data:

from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
model = make_pipeline(StandardScaler(), KNeighborsClassifier(n_neighbors=5))
model.fit(X_train, y_train)

That way the test data's mean can't leak into training; data leakage is prevented structurally.

Unscaled versus scaled

To see the effect of scaling, also train the same KNN without scaling:

ham = KNeighborsClassifier(n_neighbors=5).fit(X_train, y_train)
print(ham.score(X_test, y_test))     # proline dominates: low
print(model.score(X_test, y_test))   # scaled: clearly higher

Unscaled KNN in practice looks only at the feature with the largest units (proline); scaling lets all features vote.

Practice Train the model

Train an unscaled KNeighborsClassifier(n_neighbors=5) and a make_pipeline(StandardScaler(), KNeighborsClassifier(n_neighbors=5)). Print the test accuracy of both with round(..., 3) on separate lines.

Starter code

model.py
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split

X, y = load_wine(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y)
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

# buraya yaz

Sample solution

model.py
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split

X, y = load_wine(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y)
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

ham = KNeighborsClassifier(n_neighbors=5).fit(X_train, y_train)
model = make_pipeline(StandardScaler(), KNeighborsClassifier(n_neighbors=5)).fit(X_train, y_train)
print(round(ham.score(X_test, y_test), 3))
print(round(model.score(X_test, y_test), 3))

Expected output

0.778
0.933

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does the scaler do when <code>predict(X_test)</code> is called on a pipeline?

  • It errors
  • It transforms with the mean and std learned in training Correct answer
  • It refits on the test data
  • It does nothing

Explanation: In the test phase the pipeline only calls transform.

2 Combine the scaler and the model in one object.

Question
model = ____(StandardScaler(), KNeighborsClassifier())
Answer
model = make_pipeline(StandardScaler(), KNeighborsClassifier())

Explanation: make_pipeline links the steps in order and names them automatically.

3 Match the steps.

Term Meaning
make_pipeline Chains the steps
StandardScaler Mean 0, std 1
model.fit Learns scale and model
ham KNN Large unit dominates

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation