Understand the idea
KNN stores training examples. To predict, find nearby examples and let their labels vote. k changes how local that vote is. Distance depends on measurement units, so the familiar Pipeline learns an input scale from training rows.
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.
What the syntax does
KNeighborsClassifier(n_neighbors=3)- Creates a K-nearest neighbours classifier.
n_neighbors:1,3,5,9
n_neighborsis a positive integer no larger than the fitting-row count; the sklearn default is 5.- The first example deliberately uses 3.
- Small k can follow noise; larger k pools a wider region.
- Choose using training validation with fold-local scaling.
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 |
| 2051.15 | 1.87316 | B |
| 2362.13 | 0.334395 | B |
| 2285.63 | 0.257253 | B |
| 2680.44 | 0.961328 | B |
| 1856.81 | 0.472554 | B |
| 2946.98 | 0.880302 | B |
| -331.781 | 2.72724 | C |
| 412.325 | 3.28307 | C |
| 319.701 | 3.33371 | C |
| 1658.91 | 2.68519 | C |
| -396.782 | 2.36965 | C |
| 477.136 | 3.8745 | C |
- Fit a scaled three-neighbour classifier on
X_trainandy_train. - Store its predicted class for the first test row in
answer.
answerHints
Think: Nearby fitted training labels vote; scaling makes the two measurement units comparable.
Tools: Pipeline, StandardScaler, KNeighborsClassifier(n_neighbors=3), fit and predict.
Approach: Build the named preparation/model recipe, fit only training rows and predict the first held-away row.
Explained solution
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
model=Pipeline([('prepare',StandardScaler()),('model',KNeighborsClassifier(n_neighbors=3))])
model.fit(X_train,y_train)
answer=model.predict(X_test.iloc[:1])
Three nearby training examples vote for a class. The Pipeline learns the distance scale on fitting rows and reuses it when predicting. This introductory round needs only fit and predict; the next round retrieves the actual neighbours and counts their labels.