Evaluating Weka Simple K Means clustering results

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Evaluating Weka Simple K Means clustering results

uMatthews0060
I have performed Simple K-Means clustering on a variety of .arff files within
Eclipse using the Simple K-Means code from Weka. I now want to evaluate the
clustering to check its accuracy etc. I have wrote the following class to
perform clustering...

I have performed K-Means clustering on a variety of .arff files using the
Simple K-Means code from Weka. I now want to evaluate the clustering to
check its accuracy etc. I have wrote the following class to perform
clustering...
```
package clustering;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import weka.core.Instances;
import clustering.SimpleKMeans;

public class SimpleKMeansClustering {

        int numclusters = 0;
       
        public static BufferedReader load_data(String filename) {
                BufferedReader inputReader = null;
                try {
                        inputReader = new BufferedReader(new FileReader(filename));
                       
                } catch (FileNotFoundException ex) {
                        System.err.println("Error: file not found: " + filename);
                }
                return inputReader;
        }
       
        public static void kmeans_cluster(String filename, int clusterno) throws
Exception {
                // Create KMeans.
        KMeans k_means = new KMeans();
        kmeans.setNumClusters(clusterno);
        kmeans.setSeed(50);

        // Load data.
                BufferedReader dataset = load_data(filename);
                Instances data = new Instances(dataset);
               
                // K-Means clustering.
        try {  
            k_means.buildClusterer(data);
        } catch (Exception ex) {
            System.err.println("Unable to build: " + ex.getMessage());
            ex.printStackTrace();
        }
       
        // Print k_means object after clustering.
        System.out.println(k_means);

        }
       
    public static void save_clustering_result(String filename) {
      BufferedWriter buffw = null;
                FileWriter filew = null;
               
                try {
                        filew = new FileWriter(filename);
                        buffw = new BufferedWriter(filew);
                       
                        // Save kmeans_result object to file.
                        buffw.write(k_means.toString());
                       
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        try {
                                if (buffw != null)
                                        buffw.close();

                                if (filew != null)
                                        filew.close();
                        } catch (IOException ex) {
                                ex.printStackTrace();
                        }
                }
    }
       
        public static void main(String[] args) throws Exception {
                SimpleKMeansClustering.kmeans_cluster("iris.arff", 2);
                SimpleKMeansClustering.save_clustering_result("clustering_result.txt");
        }
````


This gives me the output of...



````
KMeans

Clusters: 2

Iterations: 6

Cluster centroids:
Cluster 0
         6.261999999999998 2.872000000000001 4.906000000000001 1.6760000000000006
Iris-versicolor
Cluster 1
         5.005999999999999 3.4180000000000006 1.464 0.2439999999999999 Iris-setosa
````

How would I evaluate the Simple K Means clustering in Eclipse? I have tried
using the Evaluation class within Weka but it is given me an invalid stream
header: 2520312E. Any help would be appreciated now. Can anyone please help
me?





--
Sent from: https://weka.8497.n7.nabble.com/
_______________________________________________
Wekalist mailing list -- [hidden email]
Send posts to: To unsubscribe send an email to [hidden email]
To subscribe, unsubscribe, etc., visit
https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating Weka Simple K Means clustering results

Eibe Frank-2
Administrator

On Mon, Dec 16, 2019 at 9:46 AM uMatthews0060 <[hidden email]> wrote:
I have performed Simple K-Means clustering on a variety of .arff files within
Eclipse using the Simple K-Means code from Weka. I now want to evaluate the
clustering to check its accuracy etc. I have wrote the following class to
perform clustering...

I have performed K-Means clustering on a variety of .arff files using the
Simple K-Means code from Weka. I now want to evaluate the clustering to
check its accuracy etc. I have wrote the following class to perform
clustering...
```
package clustering;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import weka.core.Instances;
import clustering.SimpleKMeans;

public class SimpleKMeansClustering {

        int numclusters = 0;

        public static BufferedReader load_data(String filename) {
                BufferedReader inputReader = null;
                try {
                        inputReader = new BufferedReader(new FileReader(filename));

                } catch (FileNotFoundException ex) {
                        System.err.println("Error: file not found: " + filename);
                }
                return inputReader;
        }

        public static void kmeans_cluster(String filename, int clusterno) throws
Exception {
                // Create KMeans.
        KMeans k_means = new KMeans();
        kmeans.setNumClusters(clusterno);
        kmeans.setSeed(50);

        // Load data.
                BufferedReader dataset = load_data(filename);
                Instances data = new Instances(dataset);

                // K-Means clustering.
        try { 
            k_means.buildClusterer(data);
        } catch (Exception ex) {
            System.err.println("Unable to build: " + ex.getMessage());
            ex.printStackTrace();
        }

        // Print k_means object after clustering.
        System.out.println(k_means);

        }

    public static void save_clustering_result(String filename) {
        BufferedWriter buffw = null;
                FileWriter filew = null;

                try {
                        filew = new FileWriter(filename);
                        buffw = new BufferedWriter(filew);

                        // Save kmeans_result object to file.
                        buffw.write(k_means.toString());

                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        try {
                                if (buffw != null)
                                        buffw.close();

                                if (filew != null)
                                        filew.close();
                        } catch (IOException ex) {
                                ex.printStackTrace();
                        }
                }
    }

        public static void main(String[] args) throws Exception {
                SimpleKMeansClustering.kmeans_cluster("iris.arff", 2);
                SimpleKMeansClustering.save_clustering_result("clustering_result.txt");
        }
````


This gives me the output of...



````
KMeans

Clusters: 2

Iterations: 6

Cluster centroids:
Cluster 0
         6.261999999999998 2.872000000000001 4.906000000000001 1.6760000000000006
Iris-versicolor
Cluster 1
         5.005999999999999 3.4180000000000006 1.464 0.2439999999999999 Iris-setosa
````

How would I evaluate the Simple K Means clustering in Eclipse? I have tried
using the Evaluation class within Weka but it is given me an invalid stream
header: 2520312E. Any help would be appreciated now. Can anyone please help
me?





--
Sent from: https://weka.8497.n7.nabble.com/
_______________________________________________
Wekalist mailing list -- [hidden email]
Send posts to: To unsubscribe send an email to [hidden email]
To subscribe, unsubscribe, etc., visit
https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html

_______________________________________________
Wekalist mailing list -- [hidden email]
Send posts to: To unsubscribe send an email to [hidden email]
To subscribe, unsubscribe, etc., visit
https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html