K means weka java code

I have read many examples of using this library in Java, and clustering is possible from the ARFF data file, and it works.

But I have my data in the List of double, which is generated when working with my program, and I do not know how to use this k-means algorithm to cluster my data. This is a one-dimensional list.

This 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 am reading data from iris.arff file and it works. Now I want to specify the k-value of my List of double as a parameter. How can i do this?

Thanks in advance for your answers.

Sincerely.

+4
source share
1 answer

Instances DataSource, , , Instance, a DenseInstance. . javadoc:

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

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

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

, .

+1

All Articles