Plate68
Unit 7 · Topic 3 of 6
Choosing the number of clusters
Medium, 8 min, 6 steps
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.
W, A = {}, {}for k in range(1, 9):A[k], W[k] = kmeans(M, k)def dirsek(W):a, z = W[1], W[8]def bosluk(k):d = a + (z - a) * (k - 1) / 7return d - W[k]return max(W, key=bosluk)print(dirsek(W))def uye(atama, c):return [j for j in range(n)if atama[j] == c]def ort(i, J):return mean(math.dist(M[i], M[j])for j in J)def siluet(atama):s = []for i, c in enumerate(atama):J = [j for j in uye(atama, c)if j != i]if not J:s.append(0)continuea = ort(i, J)b = min(ort(i, uye(atama, e))for e in set(atama)if e != c)s.append((b - a) / max(a, b))return mean(s)for k in range(2, 9):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