-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-9478][ML][PYSPARK] Add sample weights to Random Forest #27097
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 all commits
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 |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| package org.apache.spark.ml.classification | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.ml.classification.LinearSVCSuite.generateSVMInput | ||
| import org.apache.spark.ml.feature.LabeledPoint | ||
| import org.apache.spark.ml.linalg.{Vector, Vectors} | ||
| import org.apache.spark.ml.param.ParamsSuite | ||
|
|
@@ -41,6 +42,8 @@ class RandomForestClassifierSuite extends MLTest with DefaultReadWriteTest { | |
|
|
||
| private var orderedLabeledPoints50_1000: RDD[LabeledPoint] = _ | ||
| private var orderedLabeledPoints5_20: RDD[LabeledPoint] = _ | ||
| private var binaryDataset: DataFrame = _ | ||
| private val seed = 42 | ||
|
|
||
| override def beforeAll(): Unit = { | ||
| super.beforeAll() | ||
|
|
@@ -50,6 +53,7 @@ class RandomForestClassifierSuite extends MLTest with DefaultReadWriteTest { | |
| orderedLabeledPoints5_20 = | ||
| sc.parallelize(EnsembleTestHelper.generateOrderedLabeledPoints(numFeatures = 5, 20)) | ||
| .map(_.asML) | ||
| binaryDataset = generateSVMInput(0.01, Array[Double](-1.5, 1.0), 1000, seed).toDF() | ||
| } | ||
|
|
||
| ///////////////////////////////////////////////////////////////////////////// | ||
|
|
@@ -259,6 +263,37 @@ class RandomForestClassifierSuite extends MLTest with DefaultReadWriteTest { | |
| }) | ||
| } | ||
|
|
||
| test("training with sample weights") { | ||
| val df = binaryDataset | ||
| val numClasses = 2 | ||
| // (numTrees, maxDepth, subsamplingRate, fractionInTol) | ||
| val testParams = Seq( | ||
| (20, 5, 1.0, 0.96), | ||
| (20, 10, 1.0, 0.96), | ||
| (20, 10, 0.95, 0.96) | ||
| ) | ||
|
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. I guess maybe also add different impurity in testParams?
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. Maybe also test a special case numTrees = 1?
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. with numTrees==1, RF is exactly the DecisionTree, which is already tested in DecisionTreeClassifierSuite/DecisionTreeRegressorSuite.
I guess current tests maybe enough, Testsuites for DT/GBT do not test impurity.
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. The reason I suggested testing different impurities is because when calculating best split, the impurity path (both |
||
|
|
||
| for ((numTrees, maxDepth, subsamplingRate, tol) <- testParams) { | ||
| val estimator = new RandomForestClassifier() | ||
| .setNumTrees(numTrees) | ||
| .setMaxDepth(maxDepth) | ||
| .setSubsamplingRate(subsamplingRate) | ||
| .setSeed(seed) | ||
| .setMinWeightFractionPerNode(0.049) | ||
|
|
||
| MLTestingUtils.testArbitrarilyScaledWeights[RandomForestClassificationModel, | ||
| RandomForestClassifier](df.as[LabeledPoint], estimator, | ||
| MLTestingUtils.modelPredictionEquals(df, _ == _, tol)) | ||
| MLTestingUtils.testOutliersWithSmallWeights[RandomForestClassificationModel, | ||
| RandomForestClassifier](df.as[LabeledPoint], estimator, | ||
| numClasses, MLTestingUtils.modelPredictionEquals(df, _ == _, tol), | ||
| outlierRatio = 2) | ||
| MLTestingUtils.testOversamplingVsWeighting[RandomForestClassificationModel, | ||
| RandomForestClassifier](df.as[LabeledPoint], estimator, | ||
| MLTestingUtils.modelPredictionEquals(df, _ == _, tol), seed) | ||
| } | ||
| } | ||
|
|
||
| ///////////////////////////////////////////////////////////////////////////// | ||
| // Tests of model save/load | ||
| ///////////////////////////////////////////////////////////////////////////// | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.