Example usage for weka.clusterers ClusterEvaluation getClassesToClusters

List of usage examples for weka.clusterers ClusterEvaluation getClassesToClusters

Introduction

In this page you can find the example usage for weka.clusterers ClusterEvaluation getClassesToClusters.

Prototype

public int[] getClassesToClusters() 

Source Link

Document

Return the array (ordered by cluster number) of minimum error class to cluster mappings

Usage

From source file:tr.gov.ulakbim.jDenetX.experiments.wrappers.EvalActiveBoostingID.java

License:Open Source License

public Instances clusteredInstances(Instances data) {
    if (data == null) {
        throw new NullPointerException("Data is null at clusteredInstances method");
    }/*from ww w. j a  v a  2 s . com*/
    Instances sampled_data = data;
    for (int i = 0; i < sampled_data.numInstances(); i++) {
        sampled_data.remove(i);
    }

    SimpleKMeans sKmeans = new SimpleKMeans();
    data.setClassIndex(data.numAttributes() - 1);
    Remove filter = new Remove();
    filter.setAttributeIndices("" + (data.classIndex() + 1));
    List assignments = new ArrayList();

    try {
        filter.setInputFormat(data);
        Instances dataClusterer = Filter.useFilter(data, filter);
        String[] options = new String[3];
        options[0] = "-I"; // max. iterations
        options[1] = "500";
        options[2] = "-O";
        sKmeans.setNumClusters(data.numClasses());
        sKmeans.setOptions(options);
        sKmeans.buildClusterer(dataClusterer);
        System.out.println("Kmeans\n:" + sKmeans);
        System.out.println(Arrays.toString(sKmeans.getAssignments()));
        assignments = Arrays.asList(sKmeans.getAssignments());
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("Assignments\n: " + assignments);
    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(sKmeans);
    try {
        eval.evaluateClusterer(data);
    } catch (Exception e) {
        e.printStackTrace();
    }
    int classesToClustersMap[] = eval.getClassesToClusters();
    for (int i = 0; i < classesToClustersMap.length; i++) {
        if (assignments.get(i).equals(((Integer) classesToClustersMap[(int) data.get(i).classValue()]))) {
            ((Instances) sampled_data).add(data.get(i));
        }
    }
    return ((Instances) sampled_data);
}

From source file:tr.gov.ulakbim.jDenetX.experiments.wrappers.EvalActiveBoostingID.java

License:Open Source License

public static Instances clusterInstances(Instances data) {
    XMeans xmeans = new XMeans();
    Remove filter = new Remove();
    Instances dataClusterer = null;/*from w w w.  j a v a  2s  . c  o m*/
    if (data == null) {
        throw new NullPointerException("Data is null at clusteredInstances method");
    }
    //Get the attributes from the data for creating the sampled_data object

    ArrayList<Attribute> attrList = new ArrayList<Attribute>();
    Enumeration attributes = data.enumerateAttributes();
    while (attributes.hasMoreElements()) {
        attrList.add((Attribute) attributes.nextElement());
    }

    Instances sampled_data = new Instances(data.relationName(), attrList, 0);
    data.setClassIndex(data.numAttributes() - 1);
    sampled_data.setClassIndex(data.numAttributes() - 1);
    filter.setAttributeIndices("" + (data.classIndex() + 1));
    data.remove(0);//In Wavelet Stream of MOA always the first element comes without class

    try {
        filter.setInputFormat(data);
        dataClusterer = Filter.useFilter(data, filter);
        String[] options = new String[4];
        options[0] = "-L"; // max. iterations
        options[1] = Integer.toString(noOfClassesInPool - 1);
        if (noOfClassesInPool > 2) {
            options[1] = Integer.toString(noOfClassesInPool - 1);
            xmeans.setMinNumClusters(noOfClassesInPool - 1);
        } else {
            options[1] = Integer.toString(noOfClassesInPool);
            xmeans.setMinNumClusters(noOfClassesInPool);
        }
        xmeans.setMaxNumClusters(data.numClasses() + 1);
        System.out.println("No of classes in the pool: " + noOfClassesInPool);
        xmeans.setUseKDTree(true);
        //xmeans.setOptions(options);
        xmeans.buildClusterer(dataClusterer);
        System.out.println("Xmeans\n:" + xmeans);
    } catch (Exception e) {
        e.printStackTrace();
    }
    //System.out.println("Assignments\n: " + assignments);
    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(xmeans);
    try {
        eval.evaluateClusterer(data);
        int classesToClustersMap[] = eval.getClassesToClusters();
        //check the classes to cluster map
        int clusterNo = 0;
        for (int i = 0; i < data.size(); i++) {
            clusterNo = xmeans.clusterInstance(dataClusterer.get(i));
            //Check if the class value of instance and class value of cluster matches
            if ((int) data.get(i).classValue() == classesToClustersMap[clusterNo]) {
                sampled_data.add(data.get(i));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ((Instances) sampled_data);
}