首页 > 解决方案 > Spark Scala,同时训练多个模型

问题描述

我有一个包含特征traintest数千个customerId值的数据集。我的目标是在 Spark中同时训练一个二元xgboost分类器。customerId

我实际上是在尝试做这个帖子中的海报所要求的,但在 Scala 而不是 PySpark 中。我通过阅读了解到答案在以下文档中给出,但我不确定如何实现它。Spark 的 Job Scheduling 文档告诉我,我需要从单独的线程启动每个模型的训练。

到目前为止,我的代码看起来像这样:

// Data
val train: DataFrame = ...
val test: DataFrame = ...

// Model
val xgbClassifier: XGBoostClassifier = ...

// List of unique customerId's
val customers: List[Int] = ...

// Function for training and predicting for a given customer
def trainAndPredict(customer: Int): DataFrame = {
  val train_subset = train.filter($"customerId" === customer)
  val test_subset = test.filter($"customerId" === customer)
  ...
}

// Recursively train and predict for all customers
@tailrec
final def recTrainAndPredict(customers: List[Int], acc: DataFrame): DataFrame = customers match {
  case Nil => acc
  case x :: xs => recTrainAndPredict(xs, acc.union(trainAndPredict(x)))
}

val result = recTrainAndPredict(customers, spark.emptyDataFrame)

代码运行,但我猜它通过在不同节点上传播小数据集浪费了大量时间。我将如何在trainAndPredict不牺牲时间的情况下通过将作业分配到不同节点来同时运行不同的调用?

标签: scalaapache-sparkxgboost

解决方案


推荐阅读