【问题标题】:How to convert ML model to MLlib model?如何将 ML 模型转换为 MLlib 模型?
【发布时间】:2017-08-28 21:34:48
【问题描述】:

我使用基于 Spark RDD 的 API(mllib 包)1.5.2 训练了机器学习模型,比如“Mymodel123”,

org.apache.spark.mllib.tree.model.RandomForestModel Mymodel123 = ....;
Mymodel123.save("sparkcontext","path");

现在我正在使用基于 Spark 数据集的 API(ml 包)2.2.0。有没有办法使用基于数据集的 API 加载模型(Mymodel123)?

org.apache.spark.ml.classification.RandomForestClassificationModel newModel =  org.apache.spark.ml.classification.RandomForestClassificationModel.load("sparkcontext","path");

【问题讨论】:

    标签: apache-spark spark-dataframe apache-spark-mllib


    【解决方案1】:

    没有公共 API 可以做到这一点,但是你 RandomForestModels 包装旧的 mllib API 和 provide private methods 可用于将 mllib 模型转换为 ml 模型:

    /** Convert a model from the old API */
    private[ml] def fromOld(
        oldModel: OldRandomForestModel,
        parent: RandomForestClassifier,
        categoricalFeatures: Map[Int, Int],
        numClasses: Int,
        numFeatures: Int = -1): RandomForestClassificationModel = {
      require(oldModel.algo == OldAlgo.Classification, "Cannot convert RandomForestModel" +
        s" with algo=${oldModel.algo} (old API) to RandomForestClassificationModel (new API).")
      val newTrees = oldModel.trees.map { tree =>
        // parent for each tree is null since there is no good way to set this.
        DecisionTreeClassificationModel.fromOld(tree, null, categoricalFeatures)
      }
      val uid = if (parent != null) parent.uid else Identifiable.randomUID("rfc")
      new RandomForestClassificationModel(uid, newTrees, numFeatures, numClasses)
    }
    

    所以这不是不可能的。在 Java 中你可以直接使用它(Java 不尊重包私有修饰符),在 Scala 中你必须将适配器代码放在 org.apache.spark.ml 包中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-03
      • 2019-05-26
      • 2021-04-24
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-25
      相关资源
      最近更新 更多