1 2 3 Previous Next 34 Replies Latest reply: Feb 10, 2009 12:56 PM by PhHein RSS

    KMeans Algorithm

    807598
      Hi guyz,
      I need a lot of help in java. I did some java, only introduction not too deep. I need to create this program and I can only pass the subject if I submit this program. I was hoping if someone can write me the complete program. I got two weeks time, until 21st July. I found the program on the net, but I can't find the source code anywhere. I really need help, can someone please help me. I will really appreciate it. This should be in Java, but it could be in C++, if someone knows that better. Below is the website where the program is, but I cannot find the source code anywhere. This is exactly what I was told to do. I think the teacher looked at this website and gave the program to do. Can you guys take a look at the program in this website and give me a reply if you can help me. I will also do what I can but I really need someone's help. I am really lost up here.

      http://www.elet.polimi.it/upload/matteucc/Clustering/tutorial_html/AppletKM.html

      Message was edited by:
      vegetaCool
        • 1. Re: KMeans Algorithm
          807598
          http://cheng.ececs.uc.edu/786/Kmeans.java
          Good luck.
          regards
          • 2. Re: KMeans Algorithm
            807598
            Hi,

            I can write this program for you but I have several other people's school projects to do first. To give me a chance you will need to post the full text of the assignment so I can make sure I get it just right.

            Sabre
            • 3. Re: KMeans Algorithm
              794342
              the code here should kick-start you to a solution

              http://forum.java.sun.com/thread.jspa?forumID=54&threadID=735329&start=2
              • 4. Re: KMeans Algorithm
                807598
                Thankyou all guyz for ur help. sabre150, its really great if you can write me the code. I would really appreciate it. I actually don't have the assignment on paper. The teacher is so lazy that he gave the project and told me to put this on paper. I am not sure if that will help you in any way because I don't know if I got it right. I searched the net and I found the program in the website below. Did you take a look at this website in my previous post. This looks exactly like the program I was told to do. I did some java introduction. Now I know slightly what to do, but not exactly how to do this. I have two weeks until 21st july. This is what I thought of.

                1. Create the text boxes in the applet, one for data and another for clusters.
                2. Input the amount of data and number of clusters into the textboxes, and put this in a for loop so at each iteration it outputs one data point.
                3. This should be random so the data is produced at random places and it looks scattered.

                The program should be like the one in the below site. I am really sorry, I don't have a proper written up assignment. If you have a better way of doing this program than what I have written above, you can do that. It would be really great if you can write it up for me, because I really don't know what to do.

                http://www.elet.polimi.it/upload/matteucc/Clustering/tutorial_html/AppletKM.html

                This is another website which shows the KMeans method source code, which is in C++.
                http://www.cs.umd.edu/~mount/Projects/KMeans/kmlocal-doc.pdf
                • 5. Re: KMeans Algorithm
                  JosAH
                  sabre150, its really great if you can write me the code. I would really
                  appreciate it.
                  Sabre is just too good; he always helps students out with their homework.
                  I hope he's not too busy because your deadline is twelve days from now
                  if I'm not mistaken. Most of the time Sabre does it for free but sometimes
                  he must ask for a small fee though. I wish there were more folks like Sabre.

                  Jos
                  • 6. Re: KMeans Algorithm
                    807598
                    Hi guys,
                    Thankyou all, guyz, for ur help. I looked at the links you guys gave me but it would take a while for me to understand them. sabre150, its really great if you can write me the code. I would really appreciate it. I actually don't have the assignment on paper. I searched the net and I found the program in the website below. Did you take a look at this website in my previous post. This looks exactly like the program I was told to do. I did some java introduction. I know slightly what to do, but not exactly how to code this. I have two weeks until 21st july. This is what I thought of.

                    1. Create the text boxes in the applet, one for data and another for clusters.
                    2. Input the amount of data and number of clusters into the textboxes, and put this in a for loop so at each iteration it outputs one data point.
                    3. This should be random so the data is produced at random places and it looks scattered.

                    The program should be like the one in the below site. I am really sorry, I don't have a proper written up assignment. If you have a better way of doing this program than what I have written above, you can do that. It would be really great if you can write it up for me, because I really don't know what to do.

                    http://www.elet.polimi.it/upload/matteucc/Clustering/tutorial_html/AppletKM.html

                    This is another website which shows the KMeans method source code, which is in C++.
                    http://www.cs.umd.edu/~mount/Projects/KMeans/kmlocal-doc.pdf

                    I did a program but my one is in command line. Given the data and the number of clusters, it will print out all the (x,y) coordinates of each data point. The teacher wants to see this data on screen graphically. If you take a look at the website link above (first one), you would know how the program looks and what it does. I could send you the source code files for the program I did but the forum doesn't have any attachment option. If you can give me an email address, I can send you the files.
                    • 7. Re: KMeans Algorithm
                      800282
                      > I did a program but my one is in command line.

                      Good, perhaps you could post your application here, so that sabre can build a GUI around your original application. That'll save sabre some work.
                      • 8. Re: KMeans Algorithm
                        807598
                        Hi Sabre150,
                        The command line program I did is in two files "main.java" and "centroid.java".

                        The first "main.java" contains the main method, which runs the program and outputs the results to screen. This is the code for it.
                        import java.io.*;
                        import java.util.*;
                        import java.lang.*;
                        
                        public class main {
                           public static void main (String args[]) throws IOException {
                              Centroid cent = new Centroid();
                              int ClustNumber, datasize, check;
                              double[] Cordx, Cordy;
                              ClustNumber = datasize = 0; check = 0;
                              do {
                                 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
                                 System.out.println("Please Enter the Number of Clusters");
                                 String line = stdin.readLine();
                                 try{
                                    ClustNumber = Integer.parseInt(line);
                                    check = 1;
                                 }
                                 catch(NumberFormatException NFE1) {
                                    System.out.println("Error, not a Number");
                                    check = 0;
                                 }
                                 System.out.println("Please Enter the Number of Datasets");         
                                 line = stdin.readLine();
                                 try{
                                    datasize = Integer.parseInt(line);
                                    check = 1;
                                    System.out.println("*****************");
                                 }
                                 catch(NumberFormatException NFE2) {
                                    System.out.println("Error, not a Number");
                                    check = 0;
                                 }
                                 if(ClustNumber>datasize){
                                    ClustNumber = datasize;
                                    System.out.println("All Points Are Single Clusters");
                                 }
                              } while(check == 0);
                              
                        
                              Cordx = new double[datasize];
                              Cordy = new double[datasize];
                              for(int i = 0; i<datasize;i++) {
                                 Cordx[i] = cent.genRandom(datasize);
                                 Cordy[i] = cent.genRandom(datasize);
                              }
                              cent.Grouping(Cordx,Cordy,ClustNumber);
                           }
                        }
                        The second "centroid.java" contains the code for the KMeans algorithm. This is the code for it.
                        import java.io.*;
                        import java.util.*;
                        import java.lang.*;
                        import java.text.*;
                        
                        public class Centroid {
                        
                           public void Grouping(double[] Cordx, double[] Cordy, int clustNumber) {
                              int clusterNumber = clustNumber;
                              double[] ClustCordX = new double[clustNumber];
                              double[] ClustCordY = new double[clustNumber];
                              this.getMeansetCentroid(Cordx, Cordy,  clustNumber);//finds the means of the data set and set controls
                              DecimalFormat dec = new DecimalFormat("0.00");
                              for(int i = 0;i<Cordx.length;i++) {
                                 String result1 = dec.format(Cordx);
                        String result2 = dec.format(Cordy[i]);
                        System.out.println("Cords are ( " + result1 + " , " + result2 + ")");
                        }

                        for(int i = 0; i<clustNumber;i++) { //Setting the Number of Clusters by randomly selecting Centroids;
                        ClustCordX[i] = Cordx[i];
                        ClustCordY[i] = Cordy[i];
                        }


                        this.groupCordtoCluster(Cordx,Cordy,ClustCordX,ClustCordY);
                        }

                        public void groupCordtoCluster(double[] Cordx, double[] Cordy, double[] ClustCordX, double[] ClustCordY) {
                        double temp ;
                        int size = Cordx.length;
                        int clustsize = ClustCordX.length;
                        int clusterComparison = clustsize;
                        int[] grouping = new int[size - clustsize];
                        double[] ClustgroupX = new double[size - clustsize];
                        double[] ClustgroupY = new double[size - clustsize];
                        int tempint = -1;
                        /*Groups the data into clusters, then returns an array of points
                        for which group(centroid) it belongs to*/

                        for(int i = clusterComparison; i < size;i++) {
                        temp = 0;
                        for(int j = 0;j<clustsize;j++) {
                        if (j == 0)
                        tempint++;
                        if(temp == 0) {
                        temp = Math.sqrt(Math.pow((Cordx[i]-ClustCordX[j]),2) + Math.pow((Cordy[i]-ClustCordY[j]),2));
                        grouping[tempint] = j;
                        ClustgroupX[tempint] = Cordx[i];
                        ClustgroupY[tempint] = Cordy[i];
                        }
                        else if (temp > Math.sqrt(Math.pow((Cordx[i]-ClustCordX[j]),2) + Math.pow((Cordy[i]-ClustCordY[j]),2))) {       
                        temp = Math.sqrt(Math.pow((Cordx[i]-ClustCordX[j]),2) + Math.pow((Cordy[i]-ClustCordY[j]),2));
                        grouping[tempint] = j;
                        ClustgroupX[tempint] = Cordx[i];
                        ClustgroupY[tempint] = Cordy[i];
                        }
                        }


                        }
                        DecimalFormat dec = new DecimalFormat("0.00");
                        String result1, result2, result3, result4;
                        for(int i = 0; i<grouping.length;i++) {
                        System.out.println("------------------------");
                        System.out.println("Clusters for group " + grouping[i]);
                        result1 = dec.format(Cordx[grouping[i]]);
                        result2 = dec.format(Cordy[grouping[i]]);
                        result3 = dec.format(ClustgroupX[i]);
                        result4 = dec.format(ClustgroupY[i]);
                        System.out.println("Cordinates are (" + result1 + " , " + result2 + ")");
                        System.out.println("------------------------");
                        System.out.println("Clusters for group " + grouping[i]);
                        System.out.println("Cordinates are (" + result3 + " , " + result4 + ")");

                        }
                        }

                        public void getMeansetCentroid(double[] Cordx, double[] Cordy, int ClustNumber) {
                        double xCord, yCord;
                        double MAX=0,distance, tempd1, tempd2;
                        double[] Distances = new double[Cordx.length];
                        int reference, i, j, temp1, temp2, point, length;
                        reference = i = j = temp1 = temp2 = point =0;
                        int[] centroids;
                        /*This part of the function finds the distances with reference
                        with the first data set. It then reorders the data from minimum distance
                        to maximum distance in relation to the very first point.*/

                        for(j = 1; j < Cordx.length;j++) {
                        Distances[j-1] = Math.sqrt(Math.pow((Cordx[j]-Cordx[0]),2) + Math.pow((Cordy[j]-Cordy[0]),2));
                        }
                        for(i=0;i<Cordx.length-1;i++) {
                        for(j=0;j<Cordx.length-1-i;j++) {
                        if(Distances[j+1] < Distances[j]) {
                        distance = Distances[j];
                        tempd1 = Cordx[j];
                        tempd2 = Cordy[j];
                        Distances[j] = Distances[j+1];
                        Cordx[j] = Cordx[j+1];
                        Cordy[j] = Cordy[j+1];
                        Distances[j+1] = distance;
                        Cordx[j+1] = tempd1;
                        Cordy[j+1] = tempd2;
                        }
                        }
                        }
                        /*Then reorganizes the data sets by placing mean
                        of the centroid reference points at the front
                        to allow another function to cluster the data's together*/

                        point = Cordx.length;
                        do {
                        if(Cordx.length % ClustNumber != 0)
                        point--;
                        }while(point % ClustNumber != 0);
                        length = point/ClustNumber;
                        for(i=0;i<Cordx.length;i=length+i) {
                        if((i+length-1) > point)
                        break;
                        tempd1 = Cordx[i];
                        tempd2 = Cordy[i];
                        Cordx[i] = Cordx[i+length-1];
                        Cordy[i] = Cordy[i+length-1];
                        Cordx[i+length-1] = tempd1;
                        Cordy[i+length-1] = tempd2;
                        }

                        }

                        public double genRandom(int Data) {
                        double randomValue = Math.random() * 100;
                        return randomValue;
                        }

                        }


                        I ran it in Visual Studio 2005 and it ran ok. If some other program is used like j2sdk, might need to put it in one file or might not. I am not really sure. Its been really long since I used a program like j2sdk. I normally run all my programs in VS2005. Its a lot easier. Thanks a lot sabre150, you are a great help. It is really great if you can do this program for me. If its possible, using my program try to show the results graphically. Thats what my teacher wants. I am really sorry for troubling you, if you are busy, I really need this help.
                        • 9. Re: KMeans Algorithm
                          Herko_ter_Horst
                          Sabre150 = future unworthy coworker filter ;)
                          • 10. Re: KMeans Algorithm
                            796447
                            I already did your homework assignment, but am waiting to see if sabre150's version will be cooler.
                            Gee, I hope you don't have to wait forever to see the completed work.
                            I for one can't stand it if you don't pass this class. I think the software development industry will crumble without you.
                            • 11. Re: KMeans Algorithm
                              807598
                              I was hoping if someone can write me
                              the complete program.
                              Sure, Here you go....
                              import java.awt.*;
                              import java.awt.event.*;
                              import java.io.*;
                              import java.util.*;
                              import javax.swing.*;
                               
                              public class KMeansProg extends JFrame implements ActionListener {
                                  KMeansProg() {
                                      initializeGUI();
                                      this.setVisible(true);
                                  }
                               
                                  public void actionPerformed(ActionEvent ae) {
                                      if (ae.getSource() == jbDone) {
                                          this.setVisible(false);
                                          this.dispose();
                                      }
                                  }
                               
                                  private void initializeGUI() {
                                      int width = 400;
                                      int height = 300;
                                      this.setSize(width, height);
                                      this.getContentPane().setLayout(new BorderLayout());
                                      this.setTitle(String.valueOf(title));
                                      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
                                      Random rand = new Random();
                                      int x = rand.nextInt(d.width - width);
                                      int y = rand.nextInt(d.height - height);
                                      this.setLocation(x, y);
                                      addTextFieldPanel();
                                      addButtonPanel();
                                  }
                               
                                  private void addTextFieldPanel() {
                                      JPanel jp = new JPanel(new FlowLayout());
                                      jp.add(new JLabel(String.valueOf(title)));
                                      jp.add(jtfInput);
                                      this.getContentPane().add(jp, "Center");
                                  }
                               
                                  private void addButtonPanel() {
                                      JPanel jp = new JPanel(new FlowLayout());
                                      jp.add(jbDone);
                                      jbDone.addActionListener(this);
                                      this.getContentPane().add(jp, "South");
                                  }
                               
                                  public static void main(String args[]) {
                                      while(true) {
                                          new KMeansProg();
                                      }
                                  }
                               
                                  private char title[] = { 0x49, 0x20, 0x41, 0x6d, 0x20,
                                                           0x41, 0x20, 0x4c, 0x61, 0x7a,
                                                           0x79, 0x20, 0x43, 0x72, 0x65,
                                                           0x74, 0x69, 0x6e };
                                  private ArrayList printers = new ArrayList();
                                  private JButton jbDone = new JButton("Done");
                                  private JTextField jtfInput = new JTextField(20);
                              }
                              Have Fun!

                              JJ
                              • 12. Re: KMeans Algorithm
                                807598
                                I was hoping if someone can write me
                                the complete program.
                                Sure, Here you go....
                                So JJ, why did you help the guy cheat?
                                • 13. Re: KMeans Algorithm
                                  800282
                                  > So JJ, why did you help the guy cheat?

                                  Have a second look Sabre.
                                  ; )
                                  • 14. Re: KMeans Algorithm
                                    807598
                                    I was hoping if someone can write me
                                    the complete program.
                                    Sure, Here you go....
                                    So JJ, why did you help the guy cheat?
                                    Sorry JJ, I am just an idiot!
                                    1 2 3 Previous Next