Example usage for weka.classifiers.trees RandomTree buildClassifier

List of usage examples for weka.classifiers.trees RandomTree buildClassifier

Introduction

In this page you can find the example usage for weka.classifiers.trees RandomTree buildClassifier.

Prototype

@Override
public void buildClassifier(Instances data) throws Exception 

Source Link

Document

Builds classifier.

Usage

From source file:controller.MineroControler.java

public String clasificardorArbolAleat(String atributo) {
    BufferedReader breader = null;
    Instances datos = null;//from w w  w . j a  v  a  2s .co  m
    breader = new BufferedReader(fuente_arff);
    try {
        datos = new Instances(breader);
        Attribute atr = datos.attribute(atributo);
        datos.setClass(atr);
        //datos.setClassIndex(0);
    } catch (IOException ex) {
        System.err.println("Problemas al intentar cargar los datos");
        return null;
    }

    RandomTree arbol = new RandomTree(); // Class for constructing a tree that considers K randomly chosen attributes at each node. 

    try {

        arbol.setNumFolds(100);
        arbol.setKValue(0);
        arbol.setMinNum(1);
        arbol.setMaxDepth(0);
        arbol.setSeed(1);
        arbol.buildClassifier(datos);

    } catch (Exception ex) {
        System.err.println("Problemas al ejecutar algorimo de clasificacion" + ex.getLocalizedMessage());
    }
    return arbol.toString();
}

From source file:KFST.featureSelection.embedded.TreeBasedMethods.DecisionTreeBasedMethod.java

License:Open Source License

/**
 * {@inheritDoc }/*from  w w  w  .j a v  a2  s . c  om*/
 */
@Override
protected String buildClassifier(Instances dataTrain) {
    try {
        if (TREE_TYPE == TreeType.C45) {
            J48 decisionTreeC45 = new J48();
            decisionTreeC45.setConfidenceFactor((float) confidenceValue);
            decisionTreeC45.setMinNumObj(minNumSampleInLeaf);
            decisionTreeC45.buildClassifier(dataTrain);
            return decisionTreeC45.toString();
        } else if (TREE_TYPE == TreeType.RANDOM_TREE) {
            RandomTree decisionTreeRandomTree = new RandomTree();
            decisionTreeRandomTree.setKValue(randomTreeKValue);
            decisionTreeRandomTree.setMaxDepth(randomTreeMaxDepth);
            decisionTreeRandomTree.setMinNum(randomTreeMinNum);
            decisionTreeRandomTree.setMinVarianceProp(randomTreeMinVarianceProp);
            decisionTreeRandomTree.buildClassifier(dataTrain);
            return decisionTreeRandomTree.toString();
        }
    } catch (Exception ex) {
        Logger.getLogger(DecisionTreeBasedMethod.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "";
}

From source file:org.uclab.mm.kcl.ddkat.modellearner.ModelLearner.java

License:Apache License

/**
* Method to compute the classification accuracy.
*
* @param algo the algorithm name//from  w  w  w  .jav  a 2s.  c o  m
* @param data the data instances
* @param datanature the dataset nature (i.e. original or processed data)
* @throws Exception the exception
*/
protected String[] modelAccuracy(String algo, Instances data, String datanature) throws Exception {

    String modelResultSet[] = new String[4];
    String modelStr = "";
    Classifier classifier = null;

    // setting class attribute if the data format does not provide this information           
    if (data.classIndex() == -1)
        data.setClassIndex(data.numAttributes() - 1);

    String decisionAttribute = data.attribute(data.numAttributes() - 1).toString();
    String res[] = decisionAttribute.split("\\s+");
    decisionAttribute = res[1];

    if (algo.equals("BFTree")) {

        // Use BFTree classifiers
        BFTree BFTreeclassifier = new BFTree();
        BFTreeclassifier.buildClassifier(data);
        modelStr = BFTreeclassifier.toString();
        classifier = BFTreeclassifier;

    } else if (algo.equals("FT")) {

        // Use FT classifiers
        FT FTclassifier = new FT();
        FTclassifier.buildClassifier(data);
        modelStr = FTclassifier.toString();
        classifier = FTclassifier;

    } else if (algo.equals("J48")) {

        // Use J48 classifiers
        J48 J48classifier = new J48();
        J48classifier.buildClassifier(data);
        modelStr = J48classifier.toString();
        classifier = J48classifier;
        System.out.println("Model String: " + modelStr);

    } else if (algo.equals("J48graft")) {

        // Use J48graft classifiers
        J48graft J48graftclassifier = new J48graft();
        J48graftclassifier.buildClassifier(data);
        modelStr = J48graftclassifier.toString();
        classifier = J48graftclassifier;

    } else if (algo.equals("RandomTree")) {

        // Use RandomTree classifiers
        RandomTree RandomTreeclassifier = new RandomTree();
        RandomTreeclassifier.buildClassifier(data);
        modelStr = RandomTreeclassifier.toString();
        classifier = RandomTreeclassifier;

    } else if (algo.equals("REPTree")) {

        // Use REPTree classifiers
        REPTree REPTreeclassifier = new REPTree();
        REPTreeclassifier.buildClassifier(data);
        modelStr = REPTreeclassifier.toString();
        classifier = REPTreeclassifier;

    } else if (algo.equals("SimpleCart")) {

        // Use SimpleCart classifiers
        SimpleCart SimpleCartclassifier = new SimpleCart();
        SimpleCartclassifier.buildClassifier(data);
        modelStr = SimpleCartclassifier.toString();
        classifier = SimpleCartclassifier;

    }

    modelResultSet[0] = algo;
    modelResultSet[1] = decisionAttribute;
    modelResultSet[2] = modelStr;

    // Collect every group of predictions for J48 model in a FastVector
    FastVector predictions = new FastVector();

    Evaluation evaluation = new Evaluation(data);
    int folds = 10; // cross fold validation = 10
    evaluation.crossValidateModel(classifier, data, folds, new Random(1));
    // System.out.println("Evaluatuion"+evaluation.toSummaryString());
    System.out.println("\n\n" + datanature + " Evaluatuion " + evaluation.toMatrixString());

    // ArrayList<Prediction> predictions = evaluation.predictions();
    predictions.appendElements(evaluation.predictions());

    System.out.println("\n\n 11111");
    // Calculate overall accuracy of current classifier on all splits
    double correct = 0;

    for (int i = 0; i < predictions.size(); i++) {
        NominalPrediction np = (NominalPrediction) predictions.elementAt(i);
        if (np.predicted() == np.actual()) {
            correct++;
        }
    }

    System.out.println("\n\n 22222");
    double accuracy = 100 * correct / predictions.size();
    String accString = String.format("%.2f%%", accuracy);
    modelResultSet[3] = accString;
    System.out.println(datanature + " Accuracy " + accString);

    String modelFileName = algo + "-DDKA.model";

    System.out.println("\n\n 33333");

    ObjectOutputStream oos = new ObjectOutputStream(
            new FileOutputStream("D:\\DDKAResources\\" + modelFileName));
    oos.writeObject(classifier);
    oos.flush();
    oos.close();

    return modelResultSet;

}