Data Preparation
Min-Max Scaling
Scale a feature list to [0,1] via (x − min)/(max − min). Uses min() and
max() as primitives to isolate the scaling formula; a single loop applies
the transform. Library: sklearn.preprocessing.MinMaxScaler().fit_transform(X)
on a column-shaped 2D array; .ravel().tolist() extracts a flat list. RESULT:
scaled list (rounded).
By hand
With scikit-learn
MinMaxScaler().fit_transform(X) returns a 2D array; .ravel().tolist()
flattens it. The snapshot shows the learned min and max for verification.
naive.py
data = [10, 20, 30, 40, 50]
mn = min(data)
mx = max(data)
rng = mx - mn
scaled = []
for v in data:
scaled.append(round((v - mn) / rng, 4))
print('RESULT:', scaled)
library.py
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from dalib.display import set_display
set_display()
data = [10, 20, 30, 40, 50]
X = np.array(data).reshape(-1, 1)
scaler = MinMaxScaler()
scaled = [round(v, 4) for v in scaler.fit_transform(X).ravel().tolist()]
print('min:', float(scaler.data_min_[0]))
print('max:', float(scaler.data_max_[0]))
print('RESULT:', scaled)
min: 10.0
max: 50.0
RESULT: [0.0, 0.25, 0.5, 0.75, 1.0]
Implementation notes
- MinMaxScaler applies exactly (x − min)/(max − min), matching the naive formula precisely.
- Sensitive to outliers: a single extreme value shifts min or max, compressing all other scaled values toward the center.
reshape(-1, 1)is required because sklearn expects a 2D feature matrix (n_samples × n_features); a 1D list is ambiguous.- Cross-reference:
standardize-features(this chapter) for the mean/std alternative that is less sensitive to outliers.