pyspark: getting the best model's parameters after a gridsearch is blank {}

拈花ヽ惹草 提交于 2019-12-10 11:17:45

问题


could someone help me extract the best performing model's parameters from my grid search? It's a blank dictionary for some reason.

from pyspark.ml.tuning import ParamGridBuilder, TrainValidationSplit, CrossValidator
from pyspark.ml.evaluation import BinaryClassificationEvaluator


train, test = df.randomSplit([0.66, 0.34], seed=12345)

paramGrid = (ParamGridBuilder()
             .addGrid(lr.regParam, [0.01,0.1])
             .addGrid(lr.elasticNetParam, [1.0,])
             .addGrid(lr.maxIter, [3,])
             .build())

evaluator = BinaryClassificationEvaluator(rawPredictionCol="rawPrediction",labelCol="buy")
evaluator.setMetricName('areaUnderROC')

cv = CrossValidator(estimator=pipeline,
                          estimatorParamMaps=paramGrid,
                          evaluator=evaluator,
                          numFolds=2)  
cvModel = cv.fit(train)

> print(cvModel.bestModel) #it looks like I have a valid bestModel
PipelineModel_406e9483e92ebda90524 In [8]:

> cvModel.bestModel.extractParamMap() #fails
 {} In [9]:

> cvModel.bestModel.getRegParam() #also fails
> 
> AttributeError                            Traceback (most recent call
> last) <ipython-input-9-747196173391> in <module>()
> ----> 1 cvModel.bestModel.getRegParam()
> 
> AttributeError: 'PipelineModel' object has no attribute 'getRegParam'

回答1:


There are two different problems here:

  • Parameters are set on individual Estiamtors or Transformers not PipelineModel. All models can be accessed using stages property.
  • Before Spark 2.3 Python models don't contain Params at all (SPARK-10931).

So unless you use development branch you have to find the model of interest among branches, access its _java_obj and get parameters of interest. For example:

from pyspark.ml.classification import LogisticRegressionModel

[x._java_obj.getRegParam() 
for x in cvModel.bestModel.stages if isinstance(x, LogisticRegressionModel)]



回答2:


I encountered this problem recently, the solution that worked best for me was to create a dictionary of the key names and their values from extractParamMap and then use that to get the values I wanted by name.

best_mod = cvModel.bestModel
param_dict = best_mod.stages[-1].extractParamMap()

sane_dict = {}
for k, v in param_dict.items():
  sane_dict[k.name] = v

best_reg = sane_dict["regParam"]
best_elastic_net = sane_dict["elasticNetParam"]
best_max_iter = sane_dict["maxIter"]

hope this helps!




回答3:


Try this:

cvModel.bestModel.stages[-1].extractParamMap()

You can change -1 with any number you like.



来源:https://stackoverflow.com/questions/46110563/pyspark-getting-the-best-models-parameters-after-a-gridsearch-is-blank

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!