Neuron 101

Learning without labels / k-means clustering

Türkçe

Plate67

Unit 7 · Topic 2 of 6

k-means clustering

Medium, 9 min, 7 steps

024681012141618200246810weekday visitsweekend visits
Keyboard: left and right arrows change the step, space plays and pauses.

Step 1 of 7 Data

We want 3 clusters

The café's 25 customers are still here, none of them labeled. We give k-means only the number of clusters: k = 3. The algorithm will stand for each cluster with a centre and put each customer into a cluster.

Open in Colab
  1. import math, random
  2. from statistics import mean
  3. random.seed(10)
  4. k = 3
  5. C = random.sample(M, k)
  6. def en_yakin(p):
  7. d = [math.dist(p, c) for c in C]
  8. return d.index(min(d))
  9. atama = None
  10. while True:
  11. yeni = [en_yakin(p) for p in M]
  12. if yeni == atama:
  13. break
  14. atama = yeni
  15. for c in range(k):
  16. uye = [M[i] for i in range(n)
  17. if atama[i] == c]
  18. if uye:
  19. x, y = zip(*uye)
  20. C[c] = (mean(x), mean(y))
  21. W = sum(math.dist(p, C[a]) ** 2
  22. for p, a in zip(M, atama))
  23. print(W)
k
3
turSayisi
3
kareler
0
degisen
0
tur
1
enIyi
92.78