Package

Source Code of ArrhythmiaTest

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import mit.nn.flvqpso.FLVQPSO;

public class ArrhythmiaTest {
 
  public static void main(String[] args) {
   
    List<List<Double>> inputData = new ArrayList<List<Double>>();
    List<String> targetClass = new ArrayList<String>();
    String fileName = "dataTest15kelas.txt";
//    String fileName = "rgb.csv";
//    String fileName = "ecg.csv";
   
    readInput(fileName, inputData, targetClass);
   
    //print(inputData, targetClass);
   
    int layer = 1;
    int feature = 300;
    int numTargetClass = 14;
    int fuzzygroup = 3;
   
//    int layer = 1;
//    int feature = 11;
//    int numTargetClass = 2;
//    int fuzzygroup = 3;
   
    FLVQPSO pso = new FLVQPSO(layer, numTargetClass, feature, fuzzygroup);
   
    pso.init(inputData, targetClass);
   
    //pso.printVectorReference();
   
    int epoch = 100;
   
    System.out.println("Sebelum training");
    pso.testing(inputData, targetClass);
    //pso.printVectorReference();
   
    pso.training(epoch, inputData, targetClass);
   
    System.out.println("Sesudah training");
    pso.testing(inputData, targetClass);
    //pso.printVectorReference();
   
    System.out.println(pso.doClassification(inputData.get(0)));
    System.out.println(pso.doClassification(inputData.get(1)));
   
    List<double[]> ref = pso.getVectorReference();
    printToFile(ref);
   
    List<String> list = readFile();
    pso.setVectorReference(list);
    pso.testing(inputData, targetClass);
  }
 
  private static void printToFile(List<double[]> ref) {
    try {
      BufferedWriter bw = new BufferedWriter(new FileWriter("reference.txt"));
      for(double[] d : ref) {
        String text = d[0]+","+d[1]+","+d[2];
        bw.write(text);
        bw.write("\n");
      }
      bw.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
 
  private static List<String> readFile() {
    List<String> list = new ArrayList<String>();
   
    try {
      BufferedReader br = new BufferedReader(new FileReader("reference.txt"));
      String line = "";
      while((line = br.readLine()) != null) {
        list.add(line);
      }
     
      br.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return list;
  }

  public static void readInput(String fileName, List<List<Double>> input, List<String> target) {
   
    try {
      BufferedReader br = new BufferedReader(new FileReader(fileName));
     
      String line = "";
      while((line = br.readLine()) != null) {
        String[] lines = line.split(",");
       
        List<Double> vector = new ArrayList<Double>();
        for(int i=0; i<lines.length-1; i++) {
          double d = Double.parseDouble(lines[i]);
          vector.add(d);
        }
        input.add(vector);
        target.add(lines[lines.length-1]);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
   
  }
 
  private static void print(List<List<Double>> data, List<String> target) {
    for(int i=0; i<data.size(); i++) {
      List<Double> vector = data.get(i);
      for(int j=0; j<vector.size(); j++) {
        System.out.print(vector.get(j)+",");
      }
      System.out.println(target.get(i));
    }
  }

}
TOP

Related Classes of ArrhythmiaTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.