spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sethah <...@git.apache.org>
Subject [GitHub] spark pull request #13650: [SPARK-9623] [ML] Provide variance for RandomFore...
Date Mon, 20 Jun 2016 19:01:53 GMT
Github user sethah commented on a diff in the pull request:

    https://github.com/apache/spark/pull/13650#discussion_r67746985
  
    --- Diff: mllib/src/test/scala/org/apache/spark/ml/regression/RandomForestRegressorSuite.scala
---
    @@ -105,6 +108,55 @@ class RandomForestRegressorSuite extends SparkFunSuite with MLlibTestSparkContex
           }
       }
     
    +  test("Random Forest variance") {
    +    val categoricalFeatures = Map.empty[Int, Int]
    +    val df: DataFrame = TreeTests.setMetadata(
    +      orderedLabeledPoints50_1000, categoricalFeatures, 0)
    +
    +    // RF with one tree should have the same variance as that of the tree.
    +    val rf = new RandomForestRegressor()
    +      .setImpurity("variance")
    +      .setMaxDepth(30)
    +      .setNumTrees(1)
    +      .setMaxBins(10)
    +      .setFeatureSubsetStrategy("all")
    +      .setSubsamplingRate(1.0)
    +      .setSeed(123)
    +
    +    val rfModel = rf.fit(df)
    +    val rfVariances = rfModel.transform(df).select("variance").collect()
    +
    +    val dt = new DecisionTreeRegressor()
    +      .setImpurity("variance")
    +      .setMaxDepth(30)
    +      .setMaxBins(10)
    +      .setSeed(123)
    +    val dtModel = dt.fit(df)
    +    val dtVariances = dtModel.transform(df).select("variance").collect()
    +    val nSamples = dtVariances.size
    +    (0 to nSamples - 1).foreach { i =>
    +      val diff = math.abs(rfVariances(i).getDouble(0) - dtVariances(i).getDouble(0))
    +      assert(diff < 1e-6)
    +    }
    +
    +    rf.setMaxDepth(2)
    +    rf.setNumTrees(20)
    +    val rfNewModel = rf.fit(df)
    +    val results = rfNewModel.transform(df).select("features", "variance").collect()
    +    val features = col("features")
    +    val trees = rfNewModel.trees
    +    val numTrees = rfNewModel.getNumTrees
    +    results.map { case Row(features: Vector, variance: Double) =>
    +      val rootNodes = trees.map(_.rootNode.predictImpl(features))
    +      val predsquared = rootNodes.map(x => math.pow(x.prediction, 2)).sum / numTrees
    +      val variance = rootNodes.map(_.impurityStats.calculate()).sum / numTrees
    +      val predictions = rootNodes.map(_.prediction).sum / numTrees
    +      val expectedVariance = -math.pow(predictions, 2) + variance + predsquared
    +      assert(variance === expectedVariance,
    +        s"Expected variance $expectedVariance but got $variance.")
    --- End diff --
    
    I believe you are using the wrong variance here, since you re-use that variable name.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message