Exemplo n.º 1
0
    
# initialize means
numOfExperiments = 5 #number of random initial means experiments
for count in xrange(numOfExperiments):
    if numOfExperiments == 1:
        initialMeans[:] = np.loadtxt(os.path.join(os.path.dirname(__file__), fninitcentroids))
    else:
        initialMeans[:] = Utils.getInitialMeans(points, k)    
        
    print "Initial Means: \n%s" % initialMeans
    print "Initial SSE: %s" % Utils.calcSSE(points, initialMeans)
    
    means = np.zeros((k, len(points[0])))
    means[:] = initialMeans
    finalmeans, labels, SSE = km.run(points, means, numberOfClusters=k, threshold=d, maxiterations=i)
    print "final means: %s" % finalmeans

    if SSE < minSSE:
        print '**New min SSE %s' % SSE
        minSSE = SSE
        minInitialMeans[:] = initialMeans
        print 'Min initial means\n%s' % initialMeans
        minMeans[:] = finalmeans
        minLabels[:] = labels
        
print "min SSE: %s" % minSSE
print "min Initial means:\n %s" % minInitialMeans
print "min means:\n %s" % minMeans

#Plot.plotPoints(points, minLabels, title='final')
Exemplo n.º 2
0
import pylab
from km.kmeans import KMeans
from util.utilities import *


if __name__ == '__main__':   
    # kmeans for 3 clusters
    km = KMeans(showsubplots=False)
    k,d,i = 3, 0.1, 10
    data, centroids = Utils.generateTestDataAndCentroids(k, dimensions=2)
    means, labels = km.run(data, numberOfClusters=k, threshold=d, maxiterations=i)
    print "final means: \n%s" % means
    
    #if not subplot:
    Plot.plotPoints(data, labels, title='final means')
    Plot.plotMeans(means)
    pylab.show()