Model Workflow
Fit-Predict Pipeline
Chain min-max scaling and 1-nearest-neighbor classification by hand. Scale training features using train min/max; scale the query identically; find the nearest scaled training point and return its label. Library: sklearn Pipeline with MinMaxScaler and KNeighborsClassifier(n_neighbors=1). RESULT: predicted label.
By hand
With scikit-learn
make_pipeline(MinMaxScaler(), KNeighborsClassifier(n_neighbors=1)) chains
scaling and classification. fit applies scaling to training data and trains
the classifier on scaled features; predict scales new data with the same
parameters before classifying.
naive.py
x_train = [0, 2, 4, 8]
y_train = ['A', 'A', 'B', 'B']
query = 5
x_min = min(x_train)
x_max = max(x_train)
x_range = x_max - x_min
x_scaled = []
for x in x_train:
x_scaled.append((x - x_min) / x_range)
q_scaled = (query - x_min) / x_range
best_i = 0
best_d = (q_scaled - x_scaled[0]) ** 2
for i in range(1, len(x_scaled)):
d = (q_scaled - x_scaled[i]) ** 2
if d < best_d:
best_d = d
best_i = i
pred = y_train[best_i]
print('RESULT:', repr(pred))
library.py
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import MinMaxScaler
from sklearn.neighbors import KNeighborsClassifier
from dalib.display import set_display
set_display()
X_train = [[0], [2], [4], [8]]
y_train = ['A', 'A', 'B', 'B']
pipe = make_pipeline(MinMaxScaler(), KNeighborsClassifier(n_neighbors=1))
pipe.fit(X_train, y_train)
scaler = pipe.named_steps['minmaxscaler']
x_sc = [round(float(v), 4) for v in scaler.transform(X_train).flatten()]
q_sc = round(float(scaler.transform([[5]])[0][0]), 4)
print('x_scaled:', x_sc)
print('q_scaled:', q_sc)
print('RESULT:', repr(str(pipe.predict([[5]])[0])))
x_scaled: [0.0, 0.25, 0.5, 1.0]
q_scaled: 0.625
RESULT: 'B'
Implementation notes
- Scaling at fit time uses train min/max only; those same parameters apply to new data at predict time. Fitting the scaler on train+query together would leak query information into training (data leakage). The pipeline enforces this automatically.
(q_scaled - x_scaled[i])**2is squared distance; for 1-NN with a single feature it is equivalent toabs(q_scaled - x_scaled[i]). Cross-reference:euclidean-distance(ch02) for the multi-feature form.- Capstone ties together:
min-max-scale(ch01) for the preprocessing step,knn-classify-majority(ch02) for the classification step, andcross-val-fold-manual/compare-two-model-scores(this chapter) for how a fitted pipeline fits into a validation workflow. - Same links as pages: min-max scale, KNN classify majority, manual cross-validation fold, and compare two model scores. This page consumes those sibling mechanics instead of re-teaching each one.