Github user jodersky commented on a diff in the pull request:
https://github.com/apache/spark/pull/10231#discussion_r47274795
--- Diff:
mllib/src/main/scala/org/apache/spark/ml/tree/impl/RandomForest.scala ---
@@ -842,60 +842,63 @@ private[ml] object RandomForest extends Logging {
1.0
}
logDebug("fraction of data used for calculating quantiles = " +
fraction)
- input.sample(withReplacement = false, fraction, new
XORShiftRandom(seed).nextInt()).collect()
+ input.sample(withReplacement = false, fraction, new
XORShiftRandom(seed).nextInt())
} else {
- new Array[LabeledPoint](0)
+ input.sparkContext.emptyRDD[LabeledPoint]
}
- val splits = new Array[Array[Split]](numFeatures)
-
- // Find all splits.
- // Iterate over all features.
- var featureIndex = 0
- while (featureIndex < numFeatures) {
- if (metadata.isContinuous(featureIndex)) {
- val featureSamples = sampledInput.map(_.features(featureIndex))
- val featureSplits = findSplitsForContinuousFeature(featureSamples,
metadata, featureIndex)
+ findSplitsBinsBySorting(sampledInput, metadata, continuousFeatures)
+ }
- val numSplits = featureSplits.length
- logDebug(s"featureIndex = $featureIndex, numSplits = $numSplits")
- splits(featureIndex) = new Array[Split](numSplits)
+ private def findSplitsBinsBySorting(
+ input: RDD[LabeledPoint],
+ metadata: DecisionTreeMetadata,
+ continuousFeatures: IndexedSeq[Int]): Array[Array[Split]] = {
+
+ val continuousSplits = {
+ // reduce the parallelism for split computations when there are less
+ // continuous features than input partitions. this prevents tasks
from
+ // being spun up that will definitely do no work.
+ val numPartitions = math.min(continuousFeatures.length,
input.partitions.length)
+
+ input
+ .flatMap(point => continuousFeatures.map(idx => (idx,
point.features(idx))))
+ .groupByKey(numPartitions)
+ .map { case (idx, samples) =>
+ val thresholds = findSplitsForContinuousFeature(samples.toArray,
metadata, idx)
+ val splits: Array[Split] = thresholds.map(thresh => new
ContinuousSplit(idx, thresh))
+ logDebug(s"featureIndex = $idx, numSplits = ${splits.length}")
+ (idx, splits)
+ }.collectAsMap()
+ }
- var splitIndex = 0
- while (splitIndex < numSplits) {
- val threshold = featureSplits(splitIndex)
- splits(featureIndex)(splitIndex) = new
ContinuousSplit(featureIndex, threshold)
- splitIndex += 1
- }
- } else {
- // Categorical feature
- if (metadata.isUnordered(featureIndex)) {
- val numSplits = metadata.numSplits(featureIndex)
- val featureArity = metadata.featureArity(featureIndex)
- // TODO: Use an implicit representation mapping each category to
a subset of indices.
- // I.e., track indices such that we can calculate the set
of bins for which
- // feature value x splits to the left.
- // Unordered features
- // 2^(maxFeatureValue - 1) - 1 combinations
- splits(featureIndex) = new Array[Split](numSplits)
- var splitIndex = 0
- while (splitIndex < numSplits) {
- val categories: List[Double] =
- extractMultiClassCategories(splitIndex + 1, featureArity)
- splits(featureIndex)(splitIndex) =
- new CategoricalSplit(featureIndex, categories.toArray,
featureArity)
- splitIndex += 1
- }
- } else {
- // Ordered features
- // Bins correspond to feature values, so we do not need to
compute splits or bins
- // beforehand. Splits are constructed as needed during
training.
- splits(featureIndex) = new Array[Split](0)
+ val numFeatures = metadata.numFeatures
+ val splits = Range(0, numFeatures).map {
+ case i if metadata.isContinuous(i) =>
+ val split = continuousSplits(i)
+ metadata.setNumSplits(i, split.length)
+ split
+
+ case i if metadata.isCategorical(i) && metadata.isUnordered(i) =>
+ // Unordered features
+ // 2^(maxFeatureValue - 1) - 1 combinations
+ val featureArity = metadata.featureArity(i)
+ val split: IndexedSeq[Split] = Range(0, metadata.numSplits(i)).map
{ splitIndex =>
--- End diff --
You could use an Array.tablulate here. Something like
```scala
Array.tabulate[Split](numSplits(i)){splitIndex =>
...
}
```
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]