?? kmeans.java
字號:
package kmeans;import java.io.*;/** * The kmeans function perfotms a K-means clustering of 18 modes into two * clusters. * @author liyun * @version v1.1 */public class Kmeans{ /** *it means ture */ private static int SUCCESS=1; private static int TRUE=1; private static int FALSE=0; private static int MAXVECTDIM=20; private static int MAXPATTERN=20; private static int MAXCLUSTER=10; /** * The input Matrix * @see */ private double pattern[][] = new double[MAXPATTERN][MAXVECTDIM] ; /**The Cluster arrays */ private ACluster Cluster[] = new ACluster[MAXCLUSTER]; /**number of patterns */ private int numPatterns; /** Number of dimensions in vector */ private int sizeVector; /** Number of clusters */ private int numClusters; /** * Creates a new instance of System */ public Kmeans() {} /** initial the clusters */ public void initClusters(){ System.out.println("Initial cluster centers:"); for (int i=0; i<numClusters; i++) { Cluster[i]=new ACluster(); for (int j=0; j<sizeVector; j++) { Cluster[i].center[j]=pattern[i][j]; } /* endfor */ } /* endfor */ for (int i=0; i<numClusters; i++) { System.out.println("ClusterCenter["+i+"]="+Cluster[i].center[0]+" "+Cluster[i].center[1]); } } /** * Run the Kmeans */ public void runKmeans(){ int converged=FALSE; int pass =1; while (converged==FALSE) { System.out.println("PASS= "+(pass++)); distributeSamples(); converged=calcNewClustCenters(); showCluster(); } /* endwhile */ } private void distributeSamples(){ int i,pat,clustid,memberIndex; //Clear membership list for all current clusters for (i=0; i<numClusters;i++){ Cluster[i].numMembers=0; } for (pat=0; pat<numPatterns; pat++) { /*Find cluster center to which the pattern is closest*/ clustid= findClosestCluster(pat); System.out.println("pattern "+pat+" belong to " +"cluster["+clustid +"]"); /*post this pattern to the cluster*/ memberIndex=Cluster[clustid].numMembers; Cluster[clustid].member[memberIndex]=pat; Cluster[clustid].numMembers++; } /* endfor */ } private int findClosestCluster(int pat){ int i, clustID; double minDist, d; minDist =9.9e+99; clustID=-1; for (i=0; i<numClusters; i++) { d=eucNorm(pat,i); System.out.println("pattern "+pat+" to the cluster [" +i+"]'s distance is "+d+" !"); if (d<minDist) { minDist=d; clustID=i; } /* endif */ } /* endfor */ if (clustID<0) { System.out.println("Aaargh"); System.exit(0); } /* endif */ return clustID; } /** Calc Euclidean norm of vector difference between pattern vector, p, and cluster center, c. */ private double eucNorm(int p, int c){ double dist=0,x=0; int i; System.out.println("Now calculate pattern "+p+" to"+" the cluster["+c+"['s distance:"); for (i=0; i<sizeVector ;i++){ x += (Cluster[c].center[i]-pattern[p][i])*(Cluster[c].center[i]-pattern[p][i]); } /* endfor */ return dist=Math.sqrt(x); } private int loadPatterns(String fname) throws java.io.IOException { File InFilePtr=new File(fname); FileInputStream f1=new FileInputStream(InFilePtr); DataInputStream fd1=new DataInputStream(f1); int i,j; if(fd1!=null){ numPatterns=fd1.readInt(); sizeVector=fd1.readInt(); numClusters=fd1.readInt(); for(i=0;i<numPatterns;i++){ for(j=0;j<numClusters;j++){ double x=fd1.readDouble(); pattern[i][j]=x; } } } fd1.close(); f1.close(); for (i=0;i<18;i++){ System.out.println("Pattern:"+pattern[i][0] +" " + pattern[i][1]); } return SUCCESS; } /** calculate the new clustCenters * @see */ public int calcNewClustCenters(){ int k=0; int vectID; int ConvFlag=TRUE; double tmp[]=new double[MAXVECTDIM]; for(int j=0;j<MAXVECTDIM;j++){ tmp[j]=0.0; } for(int i=0;i<numClusters;i++){ for(int j=0;j<Cluster[i].numMembers;j++){ vectID=Cluster[i].member[j]; for (k=0;k<sizeVector;k++){ tmp[k]+=pattern[vectID][k]; } } for(k=0;k<sizeVector;k++){ tmp[k]=tmp[k]/(Cluster[i].numMembers); if(tmp[k]!=Cluster[i].center[k]) ConvFlag=FALSE; Cluster[i].center[k]=tmp[k]; } } return ConvFlag; } /** * show clusters */ public void showCluster(){ int cl; for(cl=0;cl<numClusters;cl++){ System.out.println("CLUSTER["+cl+"]: "+Cluster[cl].center[0]+" "+Cluster[cl].center[1]); for(int i=0;i<Cluster[cl].numMembers;i++) System.out.println("the Patterns in the Cluster["+cl+"] "+" are: "+pattern[Cluster[cl].member[i]][0]+ " "+pattern[Cluster[cl].member[i]][1] ); } } /** * save clusters * @param * @since */ public void saveCluster(String fname){} /** * main methoes * @param * @ */ public static void main(String[ ] args)throws java.io.IOException{ Kmeans kmean=new Kmeans(); if(kmean.loadPatterns("D:/javasources/ann/Data.txt")!=SUCCESS){ System.out.println("UNABLE TO READ PATTERN_FILE:"); System.exit(0); } kmean.initClusters(); kmean.runKmeans(); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -