patternjavaMinor
Classification Problem from CodeSprint 2012 (Updated)
Viewed 0 times
problem2012classificationcodesprintupdatedfrom
Problem
I've updated the previous version to be more object oriented as suggested.
```
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
/**
* A class containing a single record. Each record contains the Quora answer
* ID and the predictor vector. If the record is a training example then the
* target classification is also included.
*/
private class Record {
private final Vector predictor;
private final double target;
private final String answerId;
/**
* Constructor for test records
*/
public Record(Vector predictor, String answerId) {
this.predictor = predictor;
this.answerId = answerId;
this.target = 0;
}
/**
* Constructor for training records
*/
public Record(Vector predictor, String answerId, double target) {
this.predictor = predictor;
this.answerId = answerId;
this.target = target;
}
public Vector getPredictor() {
return predictor;
}
public double getTarget() {
return target;
}
public String getAnswerId() {
return answerId;
}
}
/**
* Class for storing a list of records. The constructor reads the records from
* the training or test sections of the input file as appropriate
*/
private class RecordList extends ArrayList {
private static final long serialVersionUID = 1L;
private final int numFeatures; // number of features in predictor
/**
* Constructor which loads records from training part of input file
*
* @param sc
* scanner to read training data from
*/
public RecordList(Scanner sc) {
String[] dimString = sc.nextLine().split("\\s");
int numRecords = Integer.parseInt(dimString[0]);
numFeatures = Integer.parseInt(dimString[1]);
for (int i = 0; i 0.5) {
System.out.printf("%s +1\n", record.getAnswerId());
} else {
System.out.printf("%s -1\n", record.getAnswerId());
}
}
}
}
```
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
/**
* A class containing a single record. Each record contains the Quora answer
* ID and the predictor vector. If the record is a training example then the
* target classification is also included.
*/
private class Record {
private final Vector predictor;
private final double target;
private final String answerId;
/**
* Constructor for test records
*/
public Record(Vector predictor, String answerId) {
this.predictor = predictor;
this.answerId = answerId;
this.target = 0;
}
/**
* Constructor for training records
*/
public Record(Vector predictor, String answerId, double target) {
this.predictor = predictor;
this.answerId = answerId;
this.target = target;
}
public Vector getPredictor() {
return predictor;
}
public double getTarget() {
return target;
}
public String getAnswerId() {
return answerId;
}
}
/**
* Class for storing a list of records. The constructor reads the records from
* the training or test sections of the input file as appropriate
*/
private class RecordList extends ArrayList {
private static final long serialVersionUID = 1L;
private final int numFeatures; // number of features in predictor
/**
* Constructor which loads records from training part of input file
*
* @param sc
* scanner to read training data from
*/
public RecordList(Scanner sc) {
String[] dimString = sc.nextLine().split("\\s");
int numRecords = Integer.parseInt(dimString[0]);
numFeatures = Integer.parseInt(dimString[1]);
for (int i = 0; i 0.5) {
System.out.printf("%s +1\n", record.getAnswerId());
} else {
System.out.printf("%s -1\n", record.getAnswerId());
}
}
}
}
Solution
I just looked briefly but this looks good. I would try to be more expressive with some of your variable names and break the methods up a little more. Also, I think you can get away without using clone.
Context
StackExchange Code Review Q#7941, answer score: 2
Revisions (0)
No revisions yet.