加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在scala中保存RandomForestClassifier Spark模型?

发布时间:2020-12-16 09:55:38 所属栏目:安全 来源:网络整理
导读:我使用以下代码构建了一个随机森林模型: import org.apache.spark.ml.classification.RandomForestClassificationModelimport org.apache.spark.ml.classification.RandomForestClassifierval rf = new RandomForestClassifier().setLabelCol("indexedLabel
我使用以下代码构建了一个随机森林模型:

import org.apache.spark.ml.classification.RandomForestClassificationModel
import org.apache.spark.ml.classification.RandomForestClassifier
val rf = new RandomForestClassifier().setLabelCol("indexedLabel").setFeaturesCol("features")
val labelConverter = new    IndexToString().setInputCol("prediction").setOutputCol("predictedLabel").setLabels(labelIndexer.labels)
val training = labelIndexer.transform(df)
val model = rf.fit(training)

现在我想保存模型,以便以后使用以下代码进行预测:

val predictions: DataFrame = model.transform(testData)

我查看了Spark文档here并没有找到任何选项.任何的想法?
我花了几个小时来构建模型,如果Spark破碎我将无法恢复它.

解决方法

它位于MLWriter界面中 – 可通过模型上的writer属性访问:

model.asInstanceOf[MLWritable].write.save(path)

这是界面:

abstract class MLWriter extends BaseReadWrite with Logging {

  protected var shouldOverwrite: Boolean = false

  /**
   * Saves the ML instances to the input path.
   */
  @Since("1.6.0")
  @throws[IOException]("If the input path already exists but overwrite is not enabled.")
  def save(path: String): Unit = {

这是来自早期版本的mllib / spark.ml的重构

更新似乎模型不可写:

Exception in thread “main” java.lang.UnsupportedOperationException:
Pipeline write will fail on this Pipeline because it contains a stage
which does not implement Writable. Non-Writable stage:
rfc_4e467607406f of type class
org.apache.spark.ml.classification.RandomForestClassificationModel

所以可能没有一个简单的解决方案.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读