Example usage for weka.classifiers.trees J48 J48

List of usage examples for weka.classifiers.trees J48 J48

Introduction

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

Prototype

J48

Source Link

Usage

From source file:com.relationalcloud.misc.JustifyAgnosticPartitioning.java

License:Open Source License

/**
 * @param args//from   w  w  w .j a  v  a  2 s. c  om
 */
public static void main(String[] args) {

    Properties ini = new Properties();
    try {
        ini.load(new FileInputStream(System.getProperty("prop")));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Register jdbcDriver
    try {
        Class.forName(ini.getProperty("driver"));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    // READ FROM MYSQL THE TPCC TRANSACTION LOG, PARSE EACH STATEMENT AND TEST
    // VARIOUS PARSER FUNCTIONALITIES
    System.out.println("Loading and processing TPCC traces...");

    Connection conn;
    try {

        String schemaname = ini.getProperty("schema");
        String connection = ini.getProperty("conn");
        String user = ini.getProperty("user");
        String password = ini.getProperty("password");
        conn = DriverManager.getConnection(connection + schemaname, user, password);

        Connection infschema_conn = DriverManager.getConnection(connection + "information_schema", user,
                password);

        Schema schema = SchemaLoader.loadSchemaFromDB(infschema_conn, schemaname);

        ExplanationWorkloadPrepocessor wa = new ExplanationWorkloadPrepocessor(schemaname, schema);

        conn.setAutoCommit(true);

        Statement stmt = conn.createStatement();

        String txnLogTable = ini.getProperty("txnLogTable");
        String sqlstring = "SELECT sqlstring FROM `" + txnLogTable + "`";
        ResultSet res = stmt.executeQuery(sqlstring);

        double tstart = System.currentTimeMillis();
        double i = 0;
        while (res.next()) {
            String sql = res.getString(1);
            // PARSE THE STATEMENT
            wa.processSql(sql);
            // System.out.println("SQL: " +sql);
            i++;
        }

        double tend = System.currentTimeMillis();

        String accessLogTable = ini.getProperty("accessLogTable");

        System.out.println("Processed " + i + " statements in " + (tend - tstart) + "ms average:"
                + (tend - tstart) / i + "ms per statement");
        for (String str : wa.getAllTableNames()) {

            System.out.println("-------------------------------------------");
            System.out.println("ANALYZING TABLE " + str);
            for (SimpleCount sc : wa.getFeatures(str)) {

                ArrayList<Double> a0 = new ArrayList<Double>();
                ArrayList<Double> a1 = new ArrayList<Double>();

                sqlstring = "SELECT s." + sc.colname + ", g.partition FROM `" + accessLogTable + "` g, " + str
                        + " s WHERE tableid = \"" + str + "\" AND s.id = g.id";
                System.out.println(sqlstring);
                res = stmt.executeQuery(sqlstring);

                while (res.next()) {
                    a0.add(new Double(res.getObject(1).hashCode()));
                    a1.add(new Double(res.getObject(2).hashCode()));
                }

                if (a0.size() >= 1) {
                    double[] d0 = new double[a0.size()];
                    double[] d1 = new double[a1.size()];

                    boolean unary = true;

                    for (int j = 0; j < a0.size(); j++) {
                        d0[j] = a0.get(j).doubleValue();
                        d1[j] = a1.get(j).doubleValue();
                        if (j > 0 && d1[j - 1] != d1[j])
                            unary = false;
                    }

                    if (unary) {
                        System.out.println("EASY CASE: " + str
                                + " is not partitioned and is stored in partition: " + d1[0]);
                    } else {

                        double correlation = PearsonCorrelation.getPearsonCorrelation(d0, d1);

                        correlationThreshold = Double.parseDouble(ini.getProperty("correlationThreshold"));

                        // if the correlation is high enough proceed to use decision
                        // trees.
                        if (Math.abs(correlation) > correlationThreshold) {
                            System.out.println("Testing " + str + "." + sc.colname
                                    + ", g.partition correlation: " + correlation + " (HIGH)");

                            try {
                                // InstanceQuery query;
                                // query = new InstanceQuery();
                                // query.setUsername("bbb");
                                // query.setPassword("qwer");
                                // query.connectToDatabase();
                                // Instances data = query.retrieveInstances(sqlstring);
                                res.beforeFirst();
                                Instances data = retrieveInstanceFromResultSet(res);
                                // set the last column to be the classIndex... is this
                                // correct?
                                data.setClassIndex(data.numAttributes() - 1);

                                Instances newData;

                                if (data.attribute(data.numAttributes() - 1).type() == Attribute.NUMERIC) {
                                    NumericToNominal ntn = new NumericToNominal();
                                    String[] options = new String[2];
                                    options[0] = "-R"; // "range"
                                    options[1] = "2"; // first attribute
                                    ntn.setOptions(options); // set options
                                    ntn.setInputFormat(data); // inform filter about dataset
                                    // **AFTER** setting options
                                    newData = Filter.useFilter(data, ntn); // apply fil

                                } else {
                                    StringToNominal ntn = new StringToNominal();
                                    String[] options = new String[2];
                                    options[0] = "-R"; // "range"
                                    options[1] = "2"; // first attribute
                                    ntn.setOptions(options); // set options
                                    ntn.setInputFormat(data); // inform filter about dataset
                                    // **AFTER** setting options
                                    newData = Filter.useFilter(data, ntn); // apply fil

                                }

                                String[] options = new String[1];
                                options[0] = "-P";
                                J48 tree = new J48(); // new instance of tree
                                tree.setOptions(options); // set the options

                                if (!tree.getCapabilities().test(newData)) {
                                    System.err.println("ERROR the FOLLOWING DATA CANNOT BE PROCESED:"
                                            + newData.toSummaryString());
                                    System.err.println("QUERY WAS:" + sqlstring);
                                } else {
                                    tree.buildClassifier(newData); // build classifier

                                }
                                System.out.println("CLASSIFICATION CONFIDENCE:  " + tree.getConfidenceFactor()
                                        + "\n " + tree.toString());

                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        } else {
                            System.out.println("Testing " + str + "." + sc.colname
                                    + ", g.partition correlation: " + correlation + " (LOW)");
                        }
                    }
                }
            }
        }

    } catch (SQLException e) {
        e.printStackTrace();
    }

}

From source file:com.rokittech.ml.server.utils.MLUtils.java

License:Open Source License

public static Classifier getClassifier(String mlAlgorithm) {
    notEmpty(mlAlgorithm);/*from   w w w .  j  av a  2  s.  c o m*/
    Classifier classifier;
    switch (mlAlgorithm.toUpperCase()) {
    case "J48": {
        classifier = new J48();
        break;
    }
    case "IBK": {
        classifier = new IBk();
        break;
    }
    case "NAIVE_BAYES": {
        classifier = new NaiveBayes();
        break;
    }
    case "RANDOM_TREE": {
        classifier = new RandomTree();
        break;
    }
    case "RANDOM_FOREST": {
        classifier = new RandomForest();
        break;
    }
    case "BOOSTING": {
        classifier = new DecisionStump();
        break;
    }
    case "BAGGING": {
        classifier = new Bagging();
        break;
    }
    default:
        throw new UnsupportedOperationException("Classifier " + mlAlgorithm + " is not supported.");
    }
    return classifier;
}

From source file:Control.Classificador.java

public ArrayList<Resultado> classificar(Plano plano, Arquivo arq) {
    try {/*from   www.j av a2 s .  c  o  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.BothClassificationsServlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    String dir = "/data/";
    String path = getServletContext().getRealPath(dir);

    String action = request.getParameter("action");

    switch (action) {
    case "create": {
        String fileName = request.getParameter("file");

        String aux = fileName.substring(0, fileName.indexOf("."));
        String pathInput = path + "/" + request.getParameter("file");
        String pathTrainingOutput = path + "/" + aux + "-training-arff.txt";
        String pathTestOutput = path + "/" + aux + "-test-arff.txt";
        String pathBothClassifications = path + "/" + aux + "-bothClassifications.txt";

        String name = request.getParameter("name");
        int range = Integer.parseInt(request.getParameter("range"));

        int size = Integer.parseInt(request.getParameter("counter"));
        String[] columns = new String[size];
        String[] types = new String[size];
        int[] positions = new int[size];
        int counter = 0;
        for (int i = 0; i < size; i++) {
            if (request.getParameter("column-" + (i + 1)) != null) {
                columns[counter] = request.getParameter("column-" + (i + 1));
                types[counter] = request.getParameter("type-" + (i + 1));
                positions[counter] = Integer.parseInt(request.getParameter("position-" + (i + 1)));
                counter++;/*from w  ww .  java 2 s.  c o m*/
            }
        }

        FormatFiles.convertTxtToArff(pathInput, pathTrainingOutput, pathTestOutput, name, columns, types,
                positions, counter, range);
        try {
            J48 j48 = new J48();

            BufferedReader readerTraining = new BufferedReader(new FileReader(pathTrainingOutput));
            Instances instancesTraining = new Instances(readerTraining);
            instancesTraining.setClassIndex(instancesTraining.numAttributes() - 1);

            j48.buildClassifier(instancesTraining);

            BufferedReader readerTest = new BufferedReader(new FileReader(pathTestOutput));
            //BufferedReader readerTest = new BufferedReader(new FileReader(pathTrainingOutput));
            Instances instancesTest = new Instances(readerTest);
            instancesTest.setClassIndex(instancesTest.numAttributes() - 1);

            int correctsDecisionTree = 0;

            for (int i = 0; i < instancesTest.size(); i++) {
                Instance instance = instancesTest.get(i);
                double correctValue = instance.value(instance.attribute(instancesTest.numAttributes() - 1));
                double classification = j48.classifyInstance(instance);

                if (correctValue == classification) {
                    correctsDecisionTree++;
                }
            }

            Evaluation eval = new Evaluation(instancesTraining);
            eval.evaluateModel(j48, instancesTest);

            PrintWriter writer = new PrintWriter(
                    new BufferedWriter(new FileWriter(pathBothClassifications, false)));

            writer.println("?rvore de Deciso\n\n");

            writer.println(j48.toString());

            writer.println("");
            writer.println("");
            writer.println("Results");
            writer.println(eval.toSummaryString());

            NaiveBayes naiveBayes = new NaiveBayes();

            naiveBayes.buildClassifier(instancesTraining);

            eval = new Evaluation(instancesTraining);
            eval.evaluateModel(naiveBayes, instancesTest);

            int correctsNaiveBayes = 0;

            for (int i = 0; i < instancesTest.size(); i++) {
                Instance instance = instancesTest.get(i);
                double correctValue = instance.value(instance.attribute(instancesTest.numAttributes() - 1));
                double classification = naiveBayes.classifyInstance(instance);

                if (correctValue == classification) {
                    correctsNaiveBayes++;
                }
            }

            writer.println("Naive Bayes\n\n");

            writer.println(naiveBayes.toString());

            writer.println("");
            writer.println("");
            writer.println("Results");
            writer.println(eval.toSummaryString());

            writer.close();

            response.sendRedirect("BothClassifications?action=view&correctsDecisionTree=" + correctsDecisionTree
                    + "&correctsNaiveBayes=" + correctsNaiveBayes + "&totalTest=" + instancesTest.size()
                    + "&totalTrainig=" + instancesTraining.size() + "&range=" + range + "&fileName=" + aux
                    + "-bothClassifications.txt");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            response.sendRedirect("Navigation?action=decisionTree");
        }

        break;
    }
    default:
        response.sendError(404);
    }
}

From source file:Controller.CtlDataMining.java

public String arbolJ48(Instances data) {
    try {//  ww  w.  java 2s .  c  o 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:controller.DecisionTreeBean.java

public void generateTree(int positiontomine) {
    System.out.println("Start Generating Tree");
    model = new DefaultDiagramModel();
    model.setMaxConnections(-1);/*from  w w  w  .j av a  2  s .  c  o  m*/
    try {
        inst.setClassIndex(positiontomine);
        J48 tree;
        String[] options = new String[1];
        options[0] = "-U";
        tree = new J48();
        tree.setOptions(options); // set the options
        tree.buildClassifier(inst); // build classifier
        dis = inst.toSummaryString();
        System.out.println(tree.graph());
        System.out.println(tree.toString());
        List<String> liNodeStr = new LinkedList<>();
        List<String> liConnStr = new LinkedList<>();

        liNodeElement = new LinkedList<>();
        liConnElement = new LinkedList<>();

        BufferedReader br = new BufferedReader(new StringReader(tree.graph()));
        br.readLine();
        String tmp;
        while ((tmp = br.readLine()) != null) {
            if (tmp.contains("}")) {
                break;
            } else if (tmp.contains("->")) {
                liConnStr.add(tmp);
            } else {
                liNodeStr.add(tmp);
            }
        }

        System.out.println(liConnStr);
        System.out.println(liNodeStr);

        for (String s : liNodeStr) {
            String[] arr = s.split(" ");
            String entitie1 = arr[0];
            arr = s.split("\"");
            String entitie2 = arr[1];

            System.out.println("ID:" + entitie1 + " Name:" + entitie2);
            TreeElement te = new TreeElement(entitie1, entitie2);
            liNodeElement.add(te);
        }

        for (String s : liConnStr) {
            String[] arr = s.split(" ");
            arr = arr[0].split("->");
            String from = arr[0];
            String to = arr[1];

            arr = s.split("\"");
            String label = arr[1];

            System.out.println("From:" + from + " To:" + to + "Label:" + label);
            TreeConector ce = new TreeConector(from, to, label);
            liConnElement.add(ce);
        }

        //-----------------------------------------------------------------------------------------------
        for (TreeElement te : liNodeElement) {
            if (te.getID().equals("N0")) {
                System.out.println("inside");
                genlevel(te, 0);
                te.setPosition(25);
                genposition(te, 50);
            }
        }

        for (TreeElement te : liNodeElement) {
            Element el = new Element(te, te.getPosition() + "em", te.getLevel() * 15 + "em");
            el.addEndPoint(new BlankEndPoint(EndPointAnchor.TOP));
            el.addEndPoint(new BlankEndPoint(EndPointAnchor.BOTTOM));
            model.addElement(el);
        }
        List<Element> ellist = model.getElements();
        for (TreeConector tc : liConnElement) {
            Element beginn = null;
            for (Element e : ellist) {
                TreeElement t;
                t = (TreeElement) e.getData();
                if (t.getID().equals(tc.getFrom())) {
                    beginn = e;
                    break;
                }
            }
            Element endeee = null;
            for (Element e : ellist) {
                TreeElement t;
                t = (TreeElement) e.getData();
                if (t.getID().equals(tc.getTo())) {
                    endeee = e;
                    break;
                }
            }

            StraightConnector connector = new StraightConnector();

            connector.setPaintStyle("{strokeStyle:'#F28D2A', lineWidth:3}");
            connector.setHoverPaintStyle("{strokeStyle:'#F28D2A'}");
            Connection con = new Connection(beginn.getEndPoints().get(1), endeee.getEndPoints().get(0),
                    connector);

            con.getOverlays().add(new LabelOverlay(tc.getLabel(), "flow-label", 0.5));
            model.connect(con);
        }

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

From source file:controller.DecisionTreeServlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    String dir = "/data/";
    String path = getServletContext().getRealPath(dir);

    String action = request.getParameter("action");

    switch (action) {
    case "create": {
        String fileName = request.getParameter("file");

        String aux = fileName.substring(0, fileName.indexOf("."));
        String pathInput = path + "/" + request.getParameter("file");
        String pathTrainingOutput = path + "/" + aux + "-training-arff.txt";
        String pathTestOutput = path + "/" + aux + "-test-arff.txt";
        String pathDecisionTree = path + "/" + aux + "-decisionTree.txt";

        String name = request.getParameter("name");
        int range = Integer.parseInt(request.getParameter("range"));

        int size = Integer.parseInt(request.getParameter("counter"));
        String[] columns = new String[size];
        String[] types = new String[size];
        int[] positions = new int[size];
        int counter = 0;
        for (int i = 0; i < size; i++) {
            if (request.getParameter("column-" + (i + 1)) != null) {
                columns[counter] = request.getParameter("column-" + (i + 1));
                types[counter] = request.getParameter("type-" + (i + 1));
                positions[counter] = Integer.parseInt(request.getParameter("position-" + (i + 1)));
                counter++;/*w  w  w  .j  a v a2  s  .com*/
            }
        }

        FormatFiles.convertTxtToArff(pathInput, pathTrainingOutput, pathTestOutput, name, columns, types,
                positions, counter, range);
        try {
            J48 j48 = new J48();

            BufferedReader readerTraining = new BufferedReader(new FileReader(pathTrainingOutput));
            Instances instancesTraining = new Instances(readerTraining);
            instancesTraining.setClassIndex(instancesTraining.numAttributes() - 1);

            j48.buildClassifier(instancesTraining);

            BufferedReader readerTest = new BufferedReader(new FileReader(pathTestOutput));
            //BufferedReader readerTest = new BufferedReader(new FileReader(pathTrainingOutput));
            Instances instancesTest = new Instances(readerTest);
            instancesTest.setClassIndex(instancesTest.numAttributes() - 1);

            int corrects = 0;
            int truePositive = 0;
            int trueNegative = 0;
            int falsePositive = 0;
            int falseNegative = 0;

            for (int i = 0; i < instancesTest.size(); i++) {
                Instance instance = instancesTest.get(i);
                double correctValue = instance.value(instance.attribute(instancesTest.numAttributes() - 1));
                double classification = j48.classifyInstance(instance);

                if (correctValue == classification) {
                    corrects++;
                }
                if (correctValue == 1 && classification == 1) {
                    truePositive++;
                }
                if (correctValue == 1 && classification == 0) {
                    falseNegative++;
                }
                if (correctValue == 0 && classification == 1) {
                    falsePositive++;
                }
                if (correctValue == 0 && classification == 0) {
                    trueNegative++;
                }
            }

            Evaluation eval = new Evaluation(instancesTraining);
            eval.evaluateModel(j48, instancesTest);

            PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(pathDecisionTree, false)));

            writer.println(j48.toString());

            writer.println("");
            writer.println("");
            writer.println("Results");
            writer.println(eval.toSummaryString());

            writer.close();

            response.sendRedirect("DecisionTree?action=view&corrects=" + corrects + "&totalTest="
                    + instancesTest.size() + "&totalTrainig=" + instancesTraining.size() + "&truePositive="
                    + truePositive + "&trueNegative=" + trueNegative + "&falsePositive=" + falsePositive
                    + "&falseNegative=" + falseNegative + "&fileName=" + aux + "-decisionTree.txt");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            response.sendRedirect("Navigation?action=decisionTree");
        }

        break;
    }
    default:
        response.sendError(404);
    }
}

From source file:core.classification.Classifiers.java

License:Open Source License

/**
 * Private constructor for the <code>Classifiers</code> object
 * @param train/*from w w w.j ava 2s .  co m*/
 */
private Classifiers(boolean train) {
    SCA = new BayesNet();
    SCB = new MultilayerPerceptron();
    SCC1 = new MultilayerPerceptron();
    SCC2 = new MultilayerPerceptron();
    SCC3 = new MultilayerPerceptron();

    RC = new CostSensitiveClassifier();
    YNC = new J48();

    if (train) {
        try {
            this.trainSC();
        } catch (Exception e) {
            System.out.println("The system encountered the following error while training SC:");
            e.printStackTrace();
        }
        try {
            this.trainRC();
        } catch (Exception e) {
            System.out.println("The system encountered the following error while training RC:");
            e.printStackTrace();
        }
        try {
            this.trainYNC();
        } catch (Exception e) {
            System.out.println("The system encountered the following error while training YNC:");
            e.printStackTrace();
        }
    } else {
        try {
            readSC("SCA.model", "SCB.model", "SCC1.model", "SCC2.model", "SCC3.model");
            readRC("RC.model");
            readYNC("YNC.model");
        } catch (Exception e) {
            System.out.println("Error while reading the classifiers: ");
            e.printStackTrace();
        }
    }

    // Strutures Creations
    FastVector labels = new FastVector();
    labels.addElement("0");
    labels.addElement("1");
    labels.addElement("2");
    labels.addElement("3");
    labels.addElement("4");

    FastVector clabels = new FastVector();
    clabels.addElement("1");
    clabels.addElement("2");
    clabels.addElement("3");
    clabels.addElement("4");

    FastVector clabels2 = new FastVector();
    clabels2.addElement("0");
    clabels2.addElement("1");
    clabels2.addElement("2");
    clabels2.addElement("3");
    clabels2.addElement("4");

    FastVector clabels3 = new FastVector();
    clabels3.addElement("Y");
    clabels3.addElement("N");

    // Creating the structure for SC
    FastVector attrs = new FastVector();
    attrs.addElement(new Attribute("RATIO"));
    attrs.addElement(new Attribute("CLASS", clabels));
    dataStructSCA = new Instances("SCA-STRUCT", attrs, 0);
    dataStructSCA.setClassIndex(1);

    FastVector attrsB = new FastVector();
    attrsB.addElement(new Attribute("H2"));
    attrsB.addElement(new Attribute("D2"));
    attrsB.addElement(new Attribute("DX"));
    attrsB.addElement(new Attribute("PCLASS", clabels));
    attrsB.addElement(new Attribute("CLASS", clabels));
    dataStructSCB = new Instances("SCB-STRUCT", attrsB, 0);
    dataStructSCB.setClassIndex(4);

    FastVector attrsC1 = new FastVector();
    FastVector attrsC2 = new FastVector();
    FastVector attrsC3 = new FastVector();
    attrsC1.addElement(new Attribute("LH"));
    attrsC1.addElement(new Attribute("LD"));
    attrsC1.addElement(new Attribute("LDX"));
    attrsC1.addElement(new Attribute("LCLASS", clabels));
    attrsC1.addElement(new Attribute("CLASS", clabels));

    attrsC2.addElement(new Attribute("EH"));
    attrsC2.addElement(new Attribute("ED"));
    attrsC2.addElement(new Attribute("EDX"));
    attrsC2.addElement(new Attribute("ECLASS", clabels));
    attrsC2.addElement(new Attribute("CLASS", clabels));

    attrsC3.addElement(new Attribute("SH"));
    attrsC3.addElement(new Attribute("SD"));
    attrsC3.addElement(new Attribute("SDX"));
    attrsC3.addElement(new Attribute("SCLASS", clabels));
    attrsC3.addElement(new Attribute("CLASS", clabels));

    dataStructSCC1 = new Instances("SCC1-STRUCT", attrsC1, 0);
    dataStructSCC1.setClassIndex(4);

    dataStructSCC2 = new Instances("SCC2-STRUCT", attrsC2, 0);
    dataStructSCC2.setClassIndex(4);

    dataStructSCC3 = new Instances("SCC3-STRUCT", attrsC3, 0);
    dataStructSCC3.setClassIndex(4);

    FastVector attrs2 = new FastVector();
    attrs2.addElement(new Attribute("H2"));
    attrs2.addElement(new Attribute("D2"));
    attrs2.addElement(new Attribute("DX"));
    attrs2.addElement(new Attribute("CLASS", clabels));
    attrs2.addElement(new Attribute("PCLASS", clabels));
    attrs2.addElement(new Attribute("RELID", clabels2));
    dataStructRC = new Instances("RC-STRUCT", attrs2, 0);
    dataStructRC.setClassIndex(5);

    FastVector attrs3 = new FastVector();
    attrs3.addElement(new Attribute("PCLASS", clabels));
    attrs3.addElement(new Attribute("CCLASS", clabels));
    attrs3.addElement(new Attribute("RAREA"));
    attrs3.addElement(new Attribute("H"));
    attrs3.addElement(new Attribute("D"));
    attrs3.addElement(new Attribute("V"));
    attrs3.addElement(new Attribute("YN", clabels3));
    dataStructYC = new Instances("YC-STRUCT", attrs3, 0);
    dataStructYC.setClassIndex(6);
}

From source file:core.Core.java

public String run() throws Exception {
    ConverterUtils.DataSource source = new ConverterUtils.DataSource("src/files/powerpuffgirls.arff");

    HashMap<String, Classifier> hash = new HashMap<>();

    hash.put("J48", new J48());
    hash.put("NaiveBayes", new NaiveBayes());
    hash.put("IBk=1", new IBk(1));
    hash.put("IBk=3", new IBk(3));
    hash.put("MultilayerPerceptron", new MultilayerPerceptron());

    LibSVM svm = new LibSVM();
    hash.put("LibSVM", svm);
    Instances ins = source.getDataSet();

    ins.setClassIndex(4);/*from  w  ww. ja  va 2  s . c  o m*/

    StringBuilder sb = new StringBuilder();

    int blossom = 0;
    int bubbles = 0;

    Instance test = null;

    for (Map.Entry<String, Classifier> entry : hash.entrySet()) {
        Classifier c = entry.getValue();
        c.buildClassifier(ins);

        test = new Instance(5);

        float[] array = classifyImage();

        test.setDataset(ins);
        test.setValue(0, array[0]);
        test.setValue(1, array[1]);
        test.setValue(2, array[2]);
        test.setValue(3, array[3]);

        double prob[] = c.distributionForInstance(test);

        sb.append("<em>");
        sb.append(entry.getKey());
        sb.append(":</em>");
        sb.append("<br/>");

        for (int i = 0; i < prob.length; i++) {
            String value = test.classAttribute().value(i);

            if (getRoundedValue(prob[i]) >= CUT_NOTE) {
                if (getClassValue(value))
                    blossom++;
                else
                    bubbles++;
            }

            sb.append(getClassName(value));
            sb.append(": ");
            sb.append("<strong>");
            sb.append(getRoundedValue(prob[i]) < CUT_NOTE ? "Rejeitado!" : getValueFormatted(prob[i]));
            sb.append("</strong>");
            sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        }

        sb.append("<br/>");

        System.out.println("blossom: " + blossom);
        System.out.println("bubbles: " + bubbles);
        System.out.println("=================\n");
    }

    sb.append(blossom > bubbles ? "<h3> a Florzinha!</h3>" : "<h3> a Lindinha!</h3>");

    blossom = 0;
    bubbles = 0;

    return sb.toString();
}

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

License:Open Source License

/**
 * Main method that executes the calls that calculate the AUC for the supplied data
 * set and learning algorithm.//from   w ww  . j a va 2s .  c  o m
 * @param args unused arguments.
 */
public static void main(String[] args) {
    // Declare classifier to test.
    @SuppressWarnings("unused")
    Classifier learner = new J48();

    if (getData()) {
        //double AUC = validate(learner);
        double AUC = validate5x2CVStream();

        /**
         * C4.5 Bounds
         */

        // Letter dataset
        //double plus_or_minus = 0.004; // Error margin reported in paper.
        //double reportedAUC = 0.990; // AUC reported in paper.

        // Pen digits
        //double plus_or_minus = 0.005; // Error margin reported in paper.
        //double reportedAUC = 0.985; // AUC reported in paper.

        // Satellite image
        //double plus_or_minus = 0.009; // Error margin reported in paper.
        //double reportedAUC = 0.906; // AUC reported in paper.

        // Image segmentation
        //double plus_or_minus = 0.006; // Error margin reported in paper.
        //double reportedAUC = 0.982; // AUC reported in paper.

        /**
         * HDDT Bounds
         */

        // Letter dataset
        double plus_or_minus = 0.004; // Error margin reported in paper.
        double reportedAUC = 0.990; // AUC reported in paper.

        // Pen digits
        //double plus_or_minus = 0.002; // Error margin reported in paper.
        //double reportedAUC = 0.992; // AUC reported in paper.

        // Satellite image
        //double plus_or_minus = 0.007; // Error margin reported in paper.
        //double reportedAUC = 0.911; // AUC reported in paper.

        // Image segmentation
        //double plus_or_minus = 0.007; // Error margin reported in paper.
        //double reportedAUC = 0.984; // AUC reported in paper.

        double lowerBound = reportedAUC - plus_or_minus;
        double upperBound = reportedAUC + plus_or_minus;

        boolean inInterval = inInterval(lowerBound, upperBound, AUC);

        System.out.println("Area under curve: " + AUC + " Interval [" + lowerBound + "," + upperBound
                + "] in interval: " + inInterval);

        if (showGUI)
            displayGUI();
    }
}