patternjavaMinor
General Java Style Suggestions - Classification Problem from CodeSprint 2012
Viewed 0 times
fromproblem2012javageneralstylecodesprintsuggestionsclassification
Problem
```
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
/**
* Solution for Quora Classifier question from CodeSprint 2012 This class
* implements a logistic regression classifier trained with Stochastic Gradient
* Descent. No regularization is used as it wasn't necessary for this problem
*/
public class QuoraClassifier {
static boolean debug;
double[] theta; // regression coefficients
double[] yTraining; // training targets
double[][] xTraining; // training predictors
double[] xMean; // mean of each training predictor
double[] xVarSqrt; // variance of each training predictor
double alpha = 5; // training rate
int numTrainingExamples; // number of training examples
int numFeatures; // number of features in predictor
/**
* Load training data
*
* @param sc
*/
private void loadTrainingData(Scanner sc) {
String[] trainingDimString = sc.nextLine().split("\\s");
numTrainingExamples = Integer.parseInt(trainingDimString[0]);
numFeatures = Integer.parseInt(trainingDimString[1]);
yTraining = new double[numTrainingExamples];
xTraining = new double[numTrainingExamples][numFeatures];
for (int i = 0; i 0.0) {
xVarSqrt[fIndex] = Math.sqrt(runningSum / numTrainingExamples);
} else {
xVarSqrt[fIndex] = 1.0;
}
}
// normalize by feature variances
for (int fIndex = 0; fIndex 0.5) {
System.out.printf("%s +1\n", label);
} else {
System.out.printf("%s -1\n", label);
}
if (debug) {
// read training targets
String[] outString = scOut.nextLine().split("\\s");
String correctClassification = outString[1];
// display misclassifications
if (classification > 0.5) {
if (correctClassification.equals("-1")) {
numMisclassifications++;
System.out.printf("%s +1 %s\n", label, correctClassification);
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
/**
* Solution for Quora Classifier question from CodeSprint 2012 This class
* implements a logistic regression classifier trained with Stochastic Gradient
* Descent. No regularization is used as it wasn't necessary for this problem
*/
public class QuoraClassifier {
static boolean debug;
double[] theta; // regression coefficients
double[] yTraining; // training targets
double[][] xTraining; // training predictors
double[] xMean; // mean of each training predictor
double[] xVarSqrt; // variance of each training predictor
double alpha = 5; // training rate
int numTrainingExamples; // number of training examples
int numFeatures; // number of features in predictor
/**
* Load training data
*
* @param sc
*/
private void loadTrainingData(Scanner sc) {
String[] trainingDimString = sc.nextLine().split("\\s");
numTrainingExamples = Integer.parseInt(trainingDimString[0]);
numFeatures = Integer.parseInt(trainingDimString[1]);
yTraining = new double[numTrainingExamples];
xTraining = new double[numTrainingExamples][numFeatures];
for (int i = 0; i 0.0) {
xVarSqrt[fIndex] = Math.sqrt(runningSum / numTrainingExamples);
} else {
xVarSqrt[fIndex] = 1.0;
}
}
// normalize by feature variances
for (int fIndex = 0; fIndex 0.5) {
System.out.printf("%s +1\n", label);
} else {
System.out.printf("%s -1\n", label);
}
if (debug) {
// read training targets
String[] outString = scOut.nextLine().split("\\s");
String correctClassification = outString[1];
// display misclassifications
if (classification > 0.5) {
if (correctClassification.equals("-1")) {
numMisclassifications++;
System.out.printf("%s +1 %s\n", label, correctClassification);
Solution
Just off the top of my head:
- In main - what should happen if
args.length == 1? Currently, you'll get an exception.
- Your field names are a bit odd -
xTrainingandyTrainingin particular. Instead of having these names, and comments about what the variables actually mean, why not just call your variablesregressionCoefficients,trainingTargets,trainingPredictorsand so on?
- Some of the methods are a bit long. The fact that your methods mostly have internal comments kind of indicates this. In my opinion, each method should do just one thing; and that one thing should be explained in a javadoc comment at the top of the method. No comments at all inside methods.
- Comments should be used to explain things, not just to repeat what's in the code. For example, your "return" comment at the top of
sigmoidis pointless. I suggest removing it.
- Do you really want to use
System.out.printffor logging? There are more versatile ways of logging (which you could google), and more readable means of formatting.
Context
StackExchange Code Review Q#7812, answer score: 3
Revisions (0)
No revisions yet.