-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-16957][MLlib] Use midpoints for split values. #17556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
45b7493
c49d3ae
387eb49
2e68f1e
6a5806f
7ad590d
0aaed66
c07ffac
9ca5750
b74702a
76f4ae8
031c61a
1459b14
a094029
7c50d4a
7bb11dd
ae0e48e
59866fa
10037ea
1cae998
92df1c8
591d790
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -996,7 +996,7 @@ private[spark] object RandomForest extends Logging { | |
| require(metadata.isContinuous(featureIndex), | ||
| "findSplitsForContinuousFeature can only be used to find splits for a continuous feature.") | ||
|
|
||
| val splits = if (featureSamples.isEmpty) { | ||
| val splits: Array[Double] = if (featureSamples.isEmpty) { | ||
| Array.empty[Double] | ||
| } else { | ||
| val numSplits = metadata.numSplits(featureIndex) | ||
|
|
@@ -1009,10 +1009,24 @@ private[spark] object RandomForest extends Logging { | |
| // sort distinct values | ||
| val valueCounts = valueCountMap.toSeq.sortBy(_._1).toArray | ||
|
|
||
| // if possible splits is not enough or just enough, just return all possible splits | ||
| def weightedMean(pre: (Double, Int), cur: (Double, Int)): Double = { | ||
| val (preValue, preCount) = pre | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth factoring a method for this? you could just write
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we should get rid of this method.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed. |
||
| val (curValue, curCount) = cur | ||
| (preValue * preCount + curValue * curCount) / (preCount.toDouble + curCount) | ||
| } | ||
|
|
||
| val possibleSplits = valueCounts.length - 1 | ||
| if (possibleSplits <= numSplits) { | ||
| valueCounts.map(_._1).init | ||
| if (possibleSplits == 0) { | ||
| // constant feature | ||
| Array.empty[Double] | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this line
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed. |
||
| } else if (possibleSplits <= numSplits) { | ||
| // if possible splits is not enough or just enough, just return all possible splits | ||
| valueCounts | ||
| .sliding(2) | ||
| .map(x => weightedMean(x(0), x(1))) | ||
| .toArray | ||
|
|
||
| } else { | ||
| // stride between splits | ||
| val stride: Double = numSamples.toDouble / (numSplits + 1) | ||
|
|
@@ -1037,7 +1051,10 @@ private[spark] object RandomForest extends Logging { | |
| // makes the gap between currentCount and targetCount smaller, | ||
| // previous value is a split threshold. | ||
| if (previousGap < currentGap) { | ||
| splitsBuilder += valueCounts(index - 1)._1 | ||
| val pre = valueCounts(index - 1) | ||
| val cur = valueCounts(index) | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this line
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed |
||
| splitsBuilder += weightedMean(pre, cur) | ||
| targetCount += stride | ||
| } | ||
| index += 1 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,6 +104,18 @@ class RandomForestSuite extends SparkFunSuite with MLlibTestSparkContext { | |
| assert(splits.distinct.length === splits.length) | ||
| } | ||
|
|
||
| // SPARK-16957: Use weighted midpoints for split values. | ||
| { | ||
| val fakeMetadata = new DecisionTreeMetadata(1, 0, 0, 0, | ||
| Map(), Set(), | ||
| Array(2), Gini, QuantileStrategy.Sort, | ||
| 0, 0, 0.0, 0, 0 | ||
| ) | ||
| val featureSamples = Array(0, 1, 0, 0, 1, 0, 1, 1).map(_.toDouble) | ||
| val splits = RandomForest.findSplitsForContinuousFeature(featureSamples, fakeMetadata, 0) | ||
| assert(splits === Array(0.5)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this block, would you mind adding another test that exercises the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add new case. |
||
| } | ||
|
|
||
| // find splits should not return identical splits | ||
| // when there are not enough split candidates, reduce the number of splits in metadata | ||
| { | ||
|
|
@@ -112,9 +124,9 @@ class RandomForestSuite extends SparkFunSuite with MLlibTestSparkContext { | |
| Array(5), Gini, QuantileStrategy.Sort, | ||
| 0, 0, 0.0, 0, 0 | ||
| ) | ||
| val featureSamples = Array(1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3).map(_.toDouble) | ||
| val featureSamples = Array(1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3).map(_.toDouble) | ||
| val splits = RandomForest.findSplitsForContinuousFeature(featureSamples, fakeMetadata, 0) | ||
| assert(splits === Array(1.0, 2.0)) | ||
| assert(splits === Array(1.8, 2.2)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's clearer IMO to do:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
| // check returned splits are distinct | ||
| assert(splits.distinct.length === splits.length) | ||
| } | ||
|
|
@@ -126,9 +138,10 @@ class RandomForestSuite extends SparkFunSuite with MLlibTestSparkContext { | |
| Array(3), Gini, QuantileStrategy.Sort, | ||
| 0, 0, 0.0, 0, 0 | ||
| ) | ||
| val featureSamples = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5).map(_.toDouble) | ||
| val featureSamples = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5) | ||
| .map(_.toDouble) | ||
| val splits = RandomForest.findSplitsForContinuousFeature(featureSamples, fakeMetadata, 0) | ||
| assert(splits === Array(2.0, 3.0)) | ||
| assert(splits === Array(2.0625, 3.5)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
| } | ||
|
|
||
| // find splits when most samples close to the maximum | ||
|
|
@@ -138,9 +151,9 @@ class RandomForestSuite extends SparkFunSuite with MLlibTestSparkContext { | |
| Array(2), Gini, QuantileStrategy.Sort, | ||
| 0, 0, 0.0, 0, 0 | ||
| ) | ||
| val featureSamples = Array(0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2).map(_.toDouble) | ||
| val featureSamples = Array(0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2).map(_.toDouble) | ||
| val splits = RandomForest.findSplitsForContinuousFeature(featureSamples, fakeMetadata, 0) | ||
| assert(splits === Array(1.0)) | ||
| assert(splits === Array(1.9375)) | ||
| } | ||
|
|
||
| // find splits for constant feature | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code block is too long and has 4 exits. Emphasizing its type perhaps is better to be understand, though
splitsis implied by return type.