Created
August 30, 2017 07:28
-
-
Save MLnick/4b530c3363190999f3691853988adba8 to your computer and use it in GitHub Desktop.
Ensemble pipeline component in Spark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ensemble(val uid: String, models: Seq[RegressionModel[_, _]]) extends Model[RegressionModel[_, _]] { | |
import org.apache.spark.sql.functions._ | |
def this(models: Seq[Model[_]]) = this(Identifiable.randomUID("ensemble"), models) | |
override def copy(extra: ParamMap) = ??? | |
override def transform( | |
dataset: Dataset[_]): DataFrame = { | |
val predCols = models.map { m => | |
val preds = m.transform(dataset) | |
preds.col(m.getPredictionCol) | |
} | |
def avgPreds = udf { row: Row => | |
val preds = row.toSeq.map(_.toString.toDouble) | |
val size = preds.length | |
preds.foldLeft(0.0)(_ + _) / size | |
} | |
dataset.withColumn("ensemble", avgPreds(predCols: _*)) | |
} | |
override def transformSchema( | |
schema: StructType): StructType = ??? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment