``` 1 # Clustering. Cluster analysis. Run cluster algorithms and print output 2 3 from java.util import Random 4 from jminhep.algorithms import * 5 from jminhep.cluster import * 6 7 # create a data holder 8 data = DataHolder("Example") 9 10 # fill 3D data with Gaussian random numbers 11 rand = Random() 12 for i in range(100): 13 a =[] 14 a.append( 10*rand.nextGaussian() ) 15 a.append( 2*rand.nextGaussian()+1 ) 16 a.append( 10*rand.nextGaussian()+3 ) 17 data.add( DataPoint(a) ) 18 19 # print all outputs 20 def printAnswer(alg): 21 print "Name="+alg.getName() 22 print "No of final clusters: " + str(alg.getClusters()) 23 print "No of points in clusters: " + str(alg.getNumberPoints()) 24 print "Compactness: " + str(alg.getCompactness()) 25 centers = alg.getCenters() 26 print centers.toString() 27 28 29 # show the data 30 # HTable(data) 31 32 # data.print() 33 34 alg=KMeansAlg(data) 35 alg.setClusters(3) 36 alg.setOptions(1000,0.001) 37 alg.run() 38 printAnswer(alg) 39 40 41 # run 10 times with different seeds 42 # return the best compactness 43 alg=KMeansAlg(data) 44 alg.setClusters(3) 45 alg.setOptions(1000,0.001) 46 alg.run(10) 47 printAnswer(alg) 48 49 50 alg=KMeansExchangeAlg(data) 51 alg.setClusters(3) 52 alg.setEpochMax(200) 53 alg.run() 54 printAnswer(alg) 55 56 57 alg=KMeansExchangeAlg(data) 58 alg.setClusters(3) 59 alg.setEpochMax(200) 60 alg.runBest() # find best No clusters 61 printAnswer(alg) 62 63 64 alg=FuzzyCMeansAlg(data) 65 alg.setClusters(3) 66 alg.setOptions(1000,0.001,1.7) 67 alg.setProb(0.7) 68 alg.run() 69 printAnswer(alg) 70 71 72 alg=HierarchicalAlg(data) 73 alg.setClusters(3) 74 alg.run() 75 printAnswer(alg) ### © jHepWork. S.Chekanov ### ```