Scala在if else语句中分配值

时间:2019-09-19 16:35:14

标签: scala apache-spark apache-spark-sql apache-spark-mllib

scala的新手。

我有一个if else语句,该语句基于条件将值分配给变量。

import org.apache.spark.ml.{Pipeline, PipelineModel, PipelineStage}
modelType = "rf" 

def train(trainingData: DataFrame): PipelineModel = {

        val xgb = new XGBoostClassifier()
          .setLabelCol("label")
          .setFeaturesCol("features")
          .setObjective("binary:logistic")
          .setEvalMetric("auc")  

        val rf = new RandomForestClassifier()
          .setLabelCol("label")
          .setFeaturesCol("features")
          .setImpurity("gini")

         if (modelType == "rf") {
             val pipeline = new Pipeline()
              .setStages(Array[PipelineStage](rf))
             } else if (modelType == "xgb") {
            val pipeline = new Pipeline()
              .setStages(Array[PipelineStage](rf))
             }


        pipeline.fit(trainingData)
        }

但是我看到此错误:pipeline not found。这是否意味着未分配管道变量?我该如何重写呢?

1 个答案:

答案 0 :(得分:2)

您将if设置为变量

  val xgb = new XGBoostClassifier()
              .setLabelCol("label")
              .setFeaturesCol("features")
              .setObjective("binary:logistic")
              .setEvalMetric("auc")  

            val rf = new RandomForestClassifier()
              .setLabelCol("label")
              .setFeaturesCol("features")
              .setImpurity("gini")

           val pipeline =  if (modelType == "rf") {
                  new Pipeline()
                  .setStages(Array[PipelineStage](rf))
                 } else if (modelType == "xgb") {
                new Pipeline()
                  .setStages(Array[PipelineStage](rf))
                 }
                  }

            pipeline.fit(trainingData)
            }