def hie_clustering(K, Idata): agglomerative1 = AgglomerativeClustering(n_clusters=K, linkage='ward', affinity='euclidean') agglomerative1.fit_predict(Idata) hlabels = agglomerative1.labels_ #print "hlabels : ", hlabels centers = cal_center(Idata, hlabels) ss = silhouette_score(Idata, hlabels) # di = calculate_dunn_index(Idata,label,n_cluster=i+1) pbm = cal_pbm_index(K, Idata, centers, hlabels) return centers, hlabels, ss, pbm
# # Idata=np.array([[1,2,3],[3,2,1],[4,5,6],[6,5,4],[66,74,67],[74,67,66],[44,26,34],[967,589,122]]) K = 24 print "No of clusters : ", K cluster = KMeans(n_clusters=K) cluster.fit(Idata) label = cluster.predict(Idata) centers = cluster.cluster_centers_ #print "Kmeans center : ", centers #print "cal centers : ", cal_center(Idata,label) dn = calculate_dunn_index(Idata, label, n_cluster=K) print "Kmeans Siloutte score : ", silhouette_score(Idata, label) print "PBM index : ", cal_pbm_index(K, Idata, centers, label) print "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, n_cluster=K) print "Agg ward Siloutte score : ", silhouette_score(Idata, hlabels) print "PBM index : ", cal_pbm_index(K, Idata, centers, hlabels) print "Dunn index : ", dn print "---------------------------------------------------------\n"
# 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" agglomerative = AgglomerativeClustering(n_clusters=K, linkage='average', affinity='euclidean') agglomerative.fit_predict(Idata)
K.insert(i, n) print "no. of cluster : ", n cluster = KMeans(n_clusters=n) cluster.fit(Idata) label = cluster.predict(Idata) centers = cluster.cluster_centers_ a = centers.tolist() for j in range(len(a)): for k in range(len(Idata[0])): pop.append(a[j][k]) if not max_choromosome_length - len(pop) == 0: extra_zero = max_choromosome_length - len(pop) pop.extend(0 for x in range(extra_zero)) x.insert(i, pop) ss = silhouette_score(Idata, label) pbm = cal_pbm_index(n, Idata, centers, label) sil_sco.insert(i, ss) PBM.insert(i, pbm) Initial_Label.insert(i, label.tolist()) Final_label = list(Initial_Label) print "Chromosome clusers :", K print "Chromosomes PBM Index :", PBM print "Chromosome SI : ", sil_sco print "Max PBM, Sil : ", max(PBM), ",", max(sil_sco) print "clusters corresponding to Max PBM, Sil : ", K[PBM.index( max(PBM))], ", ", K[sil_sco.index(max(sil_sco))] # file2.write("\n Initial clusters : "+ str(K) + '\n' ) # file2.write("Chromosome initial PBM index: "+ str(PBM) + '\n' ) # file2.write("Chromosome initial Sil score : " +str(sil_sco)+ '\n')