Neuron 101

Learning without labels / Choosing the number of clusters

Türkçe

Plate68

Unit 7 · Topic 3 of 6

Choosing the number of clusters

Medium, 8 min, 6 steps

894.4012345678number of clusters k
Keyboard: left and right arrows change the step, space plays and pauses.

Step 1 of 6 k = 1

One cluster

All customers are in one cluster, and its centre is the mean of the data. The sum of squares is 894.4: the squared distance of each customer to that mean, added up. The other values of k will be compared with this one.

Open in Colab
  1. W, A = {}, {}
  2. for k in range(1, 9):
  3. A[k], W[k] = kmeans(M, k)
  4. def dirsek(W):
  5. a, z = W[1], W[8]
  6. def bosluk(k):
  7. d = a + (z - a) * (k - 1) / 7
  8. return d - W[k]
  9. return max(W, key=bosluk)
  10. print(dirsek(W))
  11. def uye(atama, c):
  12. return [j for j in range(n)
  13. if atama[j] == c]
  14. def ort(i, J):
  15. return mean(math.dist(M[i], M[j])
  16. for j in J)
  17. def siluet(atama):
  18. s = []
  19. for i, c in enumerate(atama):
  20. J = [j for j in uye(atama, c)
  21. if j != i]
  22. if not J:
  23. s.append(0)
  24. continue
  25. a = ort(i, J)
  26. b = min(ort(i, uye(atama, e))
  27. for e in set(atama)
  28. if e != c)
  29. s.append((b - a) / max(a, b))
  30. return mean(s)
  31. for k in range(2, 9):
  32. print(k, siluet(A[k]))
d
3
e
3
w1
894.40
wd
92.78
sEn
0.71
a
2.57
b
10.85
s
0.76
no
1