2017-08-28 3 views
0

Ich hatte ausgebildete Machine Learning Modelle Spark-RDD-basierte API (mllib Paket) 1.5.2 unter Verwendung von sagen "Mymodel123",Wie man ML-Modell in MLlib-Modell umwandelt?

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

Jetzt bin ich mit Spark-Dataset-basierter API (ml-Paket) 2.2.0. Gibt es eine Möglichkeit, die Modelle (Mymodel123) mithilfe der datensatzbasierten API zu laden?

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

Antwort

1

Es gibt keine öffentliche API, die das tun können, aber RandomForestModels Sie alte mllib API und provide private methods wickeln, die verwendet werden können mllib Modelle ml Modelle zu konvertieren:

/** 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) 
} 

so ist es nicht unmöglich, . In Java können Sie es direkt verwenden (Java berücksichtigt keine privaten Modifikatoren des Pakets), in Scala müssen Sie den Adaptercode in das Paket org.apache.spark.ml einfügen.