无法使用网格搜索来检索bestModel

问题描述 投票:-1回答:1

我正在使用以下代码来最适合回归模型并出现错误:

# Creating parameter grid
params = ParamGridBuilder()

# Adding grids for two parameters
params = params.addGrid(regression.regParam, [0.01, 0.1, 1.0, 10.0]) \
               .addGrid(regression.elasticNetParam, [0.0, 0.5, 1.0])

# Building the parameter grid
params = params.build()
print('Number of models to be tested: ', len(params))

# Creating cross-validator
cv = CrossValidator(estimator=pipeline, estimatorParamMaps=params, evaluator=evaluator, numFolds=5)

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

# Get the best model from cross validation
best_model = cv.bestModel

错误是:

AttributeError                            Traceback (most recent 
call last)
<ipython-input-449-f7d43e2cf76b> in <module>
  3 
  4 # Get the best model from cross validation
 ----> 5 best_model = cv.bestModel
  6 
  7 # Look at the stages in the best model

AttributeError: 'CrossValidator' object has no attribute 'bestModel'

用于获取最佳模型参数的CrossValidator不会返回经过训练的模型!!

python machine-learning pyspark apache-spark-mllib grid-search
1个回答
0
投票
在访问bestModel属性之前,您必须先拟合并指定CV模型;从docs改编示例:
© www.soinside.com 2019 - 2024. All rights reserved.