100 beginner machine learning projects with runnable Python code, tests and honest case studies. By Networkers Home, Bangalore.
This small project teaches the K-nearest neighbours (KNN) idea with a hands-on comparison: you will build a tiny numeric dataset that represents item descriptors, implement a simple neighbour search from scratch, and compare that implementation to scikit-learn’s KNeighborsClassifier. The exercise emphasises why feature scaling matters, how distance choice affects matches, and how to select k using only training folds. Finally, it inspects misleading nearest neighbours for a few held-out examples.
The data is synthetic and created with scikit-learn’s make_blobs to simulate numeric item descriptors and discrete classes (a toy product-similarity scenario). This is intentionally small and structured so results are easy to interpret; it is not real product data.
Run the project script directly with Python 3.11+. The script exposes a run(seed: int = 42) function. If executed as a program it will print JSON with the reported metrics.
Automated tests (pytest) import run from the project and verify the output keys, sensible metric ranges, a conservative accuracy floor on the test split, and determinism when run with the same seed.
Measured by running python project.py (seed 42) on 2026-10-23; the tests in test_project.py assert the floor.
| Metric | Value |
|---|---|
| accuracy | 1 |
| best_k | 1 |
| cv_best_score | 1 |
| hand_knn_accuracy | 1 |
| misleading_nearest_count | 0 |
| n_test | 90 |
Read the
accuracy,cv_best_score,hand_knn_accuracyvalue with care: the data here is small and synthetic with a planted, learnable structure, so a near-perfect score shows the pipeline works, not that the method would reach this on real data. See Limitations.
This is a toy example: the dataset is small and synthetic to keep the code short and deterministic. The hand-built neighbour search is O(n_test * n_train) and not suitable for large datasets. Only Euclidean distance is demonstrated; other metrics (cosine, Manhattan) could be useful in different descriptor spaces.
Try alternative distance metrics, implement weighted voting, add a faster neighbour index (KD-tree or BallTree), or generate descriptors with correlated features so you can further study the effect of scaling and feature selection.
Full case study, build steps and evaluation checks: KNN algorithm implementation on networkershome.com
Built by Networkers Home · Learn the skills in the AI Engineer Course · Part of 100 AI projects with code