forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-3042] [mllib] DecisionTree Filter top-down instead of bottom-up
DecisionTree needs to match each example to a node at each iteration. It currently does this with a set of filters very inefficiently: For each example, it examines each node at the current level and traces up to the root to see if that example should be handled by that node. Fix: Filter top-down using the partly built tree itself. Major changes: * Eliminated Filter class, findBinsForLevel() method. * Set up node parent links in main loop over levels in train(). * Added predictNodeIndex() for filtering top-down. * Added DTMetadata class Other changes: * Pre-compute set of unorderedFeatures. Notes for following expected PR based on [https://issues.apache.org/jira/browse/SPARK-3043]: * The unorderedFeatures set will next be stored in a metadata structure to simplify function calls (to store other items such as the data in strategy). I've done initial tests indicating that this speeds things up, but am only now running large-scale ones. CC: mengxr manishamde chouqin Any comments are welcome---thanks! Author: Joseph K. Bradley <joseph.kurata.bradley@gmail.com> Closes apache#1975 from jkbradley/dt-opt2 and squashes the following commits: a0ed0da [Joseph K. Bradley] Renamed DTMetadata to DecisionTreeMetadata. Small doc updates. 3726d20 [Joseph K. Bradley] Small code improvements based on code review. ac0b9f8 [Joseph K. Bradley] Small updates based on code review. Main change: Now using << instead of math.pow. db0d773 [Joseph K. Bradley] scala style fix 6a38f48 [Joseph K. Bradley] Added DTMetadata class for cleaner code 931a3a7 [Joseph K. Bradley] Merge remote-tracking branch 'upstream/master' into dt-opt2 797f68a [Joseph K. Bradley] Fixed DecisionTreeSuite bug for training second level. Needed to update treePointToNodeIndex with groupShift. f40381c [Joseph K. Bradley] Merge branch 'dt-opt1' into dt-opt2 5f2dec2 [Joseph K. Bradley] Fixed scalastyle issue in TreePoint 6b5651e [Joseph K. Bradley] Updates based on code review. 1 major change: persisting to memory + disk, not just memory. 2d2aaaf [Joseph K. Bradley] Merge remote-tracking branch 'upstream/master' into dt-opt1 26d10dd [Joseph K. Bradley] Removed tree/model/Filter.scala since no longer used. Removed debugging println calls in DecisionTree.scala. 356daba [Joseph K. Bradley] Merge branch 'dt-opt1' into dt-opt2 430d782 [Joseph K. Bradley] Added more debug info on binning error. Added some docs. d036089 [Joseph K. Bradley] Print timing info to logDebug. e66f1b1 [Joseph K. Bradley] TreePoint * Updated doc * Made some methods private 8464a6e [Joseph K. Bradley] Moved TimeTracker to tree/impl/ in its own file, and cleaned it up. Removed debugging println calls from DecisionTree. Made TreePoint extend Serialiable a87e08f [Joseph K. Bradley] Merge remote-tracking branch 'upstream/master' into dt-opt1 c1565a5 [Joseph K. Bradley] Small DecisionTree updates: * Simplification: Updated calculateGainForSplit to take aggregates for a single (feature, split) pair. * Internal doc: findAggForOrderedFeatureClassification b914f3b [Joseph K. Bradley] DecisionTree optimization: eliminated filters + small changes b2ed1f3 [Joseph K. Bradley] Merge remote-tracking branch 'upstream/master' into dt-opt 0f676e2 [Joseph K. Bradley] Optimizations + Bug fix for DecisionTree 3211f02 [Joseph K. Bradley] Optimizing DecisionTree * Added TreePoint representation to avoid calling findBin multiple times. * (not working yet, but debugging) f61e9d2 [Joseph K. Bradley] Merge remote-tracking branch 'upstream/master' into dt-timing bcf874a [Joseph K. Bradley] Merge remote-tracking branch 'upstream/master' into dt-timing 511ec85 [Joseph K. Bradley] Merge remote-tracking branch 'upstream/master' into dt-timing a95bc22 [Joseph K. Bradley] timing for DecisionTree internals
- Loading branch information
Showing
9 changed files
with
615 additions
and
630 deletions.
There are no files selected for viewing
878 changes: 386 additions & 492 deletions
878
mllib/src/main/scala/org/apache/spark/mllib/tree/DecisionTree.scala
Large diffs are not rendered by default.
Oops, something went wrong.
101 changes: 101 additions & 0 deletions
101
mllib/src/main/scala/org/apache/spark/mllib/tree/impl/DecisionTreeMetadata.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.mllib.tree.impl | ||
|
||
import scala.collection.mutable | ||
|
||
import org.apache.spark.mllib.regression.LabeledPoint | ||
import org.apache.spark.mllib.tree.configuration.Algo._ | ||
import org.apache.spark.mllib.tree.configuration.QuantileStrategy._ | ||
import org.apache.spark.mllib.tree.configuration.Strategy | ||
import org.apache.spark.mllib.tree.impurity.Impurity | ||
import org.apache.spark.rdd.RDD | ||
|
||
|
||
/** | ||
* Learning and dataset metadata for DecisionTree. | ||
* | ||
* @param numClasses For classification: labels can take values {0, ..., numClasses - 1}. | ||
* For regression: fixed at 0 (no meaning). | ||
* @param featureArity Map: categorical feature index --> arity. | ||
* I.e., the feature takes values in {0, ..., arity - 1}. | ||
*/ | ||
private[tree] class DecisionTreeMetadata( | ||
val numFeatures: Int, | ||
val numExamples: Long, | ||
val numClasses: Int, | ||
val maxBins: Int, | ||
val featureArity: Map[Int, Int], | ||
val unorderedFeatures: Set[Int], | ||
val impurity: Impurity, | ||
val quantileStrategy: QuantileStrategy) extends Serializable { | ||
|
||
def isUnordered(featureIndex: Int): Boolean = unorderedFeatures.contains(featureIndex) | ||
|
||
def isClassification: Boolean = numClasses >= 2 | ||
|
||
def isMulticlass: Boolean = numClasses > 2 | ||
|
||
def isMulticlassWithCategoricalFeatures: Boolean = isMulticlass && (featureArity.size > 0) | ||
|
||
def isCategorical(featureIndex: Int): Boolean = featureArity.contains(featureIndex) | ||
|
||
def isContinuous(featureIndex: Int): Boolean = !featureArity.contains(featureIndex) | ||
|
||
} | ||
|
||
private[tree] object DecisionTreeMetadata { | ||
|
||
def buildMetadata(input: RDD[LabeledPoint], strategy: Strategy): DecisionTreeMetadata = { | ||
|
||
val numFeatures = input.take(1)(0).features.size | ||
val numExamples = input.count() | ||
val numClasses = strategy.algo match { | ||
case Classification => strategy.numClassesForClassification | ||
case Regression => 0 | ||
} | ||
|
||
val maxBins = math.min(strategy.maxBins, numExamples).toInt | ||
val log2MaxBinsp1 = math.log(maxBins + 1) / math.log(2.0) | ||
|
||
val unorderedFeatures = new mutable.HashSet[Int]() | ||
if (numClasses > 2) { | ||
strategy.categoricalFeaturesInfo.foreach { case (f, k) => | ||
if (k - 1 < log2MaxBinsp1) { | ||
// Note: The above check is equivalent to checking: | ||
// numUnorderedBins = (1 << k - 1) - 1 < maxBins | ||
unorderedFeatures.add(f) | ||
} else { | ||
// TODO: Allow this case, where we simply will know nothing about some categories? | ||
require(k < maxBins, s"maxBins (= $maxBins) should be greater than max categories " + | ||
s"in categorical features (>= $k)") | ||
} | ||
} | ||
} else { | ||
strategy.categoricalFeaturesInfo.foreach { case (f, k) => | ||
require(k < maxBins, s"maxBins (= $maxBins) should be greater than max categories " + | ||
s"in categorical features (>= $k)") | ||
} | ||
} | ||
|
||
new DecisionTreeMetadata(numFeatures, numExamples, numClasses, maxBins, | ||
strategy.categoricalFeaturesInfo, unorderedFeatures.toSet, | ||
strategy.impurity, strategy.quantileCalculationStrategy) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
mllib/src/main/scala/org/apache/spark/mllib/tree/model/Filter.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.