``` 1 # Clustering. A fuzzy C-means clustering 2 3 from java.awt import Color 4 from java.util import Random 5 from jhplot import P1D,HPlot 6 from jminhep.cluster import DataPoint,Partition,DataHolder 7 8 # create a data holder 9 data = DataHolder("Example") 10 11 # fill 3D data with Gaussian random numbers 12 rand = Random() 13 # make first cluster 14 for i in range(20): 15 a =[] 16 a.append( -2*rand.nextGaussian()-8) 17 a.append( 2*rand.nextGaussian()+5 ) 18 a.append( 3*rand.nextGaussian()+10 ) 19 data.add( DataPoint(a) ) 20 del a 21 22 # make second cluster 23 for i in range(30): 24 a =[] 25 a.append( 10*rand.nextGaussian() +10) 26 a.append( 2*rand.nextGaussian()+20 ) 27 a.append( 10*rand.nextGaussian()+5 ) 28 data.add( DataPoint(a) ) 29 del a 30 31 # make 3rd cluster 32 for i in range(10): 33 a =[] 34 a.append( 3*rand.nextGaussian() +10) 35 a.append( 2*rand.nextGaussian()+1 ) 36 a.append( 2*rand.nextGaussian()+20 ) 37 data.add( DataPoint(a) ) 38 del a 39 40 41 42 43 44 c1=HPlot() 45 c1.setAutoRange() 46 p1=P1D(data,0,1) # show 1st and 2nd component 47 48 # show the table 49 # HTable(p1) 50 51 c1.draw(p1) 52 c1.visible(1) 53 54 55 56 # initialte partitioner 57 pat = Partition(data) 58 59 # set mode 60 pat.set(3, 0.001, 1.7, 1000) 61 62 # probability for membership (only for Fuzzy algorithm) 63 pat.setProbab(0.68) 64 65 pat.run(131) 66 print "algorithm: " + pat.getName() 67 # print "Compactness: " + str(pat.getCompactness()) 68 # print "No of final clusters: " + str(pat.getNclusters()) 69 Centers = pat.getCenters() 70 p2=P1D(Centers,"Centroids of clusters",0,1) # show 1st and 2nd component 71 p2.setColor(Color.red) 72 c1.draw(p2); 73 # HTable(Centers) ### © jHepWork. S.Chekanov ### ```