Hi Dears!

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

Hi Dears!

Degefe
package cbr;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Writer;
import weka.clusterers.ClusterEvaluation;
import weka.clusterers.SimpleKMeans;
import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Discretize;
/**
 *
 * Cluster class declaration begins here under for SimpleKMeans clustering
 * algorithm
 */
public class Cluster {
private int Instances;
private int Clusters;
private int Clusterer;
private int[] numAttributes;
public static void main(String[] args) throws Exception {
    
 /**
         *
         * load a set of instances from the storage media
         */ 
Instances data = new Instances(new BufferedReader(new
FileReader("C:\\Users\\Degefe\\Desktop\\DataSet Testing 8\\Final_Dataset_for_CBSPD_System.arff")));
    final Instances data1 = data;
data1.setClassIndex(data1.numAttributes() -1);
Discretize filter = new Discretize();
filter.setAttributeIndices("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23");
filter.setUseEqualFrequency(true);
filter.setInputFormat(data1);
Instances dataClusterer = Filter.useFilter(data1, filter);
Discretize discretizeNumeric = new Discretize();
discretizeNumeric.setOptions(new String[]{
 "-0",
 "-M", "-1.0",
 "-B", "10",
 "-R", "first-last"
});
 //train the SimpleKMeans clusterer
SimpleKMeans clusterer = new SimpleKMeans();
clusterer.setNumClusters(4);
clusterer.setSeed(5096);
clusterer.setMaxIterations(2);
String[] options = new String[3];
int nClusters = 4;
int seed = 5096;
int maxIterations = 2;
options[0] = String.valueOf(maxIterations);
options[1] = String.valueOf(nClusters);
options[2] = String.valueOf(seed);
clusterer.setOptions(options);
ClusterEvaluation eval = new ClusterEvaluation();
eval.setClusterer(clusterer);
eval.evaluateClusterer(data1);
System.out.println("# of clusters: " + eval.getNumClusters());
clusterer.buildClusterer(dataClusterer);
eval.setClusterer(clusterer);
eval.evaluateClusterer(data1);
System.out.println(eval.clusterResultsToString());
Instances centroids = clusterer.getClusterCentroids();
int[] ClusterSize = clusterer.getClusterSizes();
File file = new File("CBSPDS_Text_File.txt");
try {Writer writer = new BufferedWriter(new FileWriter(file)); {
    for (int i = 0; i<=5096; i++) {
    for(int j=0; j<ClusterSize[i];j++)   {
String centroidsStr = centroids.instance(i).toString();
String[] to = centroidsStr.split(",");
writer.write(Integer.toString(i));
writer.write(",");
writer.write(data1.instance(j) + "," + "case" + i + "," + (to[0]));
writer.write("\n");
} }
System.out.println("\nInformation: ");
}
}finally{
}
}
}

With the above java code, I am looking the following errors:
# of clusters: 4
Exception in thread "main" weka.core.WekaException: weka.clusterers.SimpleKMeans: Cannot handle any class attribute!
at weka.core.Capabilities.test(Capabilities.java)
at weka.core.Capabilities.test(Capabilities.java)
at weka.core.Capabilities.testWithFail(Capabilities.java)
at weka.clusterers.SimpleKMeans.buildClusterer(SimpleKMeans.java)
at cbr.Cluster.main(Cluster.java)

So please pay your precious time to me having your Golden help, since I am very challenged with this problem. And if it is possible to help me, I will attach the entire data/instance.
Thanks!
With Regards!

_______________________________________________
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: Hi Dears!

Michael Hall


On Oct 23, 2019, at 8:41 AM, Degefe Ayele <[hidden email]> wrote:

data1.setClassIndex(data1.numAttributes() -1);

It doesn’t appear to like this for clustering.

_______________________________________________
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