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
| Column | Values |
|---|---|
| Class A | n2: (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 B | n1: (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": []}))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 withX_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- The requested result, with the values, labels and row order described by this task.
length | width | label |
|---|---|---|
| 236.033 | -0.805568 | A |
| 581.297 | 0.728558 | A |
| -1511.27 | -1.00866 | A |
| 99.0248 | -0.24496 | A |
| -13.0141 | -0.660765 | A |
| 681.179 | 0.602475 | A |
| 51.1472 | 0.873157 | A |
| 362.131 | -0.665605 | A |
- Search production k values 3,5,9 on CLASS180.
answerHints
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.