): ## Initialize dictionary of size upto length maximum generation CN[mm] = {} RN[mm] = {} for i in range(0, chromosome): cluster = random.randint( 2, max_cluster ) ##Randomly selects cluster number from 2 to root(poplation) K.insert(i, cluster) ##Store the number of clusters in clu u, label = Kmeans_clu(cluster, data) lab.insert(i, label) ##Store the labels in lab center.insert(i, u) new_pbm = cal_pbm_index(cluster, data, u, label) pbm_index.insert(i, new_pbm) ##Store the second objective function(PBM Index) dn = calculate_dunn_index(data, label, cluster) ini_dunn.insert(i, dn) s = silhouette_score(data, label) sil_score.insert(i, s) ##Store the first objective funvtion(Silhouette Score) new_center = pop_create(max_cluster, features, cluster, u) population.insert(i, new_center) ##Store the populations in population file.write("Initial Clusters : " + str(K) + '\n') file.write("Initial PBM : " + str(pbm_index) + '\n') file.write("Initial Sil score : " + str(sil_score) + '\n') file.write("Initial Dunn : " + str(ini_dunn) + '\n') index = ini_dunn.index(max(ini_dunn)) file.write("Max Dunn and corresponding cluster, PBM, Sil : " + str(ini_dunn[index]) + "," + str(K[index]) + "," + str(pbm_index[index]) + "," + str(sil_score[index]) + '\n')
return centroids # Give no. of clusters K = 2 # -------------------------------- cluster = KMeans(n_clusters=K) cluster.fit(Idata) label = cluster.predict(Idata) centers = cluster.cluster_centers_ dn = calculate_dunn_index(Idata, label, K) print "Kmeans Siloutte score : ", silhouette_score(Idata, label) print "Kmeans PBM index : ", cal_pbm_index(K, Idata, centers, label) print "Kmeans Dunn index : ", dn print "-------------------------------------------------------------\n" agglomerative1 = AgglomerativeClustering(n_clusters=K, linkage='ward', affinity='euclidean') idx = agglomerative1.fit_predict(Idata) hlabels = agglomerative1.labels_ centers = cal_center(Idata, hlabels) dn = calculate_dunn_index(Idata, hlabels, K) print "Hierarchical single Siloutte score : ", silhouette_score(Idata, hlabels) print "Hierarchical single PBM index : ", cal_pbm_index(K, Idata, centers, hlabels) print "Hierarchical single Dunn index : ", dn print "---------------------------------------------------------\n"