【问题标题】:Mlib RandomForest (Spark 2.0) predict a single vectorMlib RandomForest (Spark 2.0) 预测单个向量
【发布时间】:2016-12-07 20:01:46
【问题描述】:
在使用 mlib 和 DataFrame (Spark 2.0) 在 PipelineModel 中训练 RandomForestRegressor 之后
我将保存的模型加载到我的 RT 环境中,以便使用模型预测每个请求
通过加载的 PipelineModel 进行处理和转换,但在此过程中我必须转换
使用 spark.createdataframe 将单个请求向量发送到单行 DataFrame 所有这一切大约需要 700 毫秒!
如果我使用 mllib RDD RandomForestRegressor.predict(VECTOR),则与 2.5ms 相比。
有什么方法可以使用新的 mlib 来预测单个向量,而无需转换为 DataFrame 或做其他事情来加快速度?
【问题讨论】:
标签:
random
apache-spark
machine-learning
random-forest
【解决方案1】:
基于org.apache.spark.ml.regression.RandomForestRegressionModel 的数据框也将Vector 作为输入。我认为您不需要为每次调用都将向量转换为数据帧。
我认为你的代码应该是这样工作的。
//load the trained RF model
val rfModel = RandomForestRegressionModel.load("path")
val predictionData = //a dataframe containing a column 'feature' of type Vector
predictionData.map { row =>
Vector feature = row.getAs[Vector]("feature")
Double result = rfModel.predict(feature)
(feature, result)
}