Reputation: 175
I'm trying to debug my program. To see where it goes wrong, I need to look at the output. However because it uses recursion indefinitely, the only way to stop the program is to recompile which wipes the output. How should I debug?
I'm calling DTNode.
Here's my class:
package DecisionTree;
public class DTNode {
Instance[] instances;
double cutoff;
DTNode left, right;
public DTNode (Instance[] instance, int l, int r) {
this.instances = instance;
int i;
int j = 0;
int k = 0;
int getIndex;
double[] cutoff = new double[instance.length];
double[] entropy = new double[instance.length];
int[] split = new int[instance.length];
double smallestEntropy;
for(i=0; i<r; i++) {
if(instance[i].label != instance[i+1].label) {
cutoff[j] = (instance[i].attribute + instance[i+1].attribute)/2;
split[j] = i;
System.out.println("Cutoff is: " + cutoff[j] + ". Split is: " + split[j]);
j++;
}
}
for(k=0; k<j; k++) {
entropy[k] = calcEntropy(instance, l, cutoff[k], r);
System.out.println(entropy[k]);
}
smallestEntropy = entropy[0];
getIndex = split[0];
for(k=1; k<j; k++) {
if (entropy[k] < smallestEntropy) {
smallestEntropy = entropy[k];
getIndex = k;
}
}
System.out.println(getIndex + " " + entropy[getIndex]);
if((r - l) == 0 || j == 0) {
this.left = null;
this.right = null;
}
else{
this.left = new DTNode(instance, l, getIndex);
this.right = new DTNode(instance, getIndex+1, r);
}
}
public double calcEntropy(Instance[] inst, int a, double b, int c) {
int i;
double leftSideCounter = 0;
double rightSideCounter = 0;
double leftTrue = 0;
double rightTrue = 0;
double leftSideEntropy = 0;
double rightSideEntropy = 0;
double leftTrueFraction;
double leftFalseFraction;
double rightTrueFraction;
double rightFalseFraction;
double leftTotalFraction;
double rightTotalFraction;
double entropy;
for(i=a; i<=c; i++){
if(inst[i].attribute < b) {
leftSideCounter++;
}
else {
rightSideCounter++;
}
}
for(i=0; i<leftSideCounter; i++) {
if(inst[i].label == true) {
leftTrue++;
}
}
for(i=(int)leftSideCounter; i<(int)(rightSideCounter+leftSideCounter); i++) {
if(inst[i].label == true) {
rightTrue++;
}
}
leftTrueFraction = leftTrue/leftSideCounter;
leftFalseFraction = (leftSideCounter-leftTrue)/leftSideCounter;
rightTrueFraction = rightTrue/rightSideCounter;
rightFalseFraction = (rightSideCounter-rightTrue)/rightSideCounter;
leftTotalFraction = leftSideCounter/(leftSideCounter+rightSideCounter);
rightTotalFraction = rightSideCounter/(leftSideCounter+rightSideCounter);
if(leftTrue == 0 || (leftSideCounter - leftTrue) == 0) {
leftSideEntropy = 0;
}
else{
leftSideEntropy = -leftTrueFraction*logb2(leftTrueFraction)-leftFalseFraction*logb2(leftFalseFraction);
}
if (rightTrue == 0 || (rightSideCounter - rightTrue) == 0) {
rightSideEntropy = 0;
}
else{
rightSideEntropy = -rightTrueFraction*logb2(rightTrueFraction)-rightFalseFraction*logb2(rightFalseFraction);
}
entropy = leftSideEntropy*leftTotalFraction+rightSideEntropy*rightTotalFraction;
System.out.println(leftTrue);
System.out.println("leftTrueFraction = " + leftTrueFraction + ". leftFalseFraction = " + leftFalseFraction);
System.out.println("rightTrueFraction = " + rightTrueFraction + ". rightFalseFraction = " + rightFalseFraction);
System.out.println("leftTotalFraction = " + leftTotalFraction + ". rightTotalFraction = " + rightTotalFraction);
System.out.println("leftSideEntropy = " + leftSideEntropy + ". rightSideEntropy = " + rightSideEntropy);
return entropy;
}
public double logb2(double b) {
return Math.log(b)/Math.log(2);
}
}
Upvotes: 0
Views: 4063
Reputation: 14707
All major IDE comes with debugging capability. You can easily debug the java file using those debugger.
You can also find tons of video online. Just search Java debugging.
Upvotes: 1