Sub topic 4
Min-max scaling to a fixed range
This chapter assumes you know basic NumPy array operations, the scikit-learn fit and transform pattern, and the idea of a training set and a test set. You will compute min-max scaling by hand, reproduce it with MinMaxScaler, and learn what happens when new data falls outside the range seen during fitting.
The idea
Min-max scaling rescales each column linearly so that its smallest value becomes 0 and its largest becomes 1. The relative spacing between values stays the same. Only the units change.
For one column, the formula is:
x_scaled = (x - x_min) / (x_max - x_min)To land in another range [a, b], stretch and shift the result:
x_final = x_scaled * (b - a) + aEach column is processed independently, so a column measured in thousands and a column measured in fractions both end up on the same interval.
Computing it by hand
This NumPy version works column by column using axis=0.
import numpy as np
X = np.array([[1.0, 200.0],
[2.0, 400.0],
[3.0, 600.0]])
col_min = X.min(axis=0)
col_max = X.max(axis=0)
mm = (X - col_min) / (col_max - col_min)
print(mm)
a, b = -1, 1
scaled = mm * (b - a) + a
print(scaled)Both columns map to 0, 0.5, 1, even though their original units differ by a factor of 200. The second print shows the same data on [-1, 1], which is -1, 0, 1.
Using MinMaxScaler
MinMaxScaler does the same work and remembers the statistics it learned. Its feature_range parameter sets the target interval [a, b].
import numpy as np
from sklearn.preprocessing import MinMaxScaler
X = np.array([[1.0, 200.0],
[2.0, 400.0],
[3.0, 600.0]])
scaler = MinMaxScaler(feature_range=(-1, 1))
X_scaled = scaler.fit_transform(X)
print(X_scaled)
print(scaler.data_min_, scaler.data_max_)
print(scaler.scale_, scaler.min_)fit learns data_min_ and data_max_ for each column. Internally, scikit-learn turns the formula into one multiply and one add. It stores scale_ = (b - a) / (data_max_ - data_min_) and min_ = a - data_min_ * scale_, and transform computes X * scale_ + min_. Note that min_ is an offset, not the data minimum.
| Attribute | Meaning |
|---|---|
| `data_min_` | Per-column minimum seen during `fit` |
| `data_max_` | Per-column maximum seen during `fit` |
| `data_range_` | `data_max_ - data_min_` |
| `scale_` | Per-column multiplier applied by `transform` |
| `min_` | Per-column offset added by `transform` |
Values outside the fitted range
The minimum and maximum come from the data you fit on. New data can fall outside them, and scikit-learn does not stop it. Out-of-range values land outside [a, b].
import numpy as np
from sklearn.preprocessing import MinMaxScaler
X_train = np.array([[0.0], [5.0], [10.0]])
X_new = np.array([[15.0], [-5.0]])
scaler = MinMaxScaler().fit(X_train)
print(scaler.transform(X_new).ravel())
clipped = MinMaxScaler(clip=True).fit(X_train)
print(clipped.transform(X_new).ravel())The first print shows 1.5 and -0.5. With clip=True, the second print shows 1.0 and 0.0, because the output is forced into the target range.
Behavior to watch for
- Outliers set the range. One extreme value becomes the min or max, and every other value is squeezed into a narrow band. A later chapter compares this with standardization.
- The mean is not centered. The scaled values have no particular mean. They are only bounded.
- Constant columns. If
x_max == x_min, the range is zero. scikit-learn avoids dividing by zero and maps the column to the lower bounda. - Fit on training data only. The minimum and maximum are statistics learned from data, so fitting on the full dataset leaks test information. The next chapters cover this.
Common mistakes
- Calling
fit_transformon the test set. This relearns the min and max from test data, so train and test values are no longer on the same scale. - Assuming the output is always in
[0, 1]. This holds only for the fitted data, unless you setclip=True. - Passing a 1D array.
MinMaxScalerexpects 2D input of shape(n_samples, n_features). Usex.reshape(-1, 1)for a single column.
Exercise
A MinMaxScaler with the default range is fitted on the training column [20, 30, 40, 60]. What does transform return for the new value 80, and what would it return with clip=True?
Show answer
The training minimum is 20 and the maximum is 60, so the range is 40. Without clipping, (80 - 20) / 40 = 1.5. With clip=True, the result is limited to the upper bound, so it is 1.0.