Example usage for weka.classifiers Evaluation crossValidateModel

List of usage examples for weka.classifiers Evaluation crossValidateModel

Introduction

In this page you can find the example usage for weka.classifiers Evaluation crossValidateModel.

Prototype

public void crossValidateModel(Classifier classifier, Instances data, int numFolds, Random random)
        throws Exception 

Source Link

Document

Performs a (stratified if class is nominal) cross-validation for a classifier on a set of instances.

Usage

From source file:com.sliit.views.KNNView.java

void getRocCurve() {
    try {/*  w  ww. j a v a  2  s.c o  m*/
        Instances data;
        data = new Instances(new BufferedReader(new java.io.FileReader(PredictorPanel.modalText.getText())));
        data.setClassIndex(data.numAttributes() - 1);

        // train classifier
        Classifier cl = new NaiveBayes();
        Evaluation eval = new Evaluation(data);
        eval.crossValidateModel(cl, data, 10, new Random(1));

        // generate curve
        ThresholdCurve tc = new ThresholdCurve();
        int classIndex = 0;
        Instances result = tc.getCurve(eval.predictions(), classIndex);

        // plot curve
        ThresholdVisualizePanel vmc = new ThresholdVisualizePanel();
        vmc.setROCString("(Area under ROC = " + Utils.doubleToString(tc.getROCArea(result), 4) + ")");
        vmc.setName(result.relationName());
        PlotData2D tempd = new PlotData2D(result);
        tempd.setPlotName(result.relationName());
        tempd.addInstanceNumberAttribute();
        // specify which points are connected
        boolean[] cp = new boolean[result.numInstances()];
        for (int n = 1; n < cp.length; n++) {
            cp[n] = true;
        }
        tempd.setConnectPoints(cp);
        // add plot
        vmc.addPlot(tempd);

        rocPanel.removeAll();
        rocPanel.add(vmc, "vmc", 0);
        rocPanel.revalidate();

    } catch (IOException ex) {
        Logger.getLogger(DataVisualizerPanel.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(DataVisualizerPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.sliit.views.SVMView.java

/**
 * draw ROC curve/*from ww w.  j a v a  2  s.  com*/
 */
void getRocCurve() {
    try {
        Instances data;
        data = new Instances(new BufferedReader(new FileReader(PredictorPanel.modalText.getText())));
        data.setClassIndex(data.numAttributes() - 1);

        //train classifier
        Classifier cl = new NaiveBayes();
        Evaluation eval = new Evaluation(data);
        eval.crossValidateModel(cl, data, 10, new Random(1));

        // generate curve
        ThresholdCurve tc = new ThresholdCurve();
        int classIndex = 0;
        Instances result = tc.getCurve(eval.predictions(), classIndex);

        // plot curve
        ThresholdVisualizePanel vmc = new ThresholdVisualizePanel();
        vmc.setROCString("(Area under ROC = " + Utils.doubleToString(tc.getROCArea(result), 4) + ")");
        vmc.setName(result.relationName());
        PlotData2D tempd = new PlotData2D(result);
        tempd.setPlotName(result.relationName());
        tempd.addInstanceNumberAttribute();
        // specify which points are connected
        boolean[] cp = new boolean[result.numInstances()];
        for (int n = 1; n < cp.length; n++) {
            cp[n] = true;
        }
        tempd.setConnectPoints(cp);
        // add plot
        vmc.addPlot(tempd);

        //            rocPanel.removeAll();
        //            rocPanel.add(vmc, "vmc", 0);
        //            rocPanel.revalidate();
    } catch (IOException ex) {
        Logger.getLogger(DataVisualizerPanel.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(DataVisualizerPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Control.Classificador.java

public ArrayList<Resultado> classificar(Plano plano, Arquivo arq) {
    try {// w  ww  .j ava  2 s  .  co m
        FileReader leitor = new FileReader(arq.arquivo);
        Instances conjunto = new Instances(leitor);
        conjunto.setClassIndex(conjunto.numAttributes() - 1);
        Evaluation avaliacao = new Evaluation(conjunto);
        conjunto = conjunto.resample(new Random());

        Instances baseTreino = null, baseTeste = null;
        Random rand = new Random(1);

        if (plano.eHoldOut) {
            baseTeste = conjunto.testCV(3, 0);
            baseTreino = conjunto.trainCV(3, 0);
        } else {
            baseTeste = baseTreino = conjunto;
        }

        if (plano.IBK) {
            try {
                IB1 vizinho = new IB1();
                vizinho.buildClassifier(baseTeste);
                avaliacao.crossValidateModel(vizinho, baseTeste,
                        (plano.eHoldOut) ? 4 : baseTeste.numInstances(), rand);
                Resultado resultado = new Resultado("NN",
                        avaliacao.toMatrixString("Algortmo Vizinho Mais Prximo - Matriz de Confuso"),
                        avaliacao.toClassDetailsString("kNN"));
                resultado.setTaxaErro(avaliacao.errorRate());
                resultado.setTaxaAcerto(1 - avaliacao.errorRate());
                resultado.setRevocacao(recallToDouble(avaliacao, baseTeste));
                resultado.setPrecisao(precisionToDouble(avaliacao, baseTeste));
                this.resultados.add(resultado);
            } catch (UnsupportedAttributeTypeException ex) {
                Mensagem.erro("Algortmo IB1 no suporta atributos numricos!", "MTCS - ERRO");
            }
        }
        if (plano.J48) {
            try {
                J48 j48 = new J48();
                j48.buildClassifier(baseTeste);
                avaliacao.crossValidateModel(j48, baseTeste, (plano.eHoldOut) ? 4 : baseTeste.numInstances(),
                        rand);
                Resultado resultado = new Resultado("J48",
                        avaliacao.toMatrixString("Algortmo J48 - Matriz de Confuso"),
                        avaliacao.toClassDetailsString("J48"));
                resultado.setTaxaErro(avaliacao.errorRate());
                resultado.setTaxaAcerto(1 - avaliacao.errorRate());
                resultado.setRevocacao(recallToDouble(avaliacao, baseTeste));
                resultado.setPrecisao(precisionToDouble(avaliacao, baseTeste));
                this.resultados.add(resultado);
            } catch (UnsupportedAttributeTypeException ex) {
                Mensagem.erro("Algortmo J48 no suporta atributos nominais!", "MTCS - ERRO");
            }
        }
        if (plano.KNN) {
            try {
                IBk knn = new IBk(3);
                knn.buildClassifier(baseTeste);
                avaliacao.crossValidateModel(knn, baseTeste, (plano.eHoldOut) ? 4 : baseTeste.numInstances(),
                        rand);
                Resultado resultado = new Resultado("KNN",
                        avaliacao.toMatrixString("Algortmo KNN - Matriz de Confuso"),
                        avaliacao.toClassDetailsString("kNN"));
                resultado.setTaxaErro(avaliacao.errorRate());
                resultado.setTaxaAcerto(1 - avaliacao.errorRate());
                resultado.setRevocacao(recallToDouble(avaliacao, baseTeste));
                resultado.setPrecisao(precisionToDouble(avaliacao, baseTeste));
                this.resultados.add(resultado);
            } catch (UnsupportedAttributeTypeException ex) {
                Mensagem.erro("Algortmo KNN no suporta atributos numricos!", "MTCS - ERRO");
            }

        }
        if (plano.Naive) {
            NaiveBayes naive = new NaiveBayes();
            naive.buildClassifier(baseTeste);
            avaliacao.crossValidateModel(naive, baseTeste, (plano.eHoldOut) ? 4 : baseTeste.numInstances(),
                    rand);
            Resultado resultado = new Resultado("Naive",
                    avaliacao.toMatrixString("Algortmo NaiveBayes - Matriz de Confuso"),
                    avaliacao.toClassDetailsString("kNN"));
            resultado.setTaxaErro(avaliacao.errorRate());
            resultado.setTaxaAcerto(1 - avaliacao.errorRate());
            resultado.setRevocacao(recallToDouble(avaliacao, baseTeste));
            resultado.setPrecisao(precisionToDouble(avaliacao, baseTeste));
            this.resultados.add(resultado);
        }
        if (plano.Tree) {
            try {
                Id3 id3 = new Id3();
                id3.buildClassifier(baseTeste);
                avaliacao.crossValidateModel(id3, baseTeste, (plano.eHoldOut) ? 4 : baseTeste.numInstances(),
                        rand);
                Resultado resultado = new Resultado("ID3",
                        avaliacao.toMatrixString("Algortmo ID3 - Matriz de Confuso"),
                        avaliacao.toClassDetailsString("kNN"));
                resultado.setTaxaErro(avaliacao.errorRate());
                resultado.setTaxaAcerto(1 - avaliacao.errorRate());
                resultado.setRevocacao(recallToDouble(avaliacao, baseTeste));
                resultado.setPrecisao(precisionToDouble(avaliacao, baseTeste));
                this.resultados.add(resultado);
            } catch (UnsupportedAttributeTypeException ex) {
                Mensagem.erro("Algortmo Arvore de Deciso no suporta atributos numricos!",
                        "MTCS - ERRO");

            }
        }

    } catch (FileNotFoundException ex) {

        Logger.getLogger(Classificador.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Classificador.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NullPointerException ex) {
        Mensagem.erro("Selecione um arquivo para comear!", "MTCS - ERRO");
        Logger.getLogger(Classificador.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(Classificador.class.getName()).log(Level.SEVERE, null, ex);
    }

    return this.resultados;
}

From source file:Controller.CtlDataMining.java

public String redBayesiana(Instances data) {
    try {/*  ww w  .j av  a  2s  .c o m*/
        //Creamos un clasificador Bayesiano                
        NaiveBayes nb = new NaiveBayes();

        //creamos el clasificador de la redBayesiana 
        nb.buildClassifier(data);

        //Creamos un objeto para la validacion del modelo con redBayesiana
        Evaluation evalB = new Evaluation(data);

        /*Aplicamos el clasificador bayesiano
        hacemos validacion cruzada, de redBayesiana, con 10 campos, 
        y un aleatorio para la semilla, en este caso es 1 para el 
        muestreo de la validacion cruzada (Como ordenar para luego
        partirlo en 10)*/
        evalB.crossValidateModel(nb, data, 10, new Random(1));

        String resBay = "<br><br><b><center>Resultados NaiveBayes</center>" + "<br>========<br>"
                + "Modelo generado indica los siguientes resultados:" + "<br>========<br></b>";
        //Obtenemos resultados
        resBay = resBay
                + ("<b>1. Numero de instancias clasificadas:</b> " + (int) evalB.numInstances() + "<br>");
        resBay = resBay + ("<b>2. Porcentaje de instancias correctamente " + "clasificadas:</b> "
                + formato.format(evalB.pctCorrect()) + "%<br>");
        resBay = resBay + ("<b>3. Numero de instancias correctamente " + "clasificadas:</b> "
                + (int) evalB.correct() + "<br>");
        resBay = resBay + ("<b>4. Porcentaje de instancias incorrectamente " + "clasificadas:</b> "
                + formato.format(evalB.pctIncorrect()) + "%<br>");
        resBay = resBay + ("<b>5. Numero de instancias incorrectamente " + "clasificadas:</b> "
                + (int) evalB.incorrect() + "<br>");
        resBay = resBay + ("<b>6. Media del error absoluto:</b> " + formato.format(evalB.meanAbsoluteError())
                + "%<br>");
        resBay = resBay
                + ("<b>7. " + evalB.toMatrixString("Matriz de " + "confusion</b>").replace("\n", "<br>"));

        return resBay;

    } catch (Exception e) {
        return "El error es" + e.getMessage();
    }
}

From source file:Controller.CtlDataMining.java

public String arbolJ48(Instances data) {
    try {//w  ww .  jav a  2s. co  m
        // Creamos un clasidicador J48
        J48 j48 = new J48();
        //creamos el clasificador  del J48 con los datos 
        j48.buildClassifier(data);

        //Creamos un objeto para la validacion del modelo con redBayesiana
        Evaluation evalJ48 = new Evaluation(data);

        /*Aplicamos el clasificador J48
        hacemos validacion cruzada, de redBayesiana, con 10 campos, 
        y el aleatorio arrancando desde 1 para la semilla*/
        evalJ48.crossValidateModel(j48, data, 10, new Random(1));
        //Obtenemos resultados
        String resJ48 = "<br><b><center>Resultados Arbol de decision J48"
                + "</center><br>========<br>Modelo generado indica los "
                + "siguientes resultados:<br>========<br></b>";

        resJ48 = resJ48
                + ("<b>1. Numero de instancias clasificadas:</b> " + (int) evalJ48.numInstances() + "<br>");
        resJ48 = resJ48 + ("<b>2. Porcentaje de instancias correctamente " + "clasificadas:</b> "
                + formato.format(evalJ48.pctCorrect()) + "<br>");
        resJ48 = resJ48 + ("<b>3. Numero de instancias correctamente " + "clasificadas:</b>"
                + (int) evalJ48.correct() + "<br>");
        resJ48 = resJ48 + ("<b>4. Porcentaje de instancias incorrectamente " + "clasificadas:</b> "
                + formato.format(evalJ48.pctIncorrect()) + "<br>");
        resJ48 = resJ48 + ("<b>5. Numero de instancias incorrectamente " + "clasificadas:</b> "
                + (int) evalJ48.incorrect() + "<br>");
        resJ48 = resJ48 + ("<b>6. Media del error absoluto:</b> " + formato.format(evalJ48.meanAbsoluteError())
                + "<br>");
        resJ48 = resJ48
                + ("<b>7. " + evalJ48.toMatrixString("Matriz de" + " confusion</b>").replace("\n", "<br>"));

        // SE GRAFICA EL ARBOL GENERADO
        //Se crea un Jframe Temporal
        final javax.swing.JFrame jf = new javax.swing.JFrame("Arbol de decision: J48");
        /*Se asigna un tamao*/
        jf.setSize(500, 400);
        /*Se define un borde*/
        jf.getContentPane().setLayout(new BorderLayout());
        /*Se instancia la grafica del arbol, estableciendo el tipo J48
        Parametros (Listener, Tipo de arbol, Tipo de nodos)
        El placeNode2 colocar los nodos para que caigan en forma uniforme
        por debajo de su padre*/
        TreeVisualizer tv = new TreeVisualizer(null, j48.graph(), new PlaceNode2());
        /*Aade el arbol centrandolo*/
        jf.getContentPane().add(tv, BorderLayout.CENTER);
        /*Aadimos un listener para la X del close*/
        jf.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(java.awt.event.WindowEvent e) {
                jf.dispose();
            }
        });
        /*Lo visualizamos*/
        jf.setVisible(true);
        /*Ajustamos el arbol al ancho del JFRM*/
        tv.fitToScreen();

        return resJ48;

    } catch (Exception e) {
        return "El error es" + e.getMessage();

    }
}

From source file:core.classification.Classifiers.java

License:Open Source License

public void trainSC() throws Exception {
    String sql;//from www . jav  a2s.  c  o  m

    // ---
    // Connect to the database
    // ---
    InstanceQuery query = new InstanceQuery();
    query.setDatabaseURL(dbase);
    query.setUsername("");
    query.setPassword("");

    // ---
    // ---
    // SCA
    // ---
    // ---

    sql = "SELECT ";
    sql += "CR.ratio, CR.class ";
    sql += "FROM Class_ratio AS CR;";

    query.setQuery(sql);
    Instances data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    String[] options = Utils.splitOptions(
            "-D -Q weka.classifiers.bayes.net.search.local.K2 -- -P 1 -S BAYES -E weka.classifiers.bayes.net.estimate.SimpleEstimator -- -A 0.5");
    SCA.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train the classifier
    // ---
    System.out.println("Building SCA ...");
    SCA.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Classifier evaluation
    // ---
    System.out.println("Cross-validation for SCA...");
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(SCA, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for SCA: \n\n", false));

    // ---
    // ---
    // SCB
    // ---
    // ---

    sql = "SELECT ";
    sql += "Data.H2, Data.D2, Data.DX, ";
    sql += "Data.PARENT_CHAR AS PCLASS, ";
    sql += "Data.CLASS ";
    sql += "FROM Data ";
    sql += "WHERE (((Data.SEGERR)=0) AND (Data.PARENT_CHAR<>'0') );";

    query.setQuery(sql);
    data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a");
    SCB.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train the classifier
    // ---
    System.out.println("Building SCB ...");
    SCB.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Classifier evaluation
    // ---
    System.out.println("Cross-validation for SCB...");
    eval = new Evaluation(data);
    eval.crossValidateModel(SCB, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for SCB: \n\n", false));

    // ---
    // ---
    // SCC
    // ---
    // ---

    // ----
    // SCC1
    // ----

    sql = "SELECT ";
    sql += "Data.LH, Data.LD, Data.LDX, Data.LCLASS, ";
    sql += "Data.CLASS ";
    sql += "FROM Data ";
    sql += "WHERE ( (Data.SEGERR)=0  AND ( (Data.LCLASS)<>'0' ) );";

    query.setQuery(sql);
    data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a");
    SCC1.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train the classifier
    // ---
    System.out.println("Building SCC1 ...");
    SCC1.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Classifier evaluation
    // ---
    System.out.println("Cross-validation for SCC1...");
    eval = new Evaluation(data);
    eval.crossValidateModel(SCC1, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for SCC1: \n\n", false));

    // ----
    // SCC2
    // ----

    sql = "SELECT ";
    sql += "Data.EH, Data.ED, Data.EDX, Data.ECLASS, ";
    sql += "Data.CLASS ";
    sql += "FROM Data ";
    sql += "WHERE ( (Data.SEGERR)=0  AND ( (Data.ECLASS)<>'0' ) );";

    query.setQuery(sql);
    data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    //      options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a");
    SCC2.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train the classifier
    // ---
    System.out.println("Building SCC2 ...");
    SCC2.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Classifier evaluation
    // ---
    System.out.println("Cross-validation for SCC2...");
    eval = new Evaluation(data);
    eval.crossValidateModel(SCC2, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for SCC2: \n\n", false));

    // ----
    // SCC3
    // ----

    sql = "SELECT ";
    sql += "Data.SH, Data.SD, Data.SDX, Data.SCLASS, ";
    sql += "Data.CLASS ";
    sql += "FROM Data ";
    sql += "WHERE ( (Data.SEGERR)=0  AND ( (Data.SCLASS)<>'0' ) );";

    query.setQuery(sql);
    data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    //      options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a");
    SCC3.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train the classifier
    // ---
    System.out.println("Building SCC3 ...");
    SCC3.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Classifier evaluation
    // ---
    System.out.println("Cross-validation for SCC3...");
    eval = new Evaluation(data);
    eval.crossValidateModel(SCC3, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for SCC3: \n\n", false));

}

From source file:core.classification.Classifiers.java

License:Open Source License

public void trainRC() throws Exception {
    // ---// w w w .j a  va 2 s.co m
    // Retrieve the instances in the database
    // ---
    InstanceQuery query = new InstanceQuery();
    query.setDatabaseURL(dbase);
    query.setUsername("");
    query.setPassword("");

    String sql = "SELECT ";
    sql += "Data.H2, Data.D2, Data.DX, ";
    sql += "Data.CLASS, Data.PARENT_CHAR AS PCLASS, ";
    sql += "Data.RELID ";
    sql += "FROM Data ";
    sql += "WHERE (((Data.SEGERR)=0) AND (Data.PARENT_CHAR<>'0') );";

    query.setQuery(sql);
    Instances data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    //      String[] options = Utils.splitOptions("-L 0.2 -M 0.2 -N 50 -V 0 -S 0 -E 20 -H 5 ");
    String[] options = Utils.splitOptions(
            "-cost-matrix \"[0.0 1.0 1.0 0.1 0.1; 1.0 0.0 1.0 0.1 0.1; 1.0 1.0 0.0 0.1 0.1; 10.0 10.0 10.0 0.0 1.0; 10.0 10.0 10.0 1.0 0.0]\" -S 1 -W weka.classifiers.functions.MultilayerPerceptron -- -L 0.2 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a");
    RC.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train
    // ---
    System.out.println("Building RC...");
    RC.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Evaluation
    // ---
    System.out.println("Cross-validation for RC...");
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(RC, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for RC: \n\n", false));

}

From source file:core.classification.Classifiers.java

License:Open Source License

public void trainYNC() throws Exception {
    // ---//from   w ww  . j  a  v a 2  s.c om
    // Retrieve the instances in the database
    // ---
    InstanceQuery query = new InstanceQuery();
    query.setDatabaseURL(dbase);
    query.setUsername("");
    query.setPassword("");

    String sql = "SELECT ";
    sql += "YNCdata.PCLASS, YNCdata.CCLASS, YNCdata.RAREA, YNCdata.H, YNCdata.D, YNCdata.V, ";
    sql += "YNCdata.YN ";
    sql += "FROM YNCdata ";

    query.setQuery(sql);
    Instances data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    String[] options = Utils.splitOptions("-R -N 3 -Q 1 -M 30");
    YNC.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train
    // ---
    System.out.println("Building YC...");
    YNC.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Evaluation
    // ---
    System.out.println("Cross-validation for YNC...");
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(YNC, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for YNC: \n\n", false));

}

From source file:cs.man.ac.uk.classifiers.GetAUC.java

License:Open Source License

/**
 * Computes the AUC for the supplied learner.
 * @param learner the learning algorithm to use.
 * @return the AUC as a double value./*from   w w  w  . j  a v  a2s.  c o m*/
 */
@SuppressWarnings("unused")
private static double validate(Classifier learner) {
    try {

        Evaluation eval = new Evaluation(data);
        eval.crossValidateModel(learner, data, 2, new Random(1));

        // generate curve
        ThresholdCurve tc = new ThresholdCurve();
        int classIndex = 0;
        Instances result = tc.getCurve(eval.predictions(), classIndex);

        // plot curve
        vmc = new ThresholdVisualizePanel();
        double AUC = ThresholdCurve.getROCArea(result);
        vmc.setROCString(
                "(Area under ROC = " + Utils.doubleToString(ThresholdCurve.getROCArea(result), 9) + ")");
        vmc.setName(result.relationName());

        PlotData2D tempd = new PlotData2D(result);
        tempd.setPlotName(result.relationName());
        tempd.addInstanceNumberAttribute();

        // specify which points are connected
        boolean[] cp = new boolean[result.numInstances()];
        for (int n = 1; n < cp.length; n++)
            cp[n] = true;

        tempd.setConnectPoints(cp);
        // add plot
        vmc.addPlot(tempd);

        return AUC;
    } catch (Exception e) {
        System.out.println("Exception validating data!");
        return 0;
    }
}

From source file:csav2.Weka_additive.java

public void createTrainingFeatureFile1(String input) throws Exception {
    String file = "Classifier\\featurefile_additive_trial1.arff";
    ArffLoader loader = new ArffLoader();

    //ATTRIBUTES/* w ww  . ja v a 2 s.co  m*/
    Attribute attr[] = new Attribute[50];

    //numeric
    attr[0] = new Attribute("Autosentiment");

    //class
    FastVector classValue = new FastVector(3);
    classValue.addElement("p");
    classValue.addElement("n");
    classValue.addElement("o");
    attr[1] = new Attribute("answer", classValue);

    FastVector attrs = new FastVector();
    attrs.addElement(attr[0]);
    attrs.addElement(attr[1]);

    // Add Instances
    Instances dataset = new Instances("my_dataset", attrs, 0);

    if (new File(file).isFile()) {
        loader.setFile(new File(file));
        dataset = loader.getDataSet();
    }

    System.out.println("-----------------------------------------");
    System.out.println(input);
    System.out.println("-----------------------------------------");

    StringTokenizer tokenizer = new StringTokenizer(input);

    while (tokenizer.hasMoreTokens()) {
        Instance example = new Instance(2);
        for (int j = 0; j < 2; j++) {
            String st = tokenizer.nextToken();
            System.out.println(j + " " + st);
            if (j == 0)
                example.setValue(attr[j], Float.parseFloat(st));
            else if (j == 1)
                example.setValue(attr[j], st);
            else
                example.setValue(attr[j], Integer.parseInt(st));
        }
        dataset.add(example);
    }

    //Save dataset
    ArffSaver saver = new ArffSaver();
    saver.setInstances(dataset);
    saver.setFile(new File(file));
    saver.writeBatch();

    //Read dataset
    loader.setFile(new File(file));
    dataset = loader.getDataSet();

    //Build classifier
    dataset.setClassIndex(1);
    Classifier classifier = new J48();
    classifier.buildClassifier(dataset);

    //Save classifier
    String file1 = "Classifier\\classifier_add_autosentiment.model";
    OutputStream os = new FileOutputStream(file1);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(os);
    objectOutputStream.writeObject(classifier);

    // Comment out if not needed
    //Read classifier back
    InputStream is = new FileInputStream(file1);
    ObjectInputStream objectInputStream = new ObjectInputStream(is);
    classifier = (Classifier) objectInputStream.readObject();
    objectInputStream.close();

    //Evaluate resample if needed
    //dataset = dataset.resample(new Random(42));
    //split to 70:30 learn and test set
    double percent = 70.0;
    int trainSize = (int) Math.round(dataset.numInstances() * percent / 100);
    int testSize = dataset.numInstances() - trainSize;
    Instances train = new Instances(dataset, 0, trainSize);
    Instances test = new Instances(dataset, trainSize, testSize);
    train.setClassIndex(1);
    test.setClassIndex(1);

    //Evaluate
    Evaluation eval = new Evaluation(dataset); //trainset
    eval.crossValidateModel(classifier, dataset, 10, new Random(1));
    System.out.println("EVALUATION:\n" + eval.toSummaryString());
    System.out.println("WEIGHTED MEASURE:\n" + eval.weightedFMeasure());
    System.out.println("WEIGHTED PRECISION:\n" + eval.weightedPrecision());
    System.out.println("WEIGHTED RECALL:\n" + eval.weightedRecall());
}