Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Classification lessons
Probabilities and neighbours · ML-C09 · 12–18 MIN

Validate k on a meaningful scale

Choose k with fold-local scaling.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeCompute and inspect the evidence
  3. TransferCompute and inspect the evidence

Understand the idea

Validation rows must not become training neighbours. Scaling belongs inside the candidate pipeline so each fold learns its own metric scale.

Illustrative data

Illustrative data
ColumnValues
Class An2: (0.5, 0.2); n3: (0.4, 0.5); n6: (1.5, 1.2); n7: (-1.5, -0.2); n9: (2.9, 0.9)
Class Bn1: (0.3, 0.2); n4: (1, 0.4); n5: (-0.9, 0.5); n8: (2.4, 0)

Click the highlighted code to change the result.

example_model = Pipeline([
    ("prepare", StandardScaler()),
    ("model", KNeighborsClassifier(n_neighbors=)),
])
example_model.fit(example_X, example_y)
example_model.predict(pd.DataFrame({"x": [], "y": []}))
Query (0, 0): 2 A + 1 B → A. Neighbours: n1, n2, n7.Training-SD units · ○ A □ B-2-10123y / SD = 1n1: (0.3, 0.2), class Bn1n2: (0.5, 0.2), class An2n3: (0.4, 0.5), class An3n4: (1, 0.4), class Bn4n5: (-0.9, 0.5), class Bn5n6: (1.5, 1.2), class An6n7: (-1.5, -0.2), class An7n8: (2.4, 0), class Bn8n9: (2.9, 0.9), class An9Query: (0, 0)3 neighbours: 2 A + 1 B → A

k=3: 2 A + 1 B → A; nearest rows n1, n2, n7. Distances use training standard deviations.

Supplied for this practice

These objects are prepared afresh before your editor runs.

X
Feature dataframe for the declared population, preserving row indices.
X = df[['length', 'width']]
y
Target series, aligned with X.
y = df.label
X_train / X_test / y_train / y_test
Training feature rows from the declared split. Final-test feature rows from the declared split. Training targets, aligned with X_train. Final-test targets, aligned with X_test.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)

Outputs you create

answer
  1. The requested result, with the values, labels and row order described by this task.
CLASS180 · first 8 rows
lengthwidthlabel
236.033-0.805568A
581.2970.728558A
-1511.27-1.00866A
99.0248-0.24496A
-13.0141-0.660765A
681.1790.602475A
51.14720.873157A
362.131-0.665605A
Your task
  1. Search production k values 3,5,9 on CLASS180.
answer
Hints

Think: Scaling must be learned separately inside each validation fit.

Tools: Pipeline, GridSearchCV and model__n_neighbors.

Approach: Compare the declared k values in a scaling/KNN pipeline using seeded stratified macro-F1 folds.

Explained solution
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV,StratifiedKFold
model=Pipeline([('scale',StandardScaler()),('model',KNeighborsClassifier())])
search=GridSearchCV(model,{'model__n_neighbors':[3,5,9]},cv=StratifiedKFold(5,shuffle=True,random_state=42),scoring='f1_macro').fit(X_train,y_train)
answer=search.cv_results_['mean_test_score']

Fold-local scaling keeps validation observations from changing distances used to fit their own predictor.

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.