我正在构建一个决策树分类器,我发现了这种计算信息增益的方法。这可能是一个愚蠢的问题,但我想知道这种方法中的拆分是针对数字属性还是分类属性?我很困惑,因为我认为阈值(中位数)用于数字拆分,但此方法使用字符串值。
任何帮助表示赞赏。
这是代码:
public static double getInfoGain(int f, ArrayList<String[]> dataSubset) {
double entropyBefore = getEntropy(dataSubset); //Entropy before split
if(entropyBefore != 0){ // Calculate information gain if entropy is not 0
String threshold = thresholdMap.get(f); // Get threshold value of the feature
ArrayList<String[]> leftData = new ArrayList<String[]>();
ArrayList<String[]> rightData = new ArrayList<String[]>();
for(String[] d : dataSubset) {
if(d[f].equals(threshold)) {
leftData.add(d); // If feature value of data == threshold, add it to leftData
} else {
rightData.add(d); // If feature value of data != threshold, add it to leftData
}
}
if(leftData.size() > 0 && rightData.size() > 0) {
double leftProb = (double)leftData.size()/dataSubset.size();
double rightProb = (double)rightData.size()/dataSubset.size();
double entropyLeft = getEntropy(leftData); //Entropy after split - left
double entropyRight = getEntropy(rightData); //Entropy after split - right
double gain = entropyBefore - (leftProb * entropyLeft) - (rightProb * entropyRight);
return gain;
} else { // If entropy = 0 on either subsets of data, return 0
return 0;
}
} else { // If entropy = 0 before split, return 1
return -1;
}
}