Example usage for weka.classifiers.functions MultilayerPerceptron classifyInstance

List of usage examples for weka.classifiers.functions MultilayerPerceptron classifyInstance

Introduction

In this page you can find the example usage for weka.classifiers.functions MultilayerPerceptron classifyInstance.

Prototype

@Override
public double classifyInstance(Instance instance) throws Exception 

Source Link

Document

Classifies the given test instance.

Usage

From source file:Clases.RedNeuronal.RedNeuronal.java

public void redNeuronal(int puntaje, int tiempo, int error) throws Exception {
    //si puntaje >= 200 entonces aprendido
    //si tiempo <= 240 (4 minutos) entonces aprendido
    //si errores <= 3 entonces aprendido
    String[] dato = { obtnerPuntaje(puntaje), obtenerTiempo(tiempo), obtenerErrores(error) };

    ConverterUtils.DataSource con = new ConverterUtils.DataSource(
            "C:\\Users\\USUARIO\\Documents\\SILVIIS\\10 Modulo\\2.ANTEPROYECTOS DE TESIS\\Proyecto\\Aplicacion\\redeAprendizaje.arff");
    //        ConverterUtils.DataSource con = new ConverterUtils.DataSource("E:\\Unl\\10 Modulo\\2.ANTEPROYECTOS DE TESIS\\Proyecto\\Aplicacion\\redeAprendizaje.arff");

    Instances instances = con.getDataSet();
    System.out.println(instances);
    instances.setClassIndex(instances.numAttributes() - 1);

    MultilayerPerceptron mp = new MultilayerPerceptron();
    mp.buildClassifier(instances);//  w w  w. j  a va 2  s  .c  o m

    Evaluation evalucion = new Evaluation(instances);
    evalucion.evaluateModel(mp, instances);
    System.out.println(evalucion.toSummaryString());
    System.out.println(evalucion.toMatrixString());

    String datosEntrada = null;
    String datosSalida = "no se puede predecir";
    for (int i = 0; i < instances.numInstances(); i++) {
        double predecido = mp.classifyInstance(instances.instance(i));
        datosEntrada = dato[0] + " " + dato[1] + " " + dato[2];
        if ((int) instances.instance(i).value(0) == Integer.parseInt(dato[0])
                && (int) instances.instance(i).value(1) == Integer.parseInt(dato[1])
                && (int) instances.instance(i).value(2) == Integer.parseInt(dato[2])) {
            datosSalida = instances.classAttribute().value((int) predecido);
        }
    }
    System.out.println("DATOS DE ENTRADA: " + datosEntrada);
    System.out.println("SALIDA PREDECIDA: " + datosSalida);

    switch (datosSalida) {
    case "0":
        resultado = "Excelente ha aprendido";
        imgResultado = "Excelente.jpg";
        imgREDneuronal = "0.png";
        System.out.println("Excelente ha aprendido");
        break;
    case "1":
        resultado = "Disminuir Errores";
        imgResultado = "Bueno.jpg";
        imgREDneuronal = "1.png";
        System.out.println("Disminuir Errores");
        break;
    case "2":
        resultado = "Disminuir Tiempo";
        imgResultado = "Bueno.jpg";
        imgREDneuronal = "2.png";
        System.out.println("Disminuir Tiempo");
        break;
    case "3":
        resultado = "Disminuir Errores y tiempo";
        imgResultado = "Bueno.jpg";
        imgREDneuronal = "3.png";
        System.out.println("Disminuir Errores y tiempo");
        break;
    case "4":
        resultado = "Subir Puntaje";
        imgResultado = "pensando.jpg";
        imgREDneuronal = "4.png";
        System.out.println("Subir Puntaje");
        break;
    case "5":
        resultado = "Subir Puntaje y disminuir Errores";
        imgResultado = "pensando.jpg";
        imgREDneuronal = "5.png";
        System.out.println("Subir Puntaje y disminuir Errores");
        break;
    case "6":
        resultado = "Subir Puntaje y disminuir Tiempo";
        imgResultado = "pensando.jpg";
        imgREDneuronal = "6.png";
        System.out.println("Subir Puntaje y disminuir Tiempo");
        break;
    case "7":
        resultado = "Ponle mas Empeo";
        imgResultado = "pensando.jpg";
        imgREDneuronal = "7.png";
        System.out.println("Ponle mas Empeo");
        break;
    default:
        resultado = "Verifique entradas, no se puede predecir";
        imgResultado = "Error.jpg";
        System.out.println("Verifique entradas, no se puede predecir");
        break;
    }
}

From source file:cs.man.ac.uk.predict.Predictor.java

License:Open Source License

public static void makePredictionsEnsembleNew(String trainPath, String testPath, String resultPath) {
    System.out.println("Training set: " + trainPath);
    System.out.println("Test set: " + testPath);

    /**/*ww  w  . ja  v  a2  s.co  m*/
     * The ensemble classifiers. This is a heterogeneous ensemble.
     */
    J48 learner1 = new J48();
    SMO learner2 = new SMO();
    NaiveBayes learner3 = new NaiveBayes();
    MultilayerPerceptron learner5 = new MultilayerPerceptron();

    System.out.println("Training Ensemble.");
    long startTime = System.nanoTime();
    try {
        BufferedReader reader = new BufferedReader(new FileReader(trainPath));
        Instances data = new Instances(reader);
        data.setClassIndex(data.numAttributes() - 1);
        System.out.println("Training data length: " + data.numInstances());

        learner1.buildClassifier(data);
        learner2.buildClassifier(data);
        learner3.buildClassifier(data);
        learner5.buildClassifier(data);

        long endTime = System.nanoTime();
        long nanoseconds = endTime - startTime;
        double seconds = (double) nanoseconds / 1000000000.0;
        System.out.println("Training Ensemble completed in " + nanoseconds + " (ns) or " + seconds + " (s).");
    } catch (IOException e) {
        System.out.println("Could not train Ensemble classifier IOException on training data file.");
    } catch (Exception e) {
        System.out.println("Could not train Ensemble classifier Exception building model.");
    }

    try {
        String line = "";

        // Read the file and display it line by line. 
        BufferedReader in = null;

        // Read in and store each positive prediction in the tree map.
        try {
            //open stream to file
            in = new BufferedReader(new FileReader(testPath));

            while ((line = in.readLine()) != null) {
                if (line.toLowerCase().contains("@data"))
                    break;
            }
        } catch (Exception e) {
        }

        // A different ARFF loader used here (compared to above) as
        // the ARFF file may be extremely large. In which case the whole
        // file cannot be read in. Instead it is read in incrementally.
        ArffLoader loader = new ArffLoader();
        loader.setFile(new File(testPath));

        Instances data = loader.getStructure();
        data.setClassIndex(data.numAttributes() - 1);

        System.out.println("Ensemble Classifier is ready.");
        System.out.println("Testing on all instances avaialable.");

        startTime = System.nanoTime();

        int instanceNumber = 0;

        // label instances
        Instance current;

        while ((current = loader.getNextInstance(data)) != null) {
            instanceNumber += 1;
            line = in.readLine();

            double classification1 = learner1.classifyInstance(current);
            double classification2 = learner2.classifyInstance(current);
            double classification3 = learner3.classifyInstance(current);
            double classification5 = learner5.classifyInstance(current);

            // All classifiers must agree. This is a very primitive ensemble strategy!
            if (classification1 == 1 && classification2 == 1 && classification3 == 1 && classification5 == 1) {
                if (line != null) {
                    //System.out.println("Instance: "+instanceNumber+"\t"+line);
                    //System.in.read();
                }
                Writer.append(resultPath, instanceNumber + "\n");
            }
        }

        in.close();

        System.out.println("Test set instances: " + instanceNumber);

        long endTime = System.nanoTime();
        long duration = endTime - startTime;
        double seconds = (double) duration / 1000000000.0;

        System.out.println("Testing Ensemble completed in " + duration + " (ns) or " + seconds + " (s).");
    } catch (Exception e) {
        System.out.println("Could not test Ensemble classifier due to an error.");
    }
}

From source file:cs.man.ac.uk.predict.Predictor.java

License:Open Source License

public static void makePredictionsEnsembleStream(String trainPath, String testPath, String resultPath) {
    System.out.println("Training set: " + trainPath);
    System.out.println("Test set: " + testPath);

    /**//from   w w w  . java  2s. c o m
     * The ensemble classifiers. This is a heterogeneous ensemble.
     */
    J48 learner1 = new J48();
    SMO learner2 = new SMO();
    NaiveBayes learner3 = new NaiveBayes();
    MultilayerPerceptron learner5 = new MultilayerPerceptron();

    System.out.println("Training Ensemble.");
    long startTime = System.nanoTime();
    try {
        BufferedReader reader = new BufferedReader(new FileReader(trainPath));
        Instances data = new Instances(reader);
        data.setClassIndex(data.numAttributes() - 1);
        System.out.println("Training data length: " + data.numInstances());

        learner1.buildClassifier(data);
        learner2.buildClassifier(data);
        learner3.buildClassifier(data);
        learner5.buildClassifier(data);

        long endTime = System.nanoTime();
        long nanoseconds = endTime - startTime;
        double seconds = (double) nanoseconds / 1000000000.0;
        System.out.println("Training Ensemble completed in " + nanoseconds + " (ns) or " + seconds + " (s).");
    } catch (IOException e) {
        System.out.println("Could not train Ensemble classifier IOException on training data file.");
    } catch (Exception e) {
        System.out.println("Could not train Ensemble classifier Exception building model.");
    }

    try {
        // A different ARFF loader used here (compared to above) as
        // the ARFF file may be extremely large. In which case the whole
        // file cannot be read in. Instead it is read in incrementally.
        ArffLoader loader = new ArffLoader();
        loader.setFile(new File(testPath));

        Instances data = loader.getStructure();
        data.setClassIndex(data.numAttributes() - 1);

        System.out.println("Ensemble Classifier is ready.");
        System.out.println("Testing on all instances avaialable.");

        startTime = System.nanoTime();

        int instanceNumber = 0;

        // label instances
        Instance current;

        while ((current = loader.getNextInstance(data)) != null) {
            instanceNumber += 1;

            double classification1 = learner1.classifyInstance(current);
            double classification2 = learner2.classifyInstance(current);
            double classification3 = learner3.classifyInstance(current);
            double classification5 = learner5.classifyInstance(current);

            // All classifiers must agree. This is a very primitive ensemble strategy!
            if (classification1 == 1 && classification2 == 1 && classification3 == 1 && classification5 == 1) {
                Writer.append(resultPath, instanceNumber + "\n");
            }
        }

        System.out.println("Test set instances: " + instanceNumber);

        long endTime = System.nanoTime();
        long duration = endTime - startTime;
        double seconds = (double) duration / 1000000000.0;

        System.out.println("Testing Ensemble completed in " + duration + " (ns) or " + seconds + " (s).");
    } catch (Exception e) {
        System.out.println("Could not test Ensemble classifier due to an error.");
    }
}

From source file:mlp.MLP.java

/**
 * the trained multilayer perceptron tries to classify the instances in the
 * test set//from w  ww.  j a v  a2 s .  c  o  m
 *
 * @param mlp a trained multilayer perceptron
 * @param testSet the test set
 * @throws Exception
 */
public static void testMLP(MultilayerPerceptron mlp, Instances testSet) throws Exception {
    for (int i = 0; i < testSet.numInstances(); i++) {
        double classifier = mlp.classifyInstance(testSet.instance(i));
        System.out.print("Number classified as: " + classifier);
        System.out.println(" / Actual number:" + testSet.instance(i).classValue());
    }

}