2014-04-08 4 views
0

I이 .DATA 파일의 데이터에 사용하는 생성자 가지고1 또는 -1

Data

문제점 :

  • 내 코드가 각 행의 마지막 값을 -1 또는 1로 초기화하지 않습니다.

마지막 숫자는 보통 0 또는 1이며이 .data 파일에서는 13 번째 색인입니다. 그것이 0 인 경우 -1로 저장되고, 1이면 데이터 [] [] 배열에 1로 저장됩니다. 여기

// Copy over data form the .data file 
      for (int j = 0; j < data[i].length; j++) { 
       // For each row... 
       if (!numbers[j].equals(qMarks)) { 
        // If the values in each row do not equal "?" 
        // Set rows[i] to the values in column[i] 
        if (j == 13){ 
         if (numbers[j].equals(positiveHeartCondition)){ 
          data[i][j] = 1; 
         } 
         else if (numbers[j].equals(negativeHeartCondition)){ 
          data[i][j] = -1; 
         } 
        } 
        data[i][j] = Double.parseDouble(numbers[j]); 
        // System.out.println(data[i][j]); //Test code to see 
        // what's printed here 
       } else { 
        data[i][j] = 0; 
        // System.out.println(data[i][j]); //Test code to see 
        // what's printed here 
       } 
      } 

내가 생성자에 대해 가지고있는 코드입니다 : 여기

가 특정 코드

public class NeuralNetwork { 
/* 
* Setup an 2-d array of doubles to store values from a .data file This will 
* be used in the NeuralNetwork constructor 
*/ 
protected double[][] data; 

/* 
* Declare a double[][] array, and randomize the weights of the double array 
* in constructor This will be used in the train(), randomizeWeights(), 
* getWeights() methods 
*/ 
public double[][] weights; 

/* 
* We set a double field named eta equal to 0.05. Used in the train() and 
* error() methods 
*/ 
protected double eta = 0.01; 

/* 
* These are values for the NeuralNetwork Constructor 
*/ 
private final String comma = ","; 
private final String qMarks = "?"; 
private final String positiveHeartCondition = "1"; 
private final String negativeHeartCondition = "0"; 
private Node[] input; // Input Nodes 
private Node[] hiddenNodeLayer; // Array of HiddenNode to hold input in each 
           // HiddenNode 
private Node outputNode; // Take the input of hiddenNodeLayer 

// We initialize a constructor which only takes a parameter int n. 
public NeuralNetwork(File f) { 

    int noOfNodes = 0; 

    try { 
     @SuppressWarnings("resource") 
     Scanner inFile = new Scanner(f); 

     int noOfRowsInData = 0; 

     LineNumberReader lnr = new LineNumberReader(new FileReader(f)); 
     try { 
      lnr.skip(Long.MAX_VALUE); 
      noOfRowsInData = lnr.getLineNumber(); 
      lnr.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     String line = inFile.nextLine(); 

     String[] numbers = line.split(comma); 

     data = new double[noOfRowsInData][numbers.length]; 

     noOfNodes = numbers.length - 1; 

     int i = 0; 
     // While there is another line in inFile. 
     inFile = new Scanner(f); 

     while (inFile.hasNextLine()) { 
      // Store that line into String line 
      line = inFile.nextLine(); 
      // System.out.println(line);//Test code to see what the file 
      // looks like 
      // Parition values separated by a comma 
      numbers = line.split(comma); 
      // System.out.println(Arrays.toString(columns)); //Test code to 
      // see length of each column 
      /* 
      * code works and prints out row 
      */ 
      /* 
      * initialize noOfNodes to length of numbers - 1 
      */ 

      // This will count the number of rows in the .data file 

      // System.out.println(data[0].length); //Test code works 
      // properly, and prints numbers.length 

      // Copy over data form the .data file 
      for (int j = 0; j < data[i].length; j++) { 
       // For each row... 
       if (!numbers[j].equals(qMarks)) { 
        // If the values in each row do not equal "?" 
        // Set rows[i] to the values in column[i] 
        if (j == 13){ 
         if (numbers[j].equals(positiveHeartCondition)){ 
          data[i][j] = 1; 
         } 
         else if (numbers[j].equals(negativeHeartCondition)){ 
          data[i][j] = -1; 
         } 
        } 
        data[i][j] = Double.parseDouble(numbers[j]); 
        // System.out.println(data[i][j]); //Test code to see 
        // what's printed here 
       } else { 
        data[i][j] = 0; 
        // System.out.println(data[i][j]); //Test code to see 
        // what's printed here 
       } 
      } 
      i++; 
     } 

     // System.out.println(data.length); //See what the length of the 
     // data double array is. works 
     // Test code to print out the 2-d double array and see what is being 
     // stored 
     for (int k = 0; k < data.length; k++) { 
     System.out.println("For Row[" + k + "] of file:"); // Works 
     for (int j = 0; j < data[k].length; j++) { 
     System.out.println(" data[" + k + "][" + j + "] = " 
     + data[k][j]); // Works 
     } 
     } 
     } 
+1

디버거에서 코드를 실행하십시오. 'if (j == 13)'줄에 중단 점을 설정하십시오. 중단 점이 나올 때 코드를 단계별로 실행하여 변수의 정확한 값을 확인하고 왜 그렇게해야한다고 생각하는지 판단합니다. – Jesper

+0

이 주석을 만들기 직전에 해결 방법을 찾았습니다 :-)하지만 if 및 else 문이 너무 많습니다. '( –

답변

0

내가 해결 방법을 찾았지만 나는 경우 많은 사용하지 않는 것을 선호하고 else 문 :/

for (int j = 0; j < data[i].length; j++) { 
       // For each row... 
       if (!numbers[j].equals(qMarks)) { 
        // If the values in each row do not equal "?" 
        // Set rows[i] to the values in column[i] 
        if (j != 13){ 
         data[i][j] = Double.parseDouble(numbers[j]); 
         } 
        else { 
         if (j == 13 && numbers[j].equals(positiveHeartCondition)){ 
          data[i][j] = 1; 
         } 
         else if (j == 13 && numbers[j].equals(negativeHeartCondition)){ 
          data[i][j] = -1; 
         } 
        } 
        // System.out.println(data[i][j]); //Test code to see 
        // what's printed here 
       } else { 
        data[i][j] = 0; 
        // System.out.println(data[i][j]); //Test code to see 
        // what's printed here 
       } 
      }