def func(alpha, nClusters=nClusters): alpha1, alpha2 = alpha[:self.nVars], alpha[self.nVars:] alpha1 = alpha1.reshape((self.nVars, 1)) alpha2 = alpha2.reshape(self.nVars, 1) alpha = numpy.append(alpha1, alpha2, axis=1) smpl = numpy.dot(self.Z, alpha) smpl = orange.ExampleTable(smpl) km = orngClustering.KMeans(smpl, centroids=nClusters) score = orngClustering.score_silhouette(km) return -score
def func(alpha, nClusters=nClusters): alpha1, alpha2 = alpha[:self.nVars], alpha[self.nVars:] alpha1 = alpha1.reshape((self.nVars,1)) alpha2 = alpha2.reshape(self.nVars,1) alpha = numpy.append(alpha1, alpha2, axis=1) smpl = numpy.dot(self.Z, alpha) smpl = orange.ExampleTable(smpl) km = orngClustering.KMeans(smpl, centroids=nClusters) score = orngClustering.score_silhouette(km) return -score
import orange import orngClustering data = orange.ExampleTable("voting") # data = orange.ExampleTable("iris") for k in range(2, 5): km = orngClustering.KMeans(data, k, initialization=orngClustering.kmeans_init_diversity) score = orngClustering.score_silhouette(km) print k, score km = orngClustering.KMeans(data, 3, initialization=orngClustering.kmeans_init_diversity) orngClustering.plot_silhouette(km, "kmeans-silhouette.png")
import orange import orngClustering import random data = orange.ExampleTable("iris") # data = orange.ExampleTable("lung-cancer") bestscore = 0 for k in range(2, 10): random.seed(42) km = orngClustering.KMeans( data, k, initialization=orngClustering.KMeans_init_hierarchicalClustering(n=50), nstart=10) score = orngClustering.score_silhouette(km) print "%d: %.3f" % (k, score) if score > bestscore: best_km = km bestscore = score orngClustering.plot_silhouette(best_km, filename='tmp.png')