Weka[23] PART源代码分析

protected double getSizeOfBranch() {

if (m_isLeaf) {

return -localModel().distribution().total(); } else

return son(indeX).getSizeOfBranch();

}

刚才说是最多样本的下标,你可能有点意外,明明是 Utils.sm,可是你看

getSizeOfBranch 中的第一个 return 后面是有负号的,我想这种写法是不是想到样本权重 之和为 0 这种情况考虑进去。

public double classifyInstance(Instance instance) throws Exception {

double maxProb = -1; double currentProb; int maxIndex = 0; int j;

for (j = 0; j < instance.numClasses(); j++) {

currentProb = getProbs(j, instance, 1); if (Utils.gr(currentProb, maxProb)) {

maxIndex = j;

maxProb = currentProb; } }

if (Utils.eq(maxProb, 0))

return -1.0; else

return (double) maxIndex;

}

看起来与 J48 蛮相似的,我们也知道还需要看的函数就是 getProbs 了(总不至于下面几 行看不懂吧)。

private double getProbs(int classIndex, Instance instance, double weight)

throws Exception {

double[] weights; int treeIndex;

if (m_isLeaf) {

return weight * localModel().classProb(classIndex, instance, -1); } else {

treeIndex = localModel().whichSubset(instance); if (treeIndex == -1) {

weights = localModel().weights(instance); return son(indeX).getProbs(classIndex, instance,

weights[indeX] * weight);

} else {

if (treeIndex == indeX) {

return son(indeX).getProbs(classIndex, instance,

weight);

} else {

return 0; } } }

}

这里其实也与 J48 没有什么区别,我们看一下 else 中的第一个 if,treeIndex==-1 的 时候是这表示,在分裂属性上这个样本的属性值是缺失的,weights 数是计算每个叶子结点 除总权重的权重,而下面就是递归了。再看 else,如果 treeIndex==index 那么是可以递归 的,不然就不符合归则,就返回 0。

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4