gpt4 book ai didi

java - 如何从 Java 中的 TrainValidationSplitModel 中提取最佳参数集?

转载 作者:行者123 更新时间:2023-12-01 18:18:29 24 4
gpt4 key购买 nike

我正在使用 ParamGridBuilder 构建参数网格以进行搜索,并使用 TrainValidationSplit 来确定 Java 中的最佳模型 (RandomForestClassifier)。现在,我想知道 ParamGridBuilder 中生成最佳模型的参数(maxDepth、numTrees)是什么。

      Pipeline pipeline = new Pipeline().setStages(new PipelineStage[]{
new VectorAssembler()
.setInputCols(new String[]{"a", "b"}).setOutputCol("features"),
new RandomForestClassifier()
.setLabelCol("label")
.setFeaturesCol("features")});

ParamMap[] paramGrid = new ParamGridBuilder()
.addGrid(rf.maxDepth(), new int[]{10, 15})
.addGrid(rf.numTrees(), new int[]{5, 10})
.build();

BinaryClassificationEvaluator evaluator = new BinaryClassificationEvaluator().setLabelCol("label");

TrainValidationSplit trainValidationSplit = new TrainValidationSplit()
.setEstimator(pipeline)
.setEstimatorParamMaps(paramGrid)
.setEvaluator(evaluator)
.setTrainRatio(0.85);
TrainValidationSplitModel model = trainValidationSplit.fit(dataLog);

System.out.println("paramMap size: " + model.bestModel().paramMap().size());
System.out.println("defaultParamMap size: " + model.bestModel().defaultParamMap().size());
System.out.println("extractParamMap: " + model.bestModel().extractParamMap());
System.out.println("explainParams: " + model.bestModel().explainParams());
System.out.println("numTrees: " + model.bestModel().getParam("numTrees"))//NoSuchElementException: Param numTrees does not exist.

这些尝试没有帮助...

paramMap size: 0
defaultParamMap size: 0
extractParamMap: {

}
explainParams:

最佳答案

我找到了一种方法:

Pipeline bestModelPipeline = (Pipeline) model.bestModel().parent();
RandomForestClassifier bestRf = (RandomForestClassifier) bestModelPipeline.getStages()[1];

System.out.println("maxDepth : " + bestRf.getMaxDepth());
System.out.println("numTrees : " + bestRf.getNumTrees());
System.out.println("maxBins : " + bestRf.getMaxBins());

关于java - 如何从 Java 中的 TrainValidationSplitModel 中提取最佳参数集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60322875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com