我有一个构造函数,它从这个 .data 文件中获取数据:
问题:
- 我的代码没有将每行中的最后一个值初始化为 -1 或 1
最后一个数字通常是 0 或 1,在这个 .data 文件中它是第 13 个索引。如果它是 0,我希望它存储为 -1,如果它是 1,我希望它作为 1 存储在 data[][] 数组中。
下面是具体代码:
// 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
}
}
}