如何在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并没有找到任何选项.任何的想法? 解决方法
它位于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的重构 更新似乎模型不可写:
所以可能没有一个简单的解决方案. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |