问题
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
orTransformers
notPipelineModel
. All models can be accessed usingstages
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