Neuron 101

Classification / k-nearest neighbours

Türkçe

Plate35

Unit 4 · Topic 3 of 10

k-nearest neighbours

Easy, 7 min, 8 steps

812162024283236024681012temperature, degreessunny hours
Keyboard: left and right arrows change the step, space plays and pauses.

Step 1 of 8 Days

Two features, two classes

Each day now has two features: temperature across, sunny hours up. The red circles are days when the ice cream sold out, the blue squares days when it did not.

Open in Colab
  1. def knn(gunler, q, k):
  2. def uzak(g):
  3. return ((g[0] - q[0]) ** 2
  4. + (g[1] - q[1]) ** 2)
  5. yakin = sorted(gunler, key=uzak)
  6. yakin = yakin[:k]
  7. oy = sum(g[2] for g in yakin)
  8. if 2 * oy != k:
  9. return int(2 * oy > k)
  10. return yakin[0][2]
  11. print(knn(gunler, (26.0, 6.0), 3))
k
3
oy
2
sinif
1