0
我正在嘗試調試我的程序。要查看哪裏出錯,我需要查看輸出。但是,因爲它無限期地使用遞歸,停止程序的唯一方法是重新編譯擦除輸出。我應該如何調試?如何用無限循環調試程序?
我打電話給DTNode。
這裏是我的類:
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);
}
}
使用調試器... – LeatherFace
我不明白爲什麼調試器不會工作... – Kon
使用調試器?用'System.out.println'語句顯示變量的狀態?基本上,就像你會調試其他任何東西。 – yshavit