首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用if语句将值返回给val?

如何使用if语句将值返回给val?
EN

Stack Overflow用户
提问于 2022-08-16 05:53:36
回答 3查看 44关注 0票数 0

我正在尝试将var赋值转换为val赋值。目前我的代码是

代码语言:javascript
复制
  // Numerical vectorizing for normalization
  var normNumericalColNameArray: Array[String] = Array()
  if (!continousPredictors.sameElements(Array(""))) {
    if (paramStandardize) {
      println("Apply standardization")
      stages += new VectorAssembler().setInputCols(continousPredictors).setOutputCol(NumericalFeaturesCol)
      stages += new StandardScaler()
        .setWithMean(true)
        .setWithStd(true)
        .setInputCol(NumericalFeaturesCol)
        .setOutputCol(StandardizedNumericalFeaturesCol)
      normNumericalColNameArray = Array(StandardizedNumericalFeaturesCol)
    } else {
      println("Not apply standardization")
      stages += new VectorAssembler().setInputCols(continousPredictors).setOutputCol(NumericalFeaturesCol)
      normNumericalColNameArray = Array(NumericalFeaturesCol)
    }
  }

  stages += new VectorAssembler().setInputCols(normNumericalColNameArray ++ oneHotCatColNamesArray).setOutputCol(FeaturesCol)

我想做这样的事

代码语言:javascript
复制
  val normNumericalColNameArray =
    if (continousPredictors.nonEmpty && paramStandardize) {
    println("Apply standardization")
    stages += new VectorAssembler().setInputCols(continousPredictors).setOutputCol(NumericalFeaturesCol)
    stages += new StandardScaler()
      .setWithMean(true)
      .setWithStd(true)
      .setInputCol(NumericalFeaturesCol)
      .setOutputCol(StandardizedNumericalFeaturesCol)
    Array(StandardizedNumericalFeaturesCol)
    } else if (continousPredictors.nonEmpty){
    println("Not apply standardization")
    stages += new VectorAssembler().setInputCols(continousPredictors).setOutputCol(NumericalFeaturesCol)
    Array(NumericalFeaturesCol)
    }


  stages += new VectorAssembler().setInputCols(normNumericalColNameArray ++ oneHotCatColNamesArray).setOutputCol(FeaturesCol)

我遇到了这个错误

代码语言:javascript
复制
value ++ is not a member of Any
stages += new VectorAssembler().setInputCols(normNumericalColNameArray ++ oneHotCatColNamesArray).setOutputCol(FeaturesCol)

我正在尝试将数组从if条件返回到我的Val normNumericalColNameArray中。有人能帮忙吗?

EN

回答 3

Stack Overflow用户

发布于 2022-08-16 07:08:49

问题是存在一个没有if块的else,所以结果可能是无意义的(Unit)。只需添加一个else子句:

代码语言:javascript
复制
} else if (continousPredictors.nonEmpty) {
  println("Not apply standardization")
  stages += new VectorAssembler().setInputCols(continousPredictors).setOutputCol(NumericalFeaturesCol)

  Array(NumericalFeaturesCol)
} else {
  Array()
}

但是Array是Java类型,所以如果可能的话,更倾向于使用VectorList

票数 1
EN

Stack Overflow用户

发布于 2022-08-16 10:04:33

对于if表达式,结果类型是分支的最小上限.如果没有最终的else,就有一个隐含的

代码语言:javascript
复制
else ()  // where () is the singleton value of the Unit type

因此,在没有最终elseelse中,整个表达式将具有Array[String]Array[String]Unit最不上界的类型,即Any,它将成为normNumericalColNameArray的推断类型。

正如Tim和gifa注意到的那样,您可以通过添加最后的else子句来修复这个问题。也不需要将内部if提升到外部条件检查:

代码语言:javascript
复制
val normNumericalColNameArray =
  if (continousPredictors.nonEmpty) {
    stages += new VectorAssembler().setInputCols(continousPredictors).setOutputCol(NumericalFeaturesCol)

    if (paramStandardize) {
      println("Apply standardization")
      stages += new StandardScaler()
        .setWithMean(true)
        .setWithStd(true)
        .setInputCol(NumericalFeaturesCol)
        .setOutputCol(StandardizedNumericalFeaturesCol)
      Array(StandardizedNumericalFeaturesCol)
    } else {
      println("Not apply standardization")
      Array(NumericalFeaturesCol)
    }
  } else {
    Array[String]()
  }
票数 1
EN

Stack Overflow用户

发布于 2022-08-16 06:11:09

问题在if语句中。你有:

代码语言:javascript
复制
val normNumericalColNameArray = {
  if (condition1){
   Array[String]()
  }
  else if (condition2){
   Array[String]()
  }
}

现在,变量的类型为Any,因为如果condition1不是真,而condition2不是真,那么if语句(函数)就不会返回任何内容:实际上创建了一个部分函数。尝试添加最后一个else语句,例如:

代码语言:javascript
复制
val normNumericalColNameArray = {
  if (condition1){
   Array[String]()
  }
  else if (condition2){
   Array[String]()
  }
  else{
   Array[String]()
  }
}

那就行了。此外,我建议将您的normNumericalColNameArray设置为惰性val,因此只在需要时才进行评估,因为您正在修改if语句中的stages

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73369331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档