comparison Marea/marea_cluster.py @ 31:944e15aa970a draft

Uploaded
author bimib
date Tue, 15 Oct 2019 12:22:43 -0400
parents e6831924df01
children abf0bfe01c78
comparison
equal deleted inserted replaced
30:e88efefbd015 31:944e15aa970a
174 range_n_clusters = [i for i in range(k_min, k_max+1)] 174 range_n_clusters = [i for i in range(k_min, k_max+1)]
175 distortions = [] 175 distortions = []
176 scores = [] 176 scores = []
177 all_labels = [] 177 all_labels = []
178 178
179 clusterer = KMeans(n_clusters=1, random_state=10)
180 distortions.append(clusterer.fit(dataset).inertia_)
181
182
179 for n_clusters in range_n_clusters: 183 for n_clusters in range_n_clusters:
180 clusterer = KMeans(n_clusters=n_clusters, random_state=10) 184 clusterer = KMeans(n_clusters=n_clusters, random_state=10)
181 cluster_labels = clusterer.fit_predict(dataset) 185 cluster_labels = clusterer.fit_predict(dataset)
182 186
183 all_labels.append(cluster_labels) 187 all_labels.append(cluster_labels)
225 229
226 ############################## elbow_plot #################################### 230 ############################## elbow_plot ####################################
227 231
228 def elbow_plot (distortions, k_min, k_max): 232 def elbow_plot (distortions, k_min, k_max):
229 plt.figure(0) 233 plt.figure(0)
230 plt.plot(range(k_min, k_max+1), distortions, marker = 'o') 234 x = list(range(k_min, k_max + 1))
231 plt.xlabel('Number of cluster') 235 x.insert(0, 1)
236 plt.plot(x, distortions, marker = 'o')
237 plt.xlabel('Number of clusters (k)')
232 plt.ylabel('Distortion') 238 plt.ylabel('Distortion')
233 s = 'clustering/elbow_plot.png' 239 s = 'clustering/elbow_plot.png'
234 fig = plt.gcf() 240 fig = plt.gcf()
235 fig.set_size_inches(18.5, 10.5, forward = True) 241 fig.set_size_inches(18.5, 10.5, forward = True)
236 fig.savefig(s, dpi=100) 242 fig.savefig(s, dpi=100)