在使用mlib和DataFrame (Spark2.0)在PipelineModel中训练RandomForestRegressor后,我将保存的模型加载到我的RT环境中,以便使用该模型进行预测,每个请求都通过加载的PipelineModel进行处理和转换,但在此过程中,我必须使用spark.createdataframe将单个请求向量转换为单行DataFrame,所有这一切大约需要700ms!
相比之下,如果我使用mllib RDD RandomForestRegressor.predict(向量),则为2.5ms。有没有办法使用新的mlib来预测单个向量,而无需转换为DataFrame或执行其他操作来加快速度?
发布于 2019-12-24 12:54:41
基于数据帧的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)
}https://stackoverflow.com/questions/38720882
复制相似问题