我正在使用 PySpark 2.0 进行 Kaggle 比赛。我想知道模型(RandomForest
)的行为取决于不同的参数。ParamGridBuilder()
允许为单个参数指定不同的值,然后执行(我猜)整个参数集的笛卡尔积。假设 myDataFrame
已经定义:
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)))
现在我想要几件事:
- 最佳模型的参数是什么?这篇文章部分回答了这个问题:如何从 PySpark 中的 spark.ml 中提取模型超参数?
- 所有型号的参数是什么?
- 每个模型的结果(又名召回、准确率等)是什么?我只发现
print(model.validationMetrics)
显示(似乎)包含每个模型的准确性的列表,但我无法知道要引用哪个模型。
如果我可以检索所有这些信息,我应该能够显示图形、条形图,并且可以像处理 Panda 和sklearn
.