How to extract best parameters from a CrossValidatorModel

How to extract best parameters from a CrossValidatorModel

Add Comment


  • 3 Answer(s)

    Here  CrossValidatorModel.avgMetrics: Array[Double] is used to find the argmax ParamMap and  ParamMap  is properly got, and for finding the argmax ParamMap:

    implicit class BestParamMapCrossValidatorModel(cvModel: CrossValidatorModel) {
      def bestEstimatorParamMap: ParamMap = {
        cvModel.getEstimatorParamMaps
            .zip(cvModel.avgMetrics)
            .maxBy(_._2)
            ._1
      }
    }
    

    For instance, when running on the CrossValidatorModel trained in the Pipeline.

    scala> println(cvModel.bestEstimatorParamMap)
    {
        hashingTF_2b0b8ccaeeec-numFeatures: 100,
        logreg_950a13184247-regParam: 0.1
    }
    
    Answered on January 8, 2019.
    Add Comment

    For extracting best parameters from a CrossValidatorModel, try by using below code:

    val bestPipelineModel = cvModel.bestModel.asInstanceOf[PipelineModel]
    val stages = bestPipelineModel.stages
     
    val hashingStage = stages(1).asInstanceOf[HashingTF]
    println("numFeatures = " + hashingStage.getNumFeatures)
     
    val lrStage = stages(2).asInstanceOf[LogisticRegressionModel]
    println("regParam = " + lrStage.getRegParam)
    

    For more details check this source.

    Answered on January 8, 2019.
    Add Comment

    Here the chosen parameters is gotten by:

    println(cvModel.bestModel.getMaxIter)
    println(cvModel.bestModel.getRegParam)
    
    Answered on January 8, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.