def datosParaGraficarKmeans(minSize=2,maxSize=100, step=1, runs=200, nclust = 5, it = 10): totalC=[] #arreglo con el promedio de comparaciones para cada tamaƱo del arreglo totalM=[] #arreglo con el promedio de movimientos img_clstr = km.K_means(n_clusters = nclust, iterations = it) for size in range(minSize, maxSize, step): sum_mov = 0 sum_comp = 0 for i in range(runs): test_array = createIntArray(size) img_clstr.fit(test_array) sum_mov += img_clstr.mov sum_comp += img_clstr.comp totalM.append(sum_mov/runs) totalC.append(sum_comp/runs) return totalC, totalM
def classify_images(self, img_array, nclust, it, plot_3d = False, listnames = None): self.image_cluster = km.K_means(n_clusters = nclust, iterations = it) self.image_cluster.fit(img_array) self.cluster_map['values'] = img_array self.cluster_map['labels'] = self.image_cluster.labels_ self.cluster_map['filename'] = listnames self.complete_center = [False] * len(self.image_cluster.cluster_centers_) #To plot dominant colors and centers if plot_3d: colors = ['red', 'green', 'blue', 'cyan', 'orange'] color_list = [] for label in self.image_cluster.labels_: color_list.append(colors[int(label)]) fig = plt.figure(2) ax = Axes3D(fig) img_array = np.array(img_array) centers = self.image_cluster.cluster_centers_ ax.scatter(img_array[:, 0], img_array[:, 1], img_array[:, 2], c = color_list) ax.scatter(centers[:, 0], centers[:, 1], centers[:, 2], marker='*', c='#050505', s=500)