Skip to content

Commit 797f68a

Browse files
committed
Fixed DecisionTreeSuite bug for training second level. Needed to update treePointToNodeIndex with groupShift.
1 parent f40381c commit 797f68a

2 files changed

Lines changed: 8 additions & 15 deletions

File tree

mllib/src/main/scala/org/apache/spark/mllib/tree/DecisionTree.scala

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,8 @@ object DecisionTree extends Serializable with Logging {
635635
0
636636
} else {
637637
val globalNodeIndex = predictNodeIndex(nodes(0), treePoint.binnedFeatures)
638-
// Get index for this level.
639-
globalNodeIndex - levelOffset
638+
// Get index for this (level, group).
639+
globalNodeIndex - levelOffset - groupShift
640640
}
641641
}
642642

@@ -660,12 +660,6 @@ object DecisionTree extends Serializable with Logging {
660660
numClasses * numBins * featureIndex +
661661
numClasses * treePoint.binnedFeatures(featureIndex) +
662662
treePoint.label.toInt
663-
if (aggIndex >= agg.size) {
664-
println(s"nodeIndex = $nodeIndex, featureIndex = $featureIndex," +
665-
s" treePoint.binnedFeatures(featureIndex) = ${treePoint.binnedFeatures(featureIndex)}," +
666-
s" label = ${treePoint.label.toInt}, agg.size = ${agg.size}")
667-
println(s"numClasses = $numClasses, numFeatures = $numFeatures, numBins = $numBins")
668-
}
669663
agg(aggIndex) += 1
670664
}
671665

@@ -808,7 +802,10 @@ object DecisionTree extends Serializable with Logging {
808802
*/
809803
def binSeqOp(agg: Array[Double], treePoint: TreePoint): Array[Double] = {
810804
val nodeIndex = treePointToNodeIndex(treePoint)
811-
if (nodeIndex >= 0) { // Otherwise, example does not reach this level.
805+
// If the example does not reach this level, then nodeIndex < 0.
806+
// If the example reaches this level but is handled in a different group,
807+
// then either nodeIndex < 0 (previous group) or nodeIndex >= numNodes (later group).
808+
if (nodeIndex >= 0 && nodeIndex < numNodes) {
812809
strategy.algo match {
813810
case Classification =>
814811
if (isMulticlassClassificationWithCategoricalFeatures) {
@@ -825,7 +822,6 @@ object DecisionTree extends Serializable with Logging {
825822
// Calculate bin aggregate length for classification or regression.
826823
val binAggregateLength = numNodes * getElementsPerNode(numFeatures, numBins, numClasses,
827824
isMulticlassClassificationWithCategoricalFeatures, strategy.algo)
828-
println("binAggregateLength = " + binAggregateLength)
829825
logDebug("binAggregateLength = " + binAggregateLength)
830826

831827
/**

mllib/src/test/scala/org/apache/spark/mllib/tree/DecisionTreeSuite.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,7 @@ class DecisionTreeSuite extends FunSuite with LocalSparkContext {
585585
assert(bestSplits(0)._2.predict === 1)
586586
}
587587

588-
// TODO: Decide about testing 2nd level
589588
test("second level node building with/without groups") {
590-
println("START second level node building with/without groups")
591589
val arr = DecisionTreeSuite.generateOrderedLabeledPoints()
592590
assert(arr.length === 1000)
593591
val rdd = sc.parallelize(arr)
@@ -605,10 +603,9 @@ class DecisionTreeSuite extends FunSuite with LocalSparkContext {
605603
val modelOneNode = DecisionTree.train(rdd, strategyOneNode)
606604
val nodes: Array[Node] = new Array[Node](7)
607605
nodes(0) = modelOneNode.topNode
606+
nodes(0).leftNode = None
607+
nodes(0).rightNode = None
608608

609-
/* val leftFilter = Filter(new Split(0, 400, FeatureType.Continuous, List()), -1)
610-
val rightFilter = Filter(new Split(0, 400, FeatureType.Continuous, List()) ,1)
611-
val filters = Array[List[Filter]](List(), List(leftFilter), List(rightFilter))*/
612609
val parentImpurities = Array(0.5, 0.5, 0.5)
613610

614611
// Single group second level tree construction.

0 commit comments

Comments
 (0)