Weka[22] REPTree源代码分析(1).docVIP

  • 12
  • 0
  • 约 12页
  • 2017-08-02 发布于河南
  • 举报
Weka[22] REPTree 源代码分析 作者:Koala++/屈伟 如果你分析完了ID3,还想进一步学习,最好还是先学习REPTree,它没有牵扯到那么 多类,两个类完成了全部的工作,看起来比较清楚,J48虽然有很强的可扩展性,但是初看 起来还是有些费力,REPTree也是我卖算法时(为了买一台运算能力强一点的计算机,我也 不得不赚钱),顺便分析的,但因为我以前介绍过J48了,重复的东西不想再次介绍了,如 果有什么不明白的,就把我两篇写的结合起来看吧。 我们再次从buildClassifier开始。 Random random = new Random(m_Seed); m_zeroR = null; if (data.numAttributes() == 1) { m_zeroR = new ZeroR(); m_zeroR.buildClassifier(data); return; } 如果就只有一个属性,也就是类别属性,就用 ZeroR 分类器学习,ZeroR 分类器返回训 练集中出现最多的类别值,已经讲过了 Weka 开发[15]。 // Randomize and stratify data.randomize(random); if (data.classAttribute().isNominal()) { data.stratify(m_NumFolds); } randomize 就是把 data 中的数据重排一下,如果类别属性是离散值,那么用 stratify 函 数,stratify 意思是分层,现在把这个函数列出来: public void stratify(int numFolds) { if (classAttribute().isNominal()) { // sort by class int index = 1; while (index numInstances()) { Instance instance1 = instance(index - 1); for (int j = index; j numInstances(); j++) { Instance instance2 = instance(j); if ((instance1.classValue() == instance2.classValue()) || (instance1.classIsMissing() instance2 .classIsMissing())) { swap(index, j); index++; } } index++; } stratStep(numFolds); } } 上面这两重循环,就是根据类别值进行冒泡。下面有调用了 stratStep 函数: protected void stratStep(int numFolds) { FastVector newVec = new FastVector(m_Instances.capacity()); int start = 0, j; // create stratified batch while (newVec.size() numInstances()) { j = start; while (j numInstances()) { newVec.addElement(instance(j)); j = j + numFolds; } start++; } m_Instances = newVec; } 这里我举一个例子说明:j=0 时,numFolds 为 10 时,newVec 加入的 instance 下标就为 0,10,20…。这样的好处就是我们把各种类别的样本类似平均分布了。 // Split data into training and pruning set Instances train = null; Instances prune = null; if (!m_NoPruning) { train = data.trainCV(m_NumFolds, 0, random); prune = data.testCV(m_NumFolds, 0); } else { train = data; } 关于 trainCV 这个就不讲了,就是 crossValidation 的第 0 个训练集作为这次的训练集(train)。 而作为剪枝的数据集 prune 为第 0 个测试集。 // Create array of sorted indices and weights int[][] sortedIndices = new int[train.numAttributes()][0]; double[][] w

文档评论(0)

1亿VIP精品文档

相关文档