Example usage for weka.clusterers FilteredClusterer FilteredClusterer

List of usage examples for weka.clusterers FilteredClusterer FilteredClusterer

Introduction

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

Prototype

public FilteredClusterer() 

Source Link

Document

Default constructor.

Usage

From source file:model.clustering.Clustering.java

public String filledFile(Instances data, int numOfClusters, String remove) throws Exception {

    String mainData = data.toString();
    int index = mainData.indexOf("@data");
    String clusterData = mainData.substring(0, index + 6);

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

    kMeansCLusterer = new SimpleKMeans();
    kMeansCLusterer.setNumClusters(numOfClusters);

    FilteredClusterer filteredClusterer = new FilteredClusterer();
    filteredClusterer.setClusterer(kMeansCLusterer);
    filteredClusterer.setFilter(removeFilter);
    filteredClusterer.buildClusterer(data);

    Enumeration<Instance> newData = data.enumerateInstances();

    eval = new ClusterEvaluation();
    eval.setClusterer(filteredClusterer);
    eval.evaluateClusterer(data);/*  ww w .  j a va  2  s .co m*/

    while (newData.hasMoreElements()) {

        Instance i = (Instance) newData.nextElement();
        int kluster = filteredClusterer.clusterInstance(i);
        String instanceString = i.toString() + "," + kluster;
        clusterData = clusterData + instanceString + "\n";

    }
    return clusterData;
}

From source file:nl.uva.sne.commons.ClusterUtils.java

public static Map<String, String> bulidClusters(Clusterer clusterer, Instances data, String inDir)
        throws Exception {

    FilteredClusterer fc = new FilteredClusterer();
    String[] options = new String[2];
    options[0] = "-R"; // "range"
    options[1] = "1"; // we want to ignore the attribute that is in the position '1'
    Remove remove = new Remove(); // new instance of filter
    remove.setOptions(options); // set options

    fc.setFilter(remove); //add filter to remove attributes
    fc.setClusterer(clusterer); //bind FilteredClusterer to original clusterer
    fc.buildClusterer(data);//from   w  w  w . ja v a2s.  c  o m

    Map<String, String> clusters = new HashMap<>();
    for (int i = 0; i < data.numInstances(); i++) {
        Instance inst = data.instance(i);
        int theClass = fc.clusterInstance(inst);
        String s = data.attribute(0).value(i);
        clusters.put(inDir + File.separator + s, String.valueOf(theClass));
        System.err.println(s + " is in cluster " + theClass);
    }
    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(fc); // the cluster to evaluate
    eval.evaluateClusterer(data); // data to evaluate the clusterer on
    //        double ll = eval.getLogLikelihood();
    //        Logger.getLogger(ClusterUtils.class.getName()).log(Level.INFO, "LogLikelihood :{0}", ll);
    //
    //        if (clusterer instanceof SimpleKMeans) {
    //            double sqrErr = ((SimpleKMeans) clusterer).getSquaredError();
    //            Logger.getLogger(ClusterUtils.class.getName()).log(Level.INFO, "Squared Error:{0}", sqrErr);
    //        }

    Logger.getLogger(ClusterUtils.class.getName()).log(Level.INFO, "# of clusters: {0}", eval.getNumClusters());
    Logger.getLogger(ClusterUtils.class.getName()).log(Level.INFO, "clusterResults: {0}",
            eval.clusterResultsToString());

    return clusters;
}

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();/*from ww w  .  j a v a 2 s  .  c  o  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();

}