我正在尝试调试我的程序。要查看它出错的地方,我需要查看输出。但是因为它无限期地使用递归,所以停止程序的唯一方法是重新编译擦除输出。我该如何调试?
我正在调用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);
}
}
答案 0 :(得分:1)
所有主要IDE都带有调试功能。您可以使用这些调试器轻松调试java文件。
您还可以在线查找大量视频。只需搜索Java调试。