Scala Spark 加载保存的 MLlib 模型

Scala Spark load saved MLlib model

我在我的 S3 路径中保存了一个随机森林模型,现在我想加载它。但是,我得到一个方法不存在的错误。

代码(保存模型有效):

import org.apache.spark.ml.classification.RandomForestClassifier

    val rfClassifier = new RandomForestClassifier()
      .setImpurity("gini")
      .setMaxDepth(8)
      .setNumTrees(200)
      .setFeatureSubsetStrategy("auto")
      .setSeed(18)

   val rfModel = rfClassifier.fit(trainingFeatures)
    rfModel
    .write
    .overwrite()
    .save(<MY S3 PATH>)

代码(加载模型不起作用):

val rfmodel = RandomForestClassifier.load(<MY S3 PATH>)
)

错误:

java.lang.NoSuchMethodException: org.apache.spark.ml.classification.RandomForestClassificationModel.<init>(java.lang.String)

不确定加载方法存在时为什么会出现此错误

您应该加载 RandomForestClassificationModel 而不是 RandomForestClassifier

替换为:

val rfmodel = RandomForestClassificationModel.load(<MY S3 PATH>)

有关模型持久性的更多信息here