如何从 CrossValidatorModel 中提取最佳参数 [英] How to extract best parameters from a CrossValidatorModel
问题描述
我想在Spark 1.4.x 的CrossValidator 中找到最佳模型的ParamGridBuilder
参数,
I want to find the parameters of ParamGridBuilder
that make the best model in CrossValidator in Spark 1.4.x,
在 管道示例 在 Spark 文档中,他们通过在管道中使用 ParamGridBuilder
添加不同的参数(numFeatures
、regParam
).然后通过以下代码行,他们制作了最佳模型:
In Pipeline Example in Spark documentation, they add different parameters (numFeatures
, regParam
) by using ParamGridBuilder
in the Pipeline. Then by the following line of code they make the best model:
val cvModel = crossval.fit(training.toDF)
现在,我想知道从 ParamGridBuilder
生成最佳模型的参数(numFeatures
、regParam
)是什么.
Now, I want to know what are the parameters (numFeatures
, regParam
) from ParamGridBuilder
that produces the best model.
我已经使用了以下命令但没有成功:
I already used the following commands without success:
cvModel.bestModel.extractParamMap().toString()
cvModel.params.toList.mkString("(", ",", ")")
cvModel.estimatorParamMaps.toString()
cvModel.explainParams()
cvModel.getEstimatorParamMaps.mkString("(", ",", ")")
cvModel.toString()
有什么帮助吗?
提前致谢,
推荐答案
获得正确 ParamMap
对象的一种方法是使用 CrossValidatorModel.avgMetrics: Array[Double]
找到 argmax ParamMap
:
One method to get a proper ParamMap
object is to use CrossValidatorModel.avgMetrics: Array[Double]
to find the argmax ParamMap
:
implicit class BestParamMapCrossValidatorModel(cvModel: CrossValidatorModel) {
def bestEstimatorParamMap: ParamMap = {
cvModel.getEstimatorParamMaps
.zip(cvModel.avgMetrics)
.maxBy(_._2)
._1
}
}
在您引用的流水线示例中训练的 CrossValidatorModel
上运行时:
When run on the CrossValidatorModel
trained in the Pipeline Example you cited gives:
scala> println(cvModel.bestEstimatorParamMap)
{
hashingTF_2b0b8ccaeeec-numFeatures: 100,
logreg_950a13184247-regParam: 0.1
}
这篇关于如何从 CrossValidatorModel 中提取最佳参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!