๐ป def classify(x): ...
๐ What you need
- the shared phone with a Python app that runs offline (Pydroid 3 or similar) or an online editor
- your measured dataset from module 1, on paper
- patience: typing code on a phone is slow, so type carefully rather than fast
โ Do this
- Type your dataset in as a list of rows, each row being [length, width, label]. Print it. Seeing your own paper data appear on screen is the moment it becomes real.
- Write a distance function: for two feature vectors, add up the squared differences and return that. Six lines at most.
- Write classify(new_item): compute the distance to every stored example, find the smallest, and return that example's label. This is 1-nearest-neighbour, complete.
- Test it on an item you know the answer to. When it prints the right label, you have written a working classifier from scratch with no libraries at all.
- Upgrade to k=3: sort the distances, take the three closest, return the majority label. Compare k=1 and k=3 on the same test items.
- Finally, loop over ten test items and print an accuracy score. Take a screenshot of the output โ that is your evidence.
๐ก Why it works
k-nearest-neighbours has no training step at all: it simply remembers everything and compares. That makes it slow at prediction time and hungry for memory, which is precisely why other algorithms exist. Having written one yourself, you now know that machine learning is not sorcery โ it is arithmetic, arranged carefully, on features you chose.
๐ฅ Challenge
Add a third feature to every row and see whether accuracy improves. Then remove your best feature and watch it collapse. Both experiments teach more than reading about them.
๐ New words
k-nearest-neighbourspredict by finding the k most similar stored examples and taking a vote
distancea number saying how unalike two feature vectors are
functiona named block of code you can call again with different inputs
๐
Code Classifier
Tap when you have finished this module.