Python 如何从pyspark中的MLP管道模型中获取最佳超参数?
我正在使用pyspark.ml.classification中的MLP分类器。我使用交叉验证将我的MLP模型拟合到数据集,即:参数网格方法。我正在使用ParamGrid方法迭代几个超参数。之后,我使用交叉验证课程进行培训,以获得最佳超参数。经过培训,当我试图从crossvalidation对象访问最佳超参数时,我遇到了一个错误。谁能告诉我如何获得最佳超参数Python 如何从pyspark中的MLP管道模型中获取最佳超参数?,python,pyspark,apache-spark-ml,Python,Pyspark,Apache Spark Ml,我正在使用pyspark.ml.classification中的MLP分类器。我使用交叉验证将我的MLP模型拟合到数据集,即:参数网格方法。我正在使用ParamGrid方法迭代几个超参数。之后,我使用交叉验证课程进行培训,以获得最佳超参数。经过培训,当我试图从crossvalidation对象访问最佳超参数时,我遇到了一个错误。谁能告诉我如何获得最佳超参数 from pyspark.ml.classification import MultilayerPerceptronClassifier l
from pyspark.ml.classification import MultilayerPerceptronClassifier
layers = [4, 5, 4, 3]
clf = MultilayerPerceptronClassifier(labelCol='label',layers=layers)
pipeline = Pipeline(stages=[clf])
x1 = 'stepSize'
x2 = 'maxIter'
paramGrid = ParamGridBuilder() \
.addGrid(getattr(clf,x1), [0.1, 0.2]) \
.addGrid(getattr(clf,x2),[5,10])\
.build()
evaluator = MulticlassClassificationEvaluator(labelCol='label',
predictionCol='prediction', metricName='f1')
crossval = CrossValidator(estimator=pipeline,
estimatorParamMaps=paramGrid,
evaluator=evaluator,
numFolds=2)
cvModel = crossval.fit(train_data)
cvModel.bestModel.stages[0]._java_obj.getMaxIter()
错误:
Py4JError: An error occurred while calling o1127.getMaxIter. Trace:
py4j.Py4JException: Method getMaxIter([]) does not exist
at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326)
at py4j.Gateway.invoke(Gateway.java:274)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:238)
at java.lang.Thread.run(Thread.java:748)
当我使用逻辑回归或随机森林分类器时,这个cvModel.bestModel.stages[0]。\u java\u obj.getMaxIter()正在工作。我只有在使用MLP分类器时才得到错误。当我们使用MLP分类器时,有什么方法可以获得最佳的超参数吗?我在运行完全相同的代码时遇到了相同的错误,下面的帖子为我解决了这个问题 因此,您缺少的部分是“parent()”调用,您需要“parent()”调用。希望这有帮助
modelOnly.bestModel.stages[-1]._java_obj.parent().getRegParam()