rAs Kids All courses
AI: Build & Break It ยท Module 7 of 8
๐Ÿค– Salvage kit

Write the Classifier in Python

The algorithm you did with stones on the ground is about fifteen lines of Python.

๐Ÿ’ป def classify(x): ...
A phone screen with a short Python function and a printed prediction beneath it.

๐ŸŽ’ What you need

โœ‹ Do this

  1. 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.
  2. Write a distance function: for two feature vectors, add up the squared differences and return that. Six lines at most.
  3. 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.
  4. 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.
  5. 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.
  6. 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.