CodeBlanks Logo Icon CodeBlanks

Summary - Scaling

In this group you learned StandardScaler, MinMaxScaler, and Pipeline.

Summary: scaling

In this group:

  • Features in different units break distance- and gradient-based models; trees aren't affected.
  • StandardScaler (z-score), MinMaxScaler ([0, 1]), RobustScaler (median/IQR).
  • The scaler is fit only on training data; inverse_transform converts back.
  • A Pipeline combines preprocessing and the model, prevents leakage and is tuned with step__parameter.

Practice Choosing a scaler

For x, which contains an outlier, print the MinMaxScaler and RobustScaler results with ravel().round(2) on separate lines.

Starter code

model.py
import numpy as np
from sklearn.preprocessing import MinMaxScaler, RobustScaler

x = np.array([[10.0], [12.0], [11.0], [13.0], [90.0]])

# buraya yaz

Sample solution

model.py
import numpy as np
from sklearn.preprocessing import MinMaxScaler, RobustScaler

x = np.array([[10.0], [12.0], [11.0], [13.0], [90.0]])

print(MinMaxScaler().fit_transform(x).ravel().round(2))
print(RobustScaler().fit_transform(x).ravel().round(2))

Expected output

[0.   0.03 0.01 0.04 1.  ]
[-1.   0.  -0.5  0.5 39. ]

Questions and answers

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

1 Which causes data leakage?

  • Fitting the scaler on X_train
  • Training a tree model without scaling
  • Fitting the scaler on all data and splitting afterwards Correct answer
  • Using a pipeline

Explanation: The test set's statistics mix into the scale; the score comes out optimistic.

2 Create a pipeline that names the steps automatically.

Question
pipe = ____(StandardScaler(), LogisticRegression())
Answer
pipe = make_pipeline(StandardScaler(), LogisticRegression())

Explanation: make_pipeline uses the lowercase class names as step names.

3 Match the formulas.

Term Meaning
StandardScaler (x - mean) / std
MinMaxScaler (x - min) / (max - min)
RobustScaler (x - median) / IQR
inverse_transform Back to original unit

All lessons in Beginner

13 Project: Classification

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