Pyspark获取用ParamGridBuild创建的模型的所有参数

2024-05-26 16:29:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用PySpark 2.0进行一个Kaggle竞赛。我想知道一个模型(RandomForest)的行为取决于不同的参数。ParamGridBuilder()允许为单个参数指定不同的值,然后执行(我猜)整个参数集的笛卡尔积。假设我的DataFrame已经定义:

rdc = RandomForestClassifier()
pipeline = Pipeline(stages=STAGES + [rdc])
paramGrid = ParamGridBuilder().addGrid(rdc.maxDepth, [3, 10, 20])
                              .addGrid(rdc.minInfoGain, [0.01, 0.001])
                              .addGrid(rdc.numTrees, [5, 10, 20, 30])
                              .build()
evaluator = MulticlassClassificationEvaluator()
valid = TrainValidationSplit(estimator=pipeline,
                             estimatorParamMaps=paramGrid,
                             evaluator=evaluator,
                             trainRatio=0.50)
model = valid.fit(df)
result = model.bestModel.transform(df)

好的,现在我可以用手工函数检索简单的信息:

def evaluate(result):
    predictionAndLabels = result.select("prediction", "label")
    metrics = ["f1","weightedPrecision","weightedRecall","accuracy"]
    for m in metrics:
        evaluator = MulticlassClassificationEvaluator(metricName=m)
        print(str(m) + ": " + str(evaluator.evaluate(predictionAndLabels)))

现在我想要几样东西:

  • 最佳模型的参数是什么?这篇文章部分地回答了这个问题:How to extract model hyper-parameters from spark.ml in PySpark?
  • 所有模型的参数是什么?
  • 每个模型的结果(又称回忆、准确度等)是什么?我只发现print(model.validationMetrics)显示了(看起来)包含每个模型准确性的列表,但我不知道引用哪个模型。

如果我能检索到所有这些信息,我应该能够显示图表、条形图,并像处理Panda和sklearn那样工作。


Tags: 模型信息df参数modelpipelineevaluatorresult

热门问题