Example usage for weka.clusterers ClusterEvaluation evaluateClusterer

List of usage examples for weka.clusterers ClusterEvaluation evaluateClusterer

Introduction

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

Prototype

public void evaluateClusterer(Instances test) throws Exception 

Source Link

Document

Evaluate the clusterer on a set of instances.

Usage

From source file:rdfsystem.data.DataMining.java

public static String cluster(RdfManager manager) throws Exception {
    Instances ins = transformData(manager, false);
    SimpleKMeans cls = new SimpleKMeans();
    String[] options = "-N 5".split(" ");
    cls.setOptions(options);/*  ww w.ja va 2 s. co m*/
    cls.buildClusterer(ins);
    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(cls);
    eval.evaluateClusterer(ins);
    return eval.clusterResultsToString();
}

From source file:sirius.clustering.main.TrainClustererPane.java

License:Open Source License

private void start() {
    if (this.fileTextField.getText().length() == 0) {
        JOptionPane.showMessageDialog(parent, "Please choose training file!", "Error",
                JOptionPane.ERROR_MESSAGE);
        return;//from  w  ww . j  av  a  2  s  .  c  om
    }
    if (m_ClustererEditor.getValue() == null) {
        JOptionPane.showMessageDialog(parent, "Please choose clustering method!", "Error",
                JOptionPane.ERROR_MESSAGE);
        return;
    }
    if (clusterThread != null) {
        JOptionPane.showMessageDialog(parent, "Cannot start training of Clusterer as another is running!",
                "Error", JOptionPane.ERROR_MESSAGE);
        return;
    }
    this.startButton.setEnabled(false);
    this.stopButton.setEnabled(true);
    this.numberOfClusterTextField.setText("");
    clusterThread = (new Thread() {
        public void run() {
            try {
                Instances inst = new Instances(new BufferedReader(new FileReader(fileTextField.getText())));
                inst.setClassIndex(m_ClassCombo.getSelectedIndex());
                if (inst.classAttribute().isNumeric()) {
                    JOptionPane.showMessageDialog(parent, "Class must be nominal!", "Error",
                            JOptionPane.ERROR_MESSAGE);
                } else {
                    outputTextArea.setText("");
                    clusterer = (Clusterer) m_ClustererEditor.getValue();
                    statusLabel.setText(" Training Clusterer..");
                    clusterer.buildClusterer(removeClass(inst));
                    ClusterEvaluation eval = new ClusterEvaluation();
                    eval.setClusterer(clusterer);
                    eval.evaluateClusterer(inst);
                    outputTextArea.append(eval.clusterResultsToString());
                    outputTextArea.append("\n");
                    if (clusterer != null) {
                        numberOfClusterTextField.setText("" + clusterer.numberOfClusters());
                        statusLabel.setText(" Clusterer Trained..");
                    }
                }
                startButton.setEnabled(true);
                stopButton.setEnabled(false);
                clusterThread = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    clusterThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority
    clusterThread.start();
}

From source file:soccer.core.ASimplePractice.java

public void evaluate() throws IOException, Exception {
    Instances data = loader.getInstances();
    SimpleKMeans cluster = new SimpleKMeans();
    cluster.setNumClusters(4);//from  ww w.  ja  va 2  s .  c  om
    cluster.buildClusterer(data);
    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(cluster);
    eval.evaluateClusterer(data);
    System.out.println(eval.clusterResultsToString());
}

From source file:soccer.core.classifiers.BookKeeperConsistencyClassifier.java

public static void main(String[] args) throws Exception {
    BookKeeperConsistency bkc = new BookKeeperConsistency();
    Instances data = bkc.getInstances();

    RemoveWithValues rwv = new RemoveWithValues();
    rwv.setOptions(new String[] { "-C", "4", "-S", "6", "-V" });
    rwv.setInputFormat(data);/*from w w  w  . j ava2s  .  c o  m*/
    data = Filter.useFilter(data, rwv);
    RemoveWithValues rwv1 = new RemoveWithValues();
    rwv1.setOptions(new String[] { "-C", "6", "-S", "6", "-V" });
    rwv1.setInputFormat(data);
    data = Filter.useFilter(data, rwv1);

    //        Normalize nm = new Normalize();
    //        nm.setOptions(new String[]{
    //            "-S", "100"
    //        });
    //        nm.setInputFormat(data);
    //        data = Filter.useFilter(data, nm);

    Remove rm = new Remove();
    rm.setOptions(new String[] { "-R", "2-last" });
    rm.setInputFormat(data);
    Instances newData = Filter.useFilter(data, rm);

    SimpleKMeans cluster = new SimpleKMeans();
    cluster.setOptions(new String[] { "-N", "2", "-A", "weka.core.ManhattanDistance" });

    cluster.buildClusterer(newData);
    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(cluster);
    eval.evaluateClusterer(newData);
    System.out.println(eval.clusterResultsToString());
    //        for (int i = 0; i < newData.size(); i++) {
    //            Instance instance = newData.get(i);
    //            if (cluster.clusterInstance(instance) == 0) {
    //                System.out.println(data.get(i).toString());
    //            }
    //        }
}

From source file:soccer.core.models.PlayerModel.java

public static void main(String[] args) throws Exception {
    PlayerModel pm = new PlayerModel();
    Instances data = pm.buildInstance();
    SimpleKMeans cluster = new SimpleKMeans();
    cluster.setNumClusters(4);/*from  w  ww.  j a v a2  s .co m*/
    cluster.buildClusterer(data);
    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(cluster);
    eval.evaluateClusterer(data);
    System.out.println(eval.clusterResultsToString());
}

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");
    }//w w w  . jav  a  2s .  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;/*w  ww  . j  a  v  a2 s.com*/
    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);
}

From source file:view.centerPanels.ClusteringPredictPnlCenter.java

private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed

    Instances test = new Instances(Data.getInstance().getInstances());
    test.delete();//  w ww .  j  av a  2s. co  m

    //proverava da li su dobro unete vrednosti
    //ako nesto nije doro uneseno nekaa iskoci JoptionPane
    //sta je lose uneseno, naziv aributa recimo
    for (int i = 0; i < fields.size(); i++) {
        String text = fields.get(i).getText().trim();

        //prekace prazna pollja jer za klasterizaciju znaci da se ona ignorisu
        //to za klasifikaciju nije slucaj
        if (!text.equals("")) {

            if (test.attribute(i).isNominal()) {
                boolean correct = false;
                for (int j = 0; j < test.attribute(i).numValues(); j++) {
                    if (text.equals(test.attribute(i).value(j))) {
                        correct = true;
                    }
                }
                if (!correct) {
                    JOptionPane.showMessageDialog(this,
                            "Incorrect format for attribute " + test.attribute(i).name());
                    break;
                }
            }

            if (test.attribute(i).isNumeric()) {
                try {
                    double value = Double.parseDouble(text);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(this,
                            "Incorrect format for attribute " + test.attribute(i).name());
                    break;
                }
            }

        }
    }

    int numAttributes = test.numAttributes();

    Instance instance = new Instance(numAttributes);

    //ovaj remove je potreban samo zaklasterizaciju
    String remove = "";

    boolean hasRemove = false;
    for (int i = 0; i < fields.size(); i++) {
        String text = fields.get(i).getText().trim();

        //vama ne sme da se pojavi prazan string
        if (text.equals("")) {
            remove = remove + (i + 1) + ",";
            hasRemove = true;
        } else {
            try {
                double value = Double.parseDouble(text);
                instance.setValue(i, value);

            } catch (Exception e) {

                instance.setValue(i, text);
            }
        }

    }
    if (hasRemove) {
        remove = remove.substring(0, remove.length() - 1);
    }

    //meni se InstanceS zove test a vama instances, ovako se dodaje ta jedna instanca
    test.add(instance);
    //sad radite vasu evaluaciju ovo je klaserizacija ostalo

    Remove removeFilter = new Remove();
    removeFilter.setAttributeIndices(remove);

    FilteredClusterer filteredClusterer = new FilteredClusterer();
    try {

        filteredClusterer.setClusterer(kMeans);
        filteredClusterer.setFilter(removeFilter);
        filteredClusterer.buildClusterer(Data.getInstance().getInstances());

    } catch (Exception e) {

    }

    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(filteredClusterer);
    try {
        eval.evaluateClusterer(test);
    } catch (Exception ex) {
        Logger.getLogger(ClusteringPredictPnlCenter.class.getName()).log(Level.SEVERE, null, ex);
    }

    String[] results = eval.clusterResultsToString().split("\n");

    String cluster = results[results.length - 1].split(" ")[0];

    textAreaResult.setText("This instance belongs to \ncluster number:  " + cluster + ".\n\n"
            + "Take a look on visualization \nfor better feeleing about \nthis instance");

    test.delete();

}

From source file:wekimini.InputGenerator.java

public void selectEmClusters() throws Exception {
    String[] options = new String[2];
    options[0] = "-I";
    options[1] = "100";

    buildDataset();//from ww w .ja  va  2  s.  c o m

    EM clusterer = new EM();
    clusterer.setOptions(options);
    clusterer.buildClusterer(dataset);

    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(clusterer);
    eval.evaluateClusterer(dataset);
    System.out.println(eval.clusterResultsToString());

    double[][][] clusterAtts = clusterer.getClusterModelsNumericAtts();
    System.out.println(Arrays.deepToString(clusterAtts));

    numClusters = clusterer.numberOfClusters();
    System.out.println(numClusters);

    addEmClustersToTraining(clusterAtts);
}