Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

k-means weka java code

Tags:

java

k-means

weka

I read a lot of examples of use this library in Java and clustering is possible from ARFF data file and it works.

But I have my own data in List of double which is generating while working my program and I don't know how can I use this k-means algorithm to cluster my data. This is one dimension List.

It is my code:

    Instances dataa = DataSource.read("C:\\Users\\Ew\\Documents\\iris.arff"); 


    // create the model 
    kMeans = new SimpleKMeans();
    kMeans.setNumClusters(3);
    kMeans.buildClusterer(dataa); 

    // print out the cluster centroids
    Instances centroids = kMeans.getClusterCentroids(); 
    for (int i = 0; i < centroids.numInstances(); i++) { 
      System.out.println( "Centroid " + i+1 + ": " + centroids.instance(i)); 
    } 

    // get cluster membership for each instance 
    for (int i = 0; i < dataa.numInstances(); i++) { 
      System.out.println( dataa.instance(i) + " is in cluster " + kMeans.clusterInstance(dataa.instance(i)) + 1); 

    } 

I read data from iris.arff file and it's working. Now I want to give as parameter to k-means my List of double. How can I do it?

Thanks in advance for answers.

Regards.

like image 582
darson1991 Avatar asked Nov 10 '22 02:11

darson1991


1 Answers

If you do not want to create a set of Instances by reading from a DataSource, you can also create it manually, using any of the classes implementing the Instance interface, e.g. a DenseInstance. See the example code from the javadoc:

// Create empty instance with three attribute values
Instance inst = new DenseInstance(3);

// Set instance's values for the attributes "length", "weight", and "position"
inst.setValue(length, 5.3);
inst.setValue(weight, 300);
inst.setValue(position, "first");

// Set instance's dataset to be the dataset "race"
inst.setDataset(race);

Hope that helps.

like image 129
Jan Eglinger Avatar answered Nov 14 '22 21:11

Jan Eglinger