【问题标题】:cross validation with pipe line in spark与 Spark 中的管道进行交叉验证
【发布时间】:2016-11-15 22:35:47
【问题描述】:

管道外部的交叉验证。

val naivebayes
val indexer
val pipeLine = new Pipeline().setStages(Array(indexer, naiveBayes))

val paramGrid = new ParamGridBuilder()
   .addGrid(naiveBayes.smoothing, Array(1.0, 0.1, 0.3, 0.5))
   .build()
val crossValidator = new CrossValidator().setEstimator(pipeLine)
   .setEvaluator(new MulticlassClassificationEvaluator)
   .setNumFolds(2).setEstimatorParamMaps(paramGrid)

val crossValidatorModel = crossValidator.fit(trainData)

val predictions = crossValidatorModel.transform(testData)

管道内的交叉验证

val naivebayes
val indexer

// param grid for multiple parameter
val paramGrid = new ParamGridBuilder()
   .addGrid(naiveBayes.smoothing, Array(0.35, 0.1, 0.2, 0.3, 0.5))
   .build()

// validator for naive bayes
val crossValidator = new CrossValidator().setEstimator(naiveBayes)
   .setEvaluator(new MulticlassClassificationEvaluator)
   .setNumFolds(2).setEstimatorParamMaps(paramGrid)

// pipeline to execute compound transformation
val pipeLine = new Pipeline().setStages(Array(indexer, crossValidator))

// pipeline model
val pipeLineModel = pipeLine.fit(trainData)

// transform data
val predictions = pipeLineModel.transform(testData)

所以我想知道哪种方式更好以及它的优缺点。

对于这两个函数,我得到相同的结果和准确性。甚至第二种方法也比第一种方法快一点。

【问题讨论】:

    标签: apache-spark pipeline apache-spark-mllib cross-validation apache-spark-ml


    【解决方案1】:

    根据我参加的培训 - 这应该是最佳做法:

    cv = CrossValidator(estimator=lr,..)
    pipelineModel = Pipeline(stages=[idx,assembler,cv])
    cv_model= pipelineModel.fit(train)
    

    这样,您的管道将只适合一次,而不适合每次使用 param_grid 重复运行,这使得它运行得更快。 希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2020-03-07
      • 2020-10-28
      • 2023-03-17
      • 2012-10-14
      • 2015-06-22
      • 2021-03-25
      • 2022-01-25
      • 2017-11-16
      • 2020-12-26
      相关资源
      最近更新 更多