forked from justmarkham/DAT3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
11_kmeans_limitations.py
140 lines (116 loc) · 4.89 KB
/
11_kmeans_limitations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'''
THREE NOTES ON LIMITATIONS OF K-MEANS CLUSTERING
Adapted from Bart Baddely's 2014 PyData Presentation:
http://nbviewer.ipython.org/github/BartBaddeley/PyDataTalk-2014/blob/master/PyDataTalk.ipynb
Agenda:
1) K-means might not work when dimensions have different scales
2) K-means might not work for non-spherical shapes
3) K-means might not work for clusters of different sizes
'''
from sklearn.cluster import KMeans
import numpy as np
from sklearn.datasets.samples_generator import make_blobs, make_moons
import matplotlib.pyplot as plt
from sklearn.metrics.pairwise import euclidean_distances
'''
1) DIMENSIONS WITH DIFFERENT SCALES
'''
# Generate data with differing variances
np.random.seed(0)
centres = [[1, 0.75], [1, -0.75], [0, 0]]
X0, labels0_true = make_blobs(n_samples=300, centers=centres[0], cluster_std=[[0.6,0.1]])
X1, labels1_true = make_blobs(n_samples=300, centers=centres[1], cluster_std=[[0.6,0.1]])
X2, labels2_true = make_blobs(n_samples=300, centers=centres[2], cluster_std=[[0.6,0.1]])
X = np.concatenate((X0,X1,X2))
labels_true = np.concatenate((labels0_true,labels1_true+1,labels2_true+2))
colors = np.array(['#FF0054','#FBD039','#23C2BC'])
plt.figure(figsize=(12, 6))
plt.suptitle('Dimensions with Different Scales', fontsize=15)
plt.subplot(121)
for k, col in zip(range(3), colors):
my_members = labels_true == k
cluster_center = centres[k]
plt.scatter(X[my_members, 0], X[my_members, 1], c=col, marker='o',s=20)
plt.scatter(cluster_center[0], cluster_center[1], c=col, marker='o', s=200)
plt.axis('equal')
plt.title('Original data')
# Compute clustering with 3 Clusters
k_means_3 = KMeans(init='k-means++', n_clusters=3, n_init=10)
k_means_3.fit(X)
k_means_3_labels = k_means_3.labels_
k_means_3_cluster_centres = k_means_3.cluster_centers_
# Plot result
distance = euclidean_distances(k_means_3_cluster_centres,
centres,
squared=True)
order = distance.argmin(axis=0)
plt.subplot(122)
for k, col in zip(range(3), colors):
my_members = k_means_3_labels == order[k]
plt.scatter(X[my_members, 0], X[my_members, 1],c=col, marker='o', s=20)
cluster_center = k_means_3_cluster_centres[order[k]]
plt.scatter(cluster_center[0], cluster_center[1], marker = 'o', c=col, s=200, alpha=0.8)
plt.axis('equal')
plt.title('KMeans 3')
'''
#2: NON-SPHERICAL SHAPES
'''
[X, true_labels] = make_moons(n_samples=1000, noise=.05)
plt.figure(figsize=(12, 6))
plt.suptitle('Non-Spherical Shapes', fontsize=15)
plt.subplot(121)
for k, col in zip(range(2), colors):
my_members = true_labels == k
plt.scatter(X[my_members, 0], X[my_members, 1], c=col, marker='o', s=20)
plt.axis('equal')
plt.title('Original Data')
k_means_2 = KMeans(init='k-means++', n_clusters=2, n_init=10)
k_means_2.fit(X)
k_means_2_labels = k_means_2.labels_
k_means_2_cluster_centers = k_means_2.cluster_centers_
plt.subplot(122)
for k, col in zip(range(2), colors):
my_members = k_means_2_labels == k
plt.scatter(X[my_members, 0], X[my_members, 1],c=col, marker='o', s=20)
cluster_center = k_means_2_cluster_centers[k]
plt.scatter(cluster_center[0], cluster_center[1], marker = 'o', c=col, s=200, alpha=0.8)
plt.axis('equal')
plt.title('KMeans 2')
'''
#3: CLUSTERS OF DIFFERENT SIZES
'''
np.random.seed(0)
centres = [[-1, 0], [1, 0], [3, 0]]
X0, labels0_true = make_blobs(n_samples=100, centers=centres[0], cluster_std=[[0.2,0.2]])
X1, labels1_true = make_blobs(n_samples=400, centers=centres[1], cluster_std=[[0.6,0.6]])
X2, labels2_true = make_blobs(n_samples=100, centers=centres[2], cluster_std=[[0.2,0.2]])
X = np.concatenate((X0,X1,X2))
labels_true = np.concatenate((labels0_true,labels1_true+1,labels2_true+2))
plt.figure(figsize=(12, 6))
plt.suptitle('Clusters of Different Sizes', fontsize=15)
plt.subplot(121)
for k, col in zip(range(3), colors):
my_members = labels_true == k
cluster_center = centres[k]
plt.scatter(X[my_members, 0], X[my_members, 1], c=col, marker='o',s=20)
plt.scatter(cluster_center[0], cluster_center[1], c=col, marker='o', s=200)
plt.axis('equal')
plt.title('Original data')
# Compute clustering with 3 Clusters
k_means_3 = KMeans(init='k-means++', n_clusters=3, n_init=10)
k_means_3.fit(X)
k_means_3_labels = k_means_3.labels_
k_means_3_cluster_centres = k_means_3.cluster_centers_
# Plot result
distance = euclidean_distances(k_means_3_cluster_centres,
centres,
squared=True)
order = distance.argmin(axis=0)
plt.subplot(122)
for k, col in zip(range(3), colors):
my_members = k_means_3_labels == order[k]
plt.scatter(X[my_members, 0], X[my_members, 1],c=col, marker='o', s=20)
cluster_center = k_means_3_cluster_centres[order[k]]
plt.scatter(cluster_center[0], cluster_center[1], marker = 'o', c=col, s=200, alpha=0.8)
plt.axis('equal')
plt.title('KMeans 3')