scala - Spark Scala,同时训练多个模型
问题描述
我有一个包含特征train
和test
数千个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
不牺牲时间的情况下通过将作业分配到不同节点来同时运行不同的调用?
解决方案
推荐阅读
- c++ - C++17/20 并行算法:对对象的非同步访问
- postgresql - postgres - 将列值插入为文字'null'
- javascript - 滚动时,Material VueJS组件未固定在底部页面上
- android - 我正在尝试使用“Pdf 创建库”在颤振应用程序中生成 pdf。在用其他语言生成 pdf 时会出现异常
- c# - Convert OpenImage Coordinates to DarkNet format
- java - 如何在 GridPane 中对齐 ListView 基线
- typescript - make TS 将所有源文件,包括非 TS,放入 build 目录
- tensorflow - 需要有关使用 TensorFlow 跟踪手头对象的建议
- javascript - ES6 to ES3 translation for MediaWiki (foreach() method and spreading operator)
- swift - How to init with a generic NSFetchedResultsController?