ffnn.FFNN.java Source code

Java tutorial

Introduction

Here is the source code for ffnn.FFNN.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ffnn;

import java.util.Random;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.NominalToBinary;
import weka.filters.unsupervised.attribute.Normalize;
import weka.filters.unsupervised.attribute.Reorder;
import weka.filters.unsupervised.attribute.StringToNominal;

/**
 *
 * @author doubleH
 */
public class FFNN {
    public static Instances preprocess(Instances i) {
        try {
            Reorder rfilter = new Reorder();
            int classIdx = i.classIndex() + 1;
            String order;
            if (classIdx != 1) {
                order = "1";
                for (int j = 2; j <= i.numAttributes(); j++) {
                    if (j != classIdx) {
                        order = order + "," + j;
                    }
                }
            } else {
                order = "2";
                for (int j = 3; j <= i.numAttributes(); j++) {
                    order = order + "," + j;
                }
            }
            order = order + "," + classIdx;
            rfilter.setAttributeIndices(order);
            rfilter.setInputFormat(i);
            i = Filter.useFilter(i, rfilter);

            StringToNominal stnfilter = new StringToNominal();
            stnfilter.setAttributeRange("first-last");
            stnfilter.setInputFormat(i);
            i = Filter.useFilter(i, stnfilter);

            NominalToBinary ntbfilter = new NominalToBinary();
            ntbfilter.setInputFormat(i);
            i = Filter.useFilter(i, ntbfilter);

            Normalize nfilter = new Normalize();
            nfilter.setInputFormat(i);
            i = Filter.useFilter(i, nfilter);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return i;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        FFNNTubesAI cls;
        Scanner scan = new Scanner(System.in);
        System.out.print("new / read? (n/r)");
        if (scan.next().equals("n")) {
            cls = new FFNNTubesAI();
        } else {
            cls = (FFNNTubesAI) TucilWeka.readModel();
        }
        int temp;
        Instances data = TucilWeka.readDataSet("C:\\Program Files\\Weka-3-8\\data\\Team.arff");
        //Tampilkan opsi
        for (int i = 0; i < data.numAttributes(); i++) {
            System.out.println(i + ". " + data.attribute(i));
        }
        System.out.print("Class Index : ");
        temp = scan.nextInt();
        data.setClassIndex(temp);
        data = preprocess(data);
        System.out.println(data);

        System.out.print("full train? (y/n)");
        if (scan.next().equals("y")) {
            try {
                cls.buildClassifier(data);
            } catch (Exception ex) {
                Logger.getLogger(FFNNTubesAI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        int fold = 10;

        //FFNNTubesAI.printMatrix(cls.weight1, cls.input_layer+1, cls.hidden_layer);
        //FFNNTubesAI.printMatrix(cls.weight2, cls.hidden_layer, cls.output_layer);
        //FFNNTubesAI.printMatrix(cls.bias2, 1, cls.output_layer);
        Evaluation eval = new Evaluation(data);
        System.out.print("eval/10-fold? (e/f)");
        if (scan.next().equals("e")) {
            eval.evaluateModel(cls, data);
        } else {
            eval.crossValidateModel(cls, data, fold, new Random(1));
        }
        System.out.println(eval.toSummaryString());
        System.out.println(eval.toMatrixString());
        System.out.println(eval.toClassDetailsString());
    }
}