EMBEDLIX RESEARCH · MACHINE LEARNING LABORATORY SERIES
Classical learning: KNN and Decision Trees
Recording-aware cross-validation, feature scaling, search settings and the selected classical baseline.
2.1 Why begin with a classical baseline?
A baseline establishes what can be learned from the six raw channels before introducing a neural network. KNN compares a new standardized row with stored training rows and combines nearby class labels. A Decision Tree follows learned feature thresholds. Both classify one row at a time; neither explicitly retains the order of a walking cycle. [1,3]
For Euclidean distance between six standardized features:
The selected KNN uses 11 neighbours, distance weighting and Euclidean distance, with StandardScaler. This replaces the earlier portfolio summary of nine neighbours and Manhattan distance: the supplied final logbook and notebook identify the revised configuration.
Distance weighting gives nearby examples more influence. K is a bias–variance choice, not a universal optimum: a tiny neighbourhood may follow noise, while a large one can blur class boundaries.
2.2 Initial search and the role of scaling
| Grade 3 model | Best setting | Mean grouped CV | Fold SD |
|---|---|---|---|
| KNN, unscaled | k = 5 | 91.44% | 2.44 points |
| KNN, standardized | k = 7 | 93.42% | 1.85 points |
| Decision Tree | entropy; depth unrestricted | 89.44% | 2.28 points |
The initial KNN search tested odd k values from 1 to 15. The tree compared maximum depth 3, 5, 7, 10 and unrestricted, using gini and entropy. Tree depth mattered more than the split criterion. The strongest unrestricted tree reached 100% training accuracy, a warning that training performance alone can hide overfitting.
Standardization raised the best KNN validation mean and reduced fold variation. Acceleration values otherwise have larger numerical ranges than angular velocity. Trees do not use distances, and scaling preserves feature ordering; their splits therefore do not require the same standardization strategy.
The Grade 3 standardized KNN achieved 91.04% on the same-person test. This result was later inspected again after the extended search.
2.3 Extended model search and stability
| Model | Selected extended configuration | Mean CV | Fold SD |
|---|---|---|---|
| Standardized KNN | k = 11; distance; Euclidean | 93.62% | 1.87 points |
| Decision Tree | entropy; depth 20; split 2; leaf 1 | 89.55% | 2.24 points |
KNN tested k = 3, 5, 7, 9, 11; uniform/distance weighting; and Euclidean/Manhattan distance. The tree tested depths 10, 15, 20 and unrestricted, minimum split sizes 2, 5, 10, and leaf sizes 1, 2, 5.
Extended KNN fold scores were 94.18%, 94.06%, 93.91%, 90.15% and 95.82%; the tree scores were 90.37%, 89.89%, 90.39%, 85.26% and 91.82%. Fold 4 was difficult for both models, suggesting a challenging set of recordings rather than a weakness unique to one algorithm. These fold SDs are descriptive variation, not confidence intervals.
The k = 11 result was only about 0.01 percentage points above a leading k = 7 configuration. Many settings were within 0.2 points. The evidence supports KNN over the tree more clearly than it establishes one neighbour count as uniquely optimal.
2.4 Implementing the selected pipeline
The following compact example reproduces the final configuration but is a teaching reconstruction, not a complete executable export. X_train, y_train, groups_train and cv come from the recording-aware development data. The full submitted notebook is linked in the reproducibility chapter.
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score
pipeline = Pipeline([
("scale", StandardScaler()),
("knn", KNeighborsClassifier(
n_neighbors=11, weights="distance", metric="euclidean"
)),
])
scores = cross_val_score(
pipeline, X_train, y_train,
groups=groups_train, cv=cv, scoring="accuracy"
)
pipeline.fit(X_train, y_train)
y_pred = pipeline.predict(X_test)
The pipeline prevents scaling from being fitted on a validation fold before that fold is evaluated. groups must be supplied to the grouped splitter. The original fold check below explicitly compares recording identity sets.
Original notebook cell · dependencies described above
for fold_number, (train_index, validation_index) in enumerate(
cv.split(X_train, y_train, groups_train),
start=1
):
fold_train_ids = set(groups_train.iloc[train_index])
fold_validation_ids = set(groups_train.iloc[validation_index])
fold_overlap = fold_train_ids & fold_validation_ids
print(
f"Fold {fold_number}: "
f"{len(fold_train_ids)} training recordings, "
f"{len(fold_validation_ids)} validation recordings, "
f"overlap = {fold_overlap}"
)
2.5 Final classical results and errors
The selected KNN reached 91.12% test accuracy on 16,042 same-person rows, 2.50 percentage points below its grouped-CV mean. No final held-out Decision Tree test score was reported; the tree’s 89.55% must not be presented as test accuracy.
| Activity | Precision | Recall | F1 |
|---|---|---|---|
| Walking | 89.72% | 82.87% | 86.16% |
| Running | 93.83% | 97.16% | 95.47% |
| Jumping | 95.87% | 89.78% | 92.73% |
| Forward/backward | 85.71% | 94.66% | 89.96% |
Walking produced 440 rows predicted as forward/backward. The difficult walk_1 recording had 75.32% accuracy and jump_1 had 85.89%. A single instant can resemble another activity even when the full signal has a recognizable rhythm. This motivates window features or sequence models rather than simply enlarging the parameter search.
The original side-by-side matrices also show the external-person failure: 91.12% falls to 37.94%. Matrix label ordering differs from the publication’s standard walk/run/jump/forward-backward order; retain the original axis labels when reading this source image.