Run k-means cluster algorithms
Code: "clustering_kmeans.py". Programming language: Python
DMelt Version 1. Last modified: 12/08/2015. License: Pro
https://datamelt.org/code/cache/clustering_kmeans_5014.py
To run this script using the DMelt IDE,
copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.
from java.util import Random
from jminhep.algorithms import *
from jminhep.cluster import *
# create a data holder
data = DataHolder("Example")
# fill 3D data with Gaussian random numbers
rand = Random()
for i in range(100):
a =[]
a.append( 10*rand.nextGaussian() )
a.append( 2*rand.nextGaussian()+1 )
a.append( 10*rand.nextGaussian()+3 )
data.add( DataPoint(a) )
# print all outputs
def printAnswer(alg):
print "Name="+alg.getName()
print "No of final clusters: " + str(alg.getClusters())
print "No of points in clusters: " + str(alg.getNumberPoints())
print "Compactness: " + str(alg.getCompactness())
centers = alg.getCenters()
print centers.toString()
# show the data
# HTable(data)
# data.print()
alg=KMeansAlg(data)
alg.setClusters(3)
alg.setOptions(1000,0.001)
alg.run()
printAnswer(alg)
# run 10 times with different seeds
# return the best compactness
alg=KMeansAlg(data)
alg.setClusters(3)
alg.setOptions(1000,0.001)
alg.run(10)
printAnswer(alg)
alg=KMeansExchangeAlg(data)
alg.setClusters(3)
alg.setEpochMax(200)
alg.run()
printAnswer(alg)
You see the box below because you did not login.