Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,22 @@ class RandomForestRegressionModel private[ml] (
if ($(predictionCol).nonEmpty || $(leafCol).nonEmpty) {
var predColNames = Seq.empty[String]
var predCols = Seq.empty[Column]
val bcModel = dataset.sparkSession.sparkContext.broadcast(this)
val bcRootNodes = dataset.sparkSession.sparkContext.broadcast(_trees.map(_.rootNode))

if ($(predictionCol).nonEmpty) {
val predUDF = udf { features: Vector => bcModel.value.predict(features) }
val predUDF = udf { features: Vector =>
val rootNodes = bcRootNodes.value
TreeEnsembleModel.predictRaw(features, rootNodes) / rootNodes.length
}
predColNames :+= $(predictionCol)
predCols :+= predUDF(col($(featuresCol)))
.as($(predictionCol), outputSchema($(predictionCol)).metadata)
}

if ($(leafCol).nonEmpty) {
val leafUDF = udf { features: Vector => bcModel.value.predictLeaf(features) }
val leafUDF = udf { features: Vector =>
TreeEnsembleModel.predictLeaf(features, bcRootNodes.value)
}
predColNames :+= $(leafCol)
predCols :+= leafUDF(col($(featuresCol)))
.as($(leafCol), outputSchema($(leafCol)).metadata)
Expand Down
10 changes: 10 additions & 0 deletions mllib/src/main/scala/org/apache/spark/ml/tree/treeModels.scala
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ private[ml] object TreeEnsembleModel {
prediction
}

private[ml] def predictRaw(features: Vector, rootNodes: Array[Node]): Double = {
var prediction = 0.0
var i = 0
while (i < rootNodes.length) {
prediction += rootNodes(i).predictImpl(features).prediction
i += 1
}
prediction
}

private[ml] def predictLeaf(features: Vector, rootNodes: Array[Node]): Vector = {
val indices = Array.ofDim[Double](rootNodes.length)
var i = 0
Expand Down