java - Unable to get Fuzzy C means working -
i need find out why centroid positions close each other?
i found fuzzy c means code here http://msugvnua000.web710.discountasp.net/posts/details/3347 , tried hard convert java code (below) there missing.
i tried looking @ implementation http://www.codeproject.com/articles/91675/computer-vision-applications-with-c-fuzzy-c-means code looks similar it's different because instead of updating cluster indexes membership values updates - i'm not sure why change implemented?
public class cmeansalgorithm3 { private static int fuzzyness = 2; private final map<double, species> integerclusterhashmap = new hashmap<double, species>(); /// array containing points used algorithm private list<job> points; /// gets or sets membership matrix public double[][] u; /// algorithm precision private double eps = math.pow(10, -5); /// gets or sets objective function private double j; /// gets or sets log message public string log; private list<species> clusterlist; public cmeansalgorithm3(list<job> points, int clustersize){ this.points = points; clusterlist = initialisecentroids(points, clustersize); u = new double[points.size()][clusterlist.size()]; calculateclustermembershipvalues(); recalculateclusterindexes(); } private void calculateclustermembershipvalues() { // iterate through points create initial u matrix (int = 0; < points.size(); i++) { job p = points.get(i); double sum = 0.0; (int j = 0; j < clusterlist.size(); j++) { cluster c = clusterlist.get(j); double diff = math.sqrt(math.pow(p.getmidpointx() - c.getcentroid().getx(), 2.0) + math.pow(p.getmidpointy() - c.getcentroid().gety(), 2.0)); u[i][j] = (diff == 0) ? eps : diff; sum += u[i][j]; } double sum2 = 0.0; (int j = 0; j < clusterlist.size(); j++) { u[i][j] = 1.0 / math.pow(u[i][j] / sum, 2.0 / (fuzzyness - 1.0)); sum2 += u[i][j]; } (int j = 0; j < clusterlist.size(); j++) { u[i][j] = u[i][j] / sum2; } } } /// recalculates cluster indexes private void recalculateclusterindexes() { (int = 0; < points.size(); i++) { double max = -1.0; job p = points.get(i); (int j = 0; j < clusterlist.size(); j++) { max = u[i][j] > max ? u[i][j] : max; // if (max < u[i][j]) { // max = u[i][j]; // p.setclusterindex((max == 0.5) ? 0.5 : j); // } } p.setclusterindex(max); } } /// perform complete run of algorithm until desired accuracy achieved. /// demonstration issues, maximum iteration counter set 20. /// algorithm accuracy /// number of steps algorithm needed complete public list<species> run(double accuracy) { int k = 0; int maxiterations = 100; { k++; j = calculateobjectivefunction(); calculateclustercentroids(); step(); double jnew = calculateobjectivefunction(); if (math.abs(j - jnew) < accuracy) break; } while (maxiterations > k); assignjobstoclusters(); return clusterlist; } /// calculate objective function /// objective function double value private double calculateobjectivefunction() { double jk = 0; (int = 0; < this.points.size();i++) { (int j = 0; j < clusterlist.size(); j++) { jk += math.pow(u[i][j], this.fuzzyness) * math.pow(this.calculateeucliddistance(points.get(i), clusterlist.get(j)), 2); } } return jk; } private list<species> initialisecentroids(final list<job> dataset, final int speciessize) { final list<species> clusterlist = new arraylist<species>(); final list<integer> uniqueindexes = toolbox.uniqueindexes(dataset.size(), speciessize); (int i=0; i< uniqueindexes.size(); i++){ final int randomindex = uniqueindexes.get(i); final species species = new species(i); final centroid centroid = new centroid(dataset.get(randomindex).getmidpointx(), dataset.get(randomindex).getmidpointy(), i); species.setcentroid(centroid); specieslist.add(species); } return clusterlist; } /// perform 1 step of algorithm public void step() { (int c = 0; c < clusterlist.size(); c++) { (int h = 0; h < points.size(); h++) { double top; top = calculateeucliddistance(points.get(h), clusterlist.get(c)); if (top < 1.0) top = eps; // sumterms sum of distances data point clusters. double sumterms = 0.0; (int ck = 0; ck < clusterlist.size(); ck++) { double thisdistance = calculateeucliddistance(points.get(h), clusterlist.get(ck)); if (thisdistance < 1.0) thisdistance = eps; sumterms += math.pow(top / thisdistance, 2.0 / (fuzzyness - 1.0)); } // membership value can calculated as... u[h][c] = (1.0 / sumterms); } } recalculateclusterindexes(); } /// calculates euclid distance between point , centroid /// point /// centroid /// calculated distance private double calculateeucliddistance(job p, species c) { return toolbox.calculatedistance(p.getmidpointx(), p.getmidpointy(), c.getcentroid().getx(), c.getcentroid().gety()); } /// calculates centroids of clusters private void calculateclustercentroids() { (int j = 0; j < clusterlist.size(); j++) { species c = clusterlist.get(j); double ux = 0.0; double uy = 0.0; double membershipsum = 0.0; (int = 0; < points.size(); i++) { job p = points.get(i); double uu = math.pow(u[i][j], this.fuzzyness); ux += uu * p.getmidpointx(); uy += uu * p.getmidpointy(); membershipsum += uu; } c.setmembershipsum(membershipsum); c.getcentroid().setx(((ux / membershipsum))); c.getcentroid().sety(((uy / membershipsum))); log += string.format("cluster centroid: (" + c.getcentroid().getx() + "; " + c.getcentroid().gety() + ")"); } } private void assignjobstoclusters(){ (final cluster cluster : clusterlist){ if (!integerclusterhashmap.containskey(cluster.getmembershipsum())) integerclusterhashmap.put(cluster.getmembershipsum(), cluster); } (job job : points){ final double clusterindex = job.getclusterindex(); species c = integerspecieshashmap.get(clusterindex); if (c != null) { c.add(job); } } }
fuzzy c means code in java
package f; import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; /** * * @author anooj.k.varghese */ public class f{ static double data_set[][]=new double[20000][100]; static double diff[][]=new double[20000][100]; static double eud[][]=new double[20000][1000]; static double intial_centroid[][]=new double[300][400]; static double new_center[][]=new double[300][400]; static int num = 0; static int row=4;//rows in dataset here use iris dataset static int cnum; static int itc=0; static int checker=1; private static void readfile() throws filenotfoundexception { scanner scanner = new scanner(new file("e:/aa.txt"));//dataset path scanner.usedelimiter(system.getproperty("line.separator")); //scanner.usedelimiter(","); int lineno = 0; while (scanner.hasnext()) { parseline(scanner.next(),lineno); lineno++; system.out.println(); } // system.out.println("total"+num); print total scanner.close(); } //read file copey data_set public static void parseline(string line,int lineno) { itc=0; scanner linescanner = new scanner(line); linescanner.usedelimiter(","); for(int col=0;col<row;col++) { double arry=linescanner.nextdouble(); data_set[num][col]=arry; ///here read data set assign variable data_set } num++; } public static void init() { for(int i=0;i<num;i++) { data_set[i][row]=0.0; data_set[i][row+1]=0.0; } } public static void print() { double re=0; double a=0; if(itc==0) { system.out.println("enter k"); scanner sc=new scanner(system.in); cnum=sc.nextint(); //enter number of cenroid system.out.println("centroid"); for(int i=0;i<cnum;i++) { for(int j=0;j<row;j++) { intial_centroid[i][j]=data_set[i][j]; //// centroid stored in intial_centroid variable system.out.print(intial_centroid[i][j]); } system.out.println(); } } else { for(int i=0;i<cnum;i++) { for(int j=0;j<row;j++) { intial_centroid[i][j]=new_center[i][j]; //// centroid stored in intial_centroid variable system.out.print(intial_centroid[i][j]); } system.out.println(); } } for(int i=0;i<num;i++) { for(int j=0;j<cnum;j++) { re=0; for(int k=0;k<row;k++) { a= (intial_centroid[j][k]-data_set[i][k]); //system.out.println(a); a=a*a; re=re+a; // store row sum } diff[i][j]= math.sqrt(re);// find squre root system.out.println(diff[i][j]); } } } public static void s() { double b,c; for(int i=0;i<num;i++) { for(int j=0;j<cnum;j++) { c=0.0; b=0.0; for(int k=0;k<cnum;k++) { if(diff[i][k]==0) { b=0; } if(diff[i][k]!=0) { b=diff[i][j]/diff[i][k]; } c=c+b; } if(c==0) { eud[i][j]=0.0; } else { eud[i][j]=1/c; } } } double a=0; for(int i=0;i<num;i++) { a=0; for(int j=0;j<cnum;j++) { a=a+eud[i][j]; system.out.print(eud[i][j]+" "); } system.out.print("total "+a); system.out.println(); } double aaa; int counter=0; for(int i=0;i<num;i++) {counter=0; aaa=eud[i][0]; for(int j=0;j<cnum;j++) { if(aaa<=eud[i][j]) { aaa=eud[i][j]; counter=j; } } if(itc%2==0) { data_set[i][row]=(double)counter; } if(itc%2==1) { data_set[i][row+1]=(double)counter; } } for(int i=0;i<num;i++) { for(int j=0;j<=row+1;j++) { system.out.print(data_set[i][j]+", "); } system.out.println(); } } public static void newcenter() { itc++; double a=0.0; double c=0.0; double d=0.0; double f=0.0; for(int k=0;k<cnum;k++) { for(int j=0;j<row;j++) { a=0.0; d=0.0; c=0.0; f=0.0; for(int i=0;i<num;i++) { //system.out.print("edu"+eud[i][k]); a=eud[i][k]; a=a*a; c=c+a; //system.out.println("data"+data_set[i][j]); d=a*data_set[i][j]; f=f+d; } new_center[k][j]=f/c; system.out.println("centroid new "+new_center[k][j]); // j=row+5; // k=cnum+5; } } } public static void print11() { system.out.println(); system.out.println(); system.out.println(); system.out.println("----output----"); int c=0; int a=0; for(int i=0;i<cnum;i++) { system.out.println("---------cluster-"+i+"-----"); a=0; for(int j=0;j<num;j++) { if(data_set[j][row]==i) {a++; for(int k=0;k<row;k++) { system.out.print(data_set[j][k]+" "); } c++; system.out.println(); } //system.out.println(num); } system.out.println("cluster instances="+a); } system.out.println("total instance"+c); } public static void check() { checker=0; for(int i=0;i<num;i++) { //system.out.println("hii"); if(double.compare(data_set[i][row],data_set[i][row+1]) != 0) { checker=1; //system.out.println("hii " + + " " + data_set[i][4]+ " "+data_set[i][4]); break; } system.out.println(); } } public static void main(string[] args) throws filenotfoundexception { readfile(); // init(); while(checker!=0) //for(int i=0;i<5;i++) { print(); s(); newcenter(); check(); } print11(); } }
Comments
Post a Comment