我正在尝试将var赋值转换为val赋值。目前我的代码是
// 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)我想做这样的事
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)我遇到了这个错误
value ++ is not a member of Any
stages += new VectorAssembler().setInputCols(normNumericalColNameArray ++ oneHotCatColNamesArray).setOutputCol(FeaturesCol)我正在尝试将数组从if条件返回到我的Val normNumericalColNameArray中。有人能帮忙吗?
发布于 2022-08-16 07:08:49
问题是存在一个没有if块的else,所以结果可能是无意义的(Unit)。只需添加一个else子句:
} else if (continousPredictors.nonEmpty) {
println("Not apply standardization")
stages += new VectorAssembler().setInputCols(continousPredictors).setOutputCol(NumericalFeaturesCol)
Array(NumericalFeaturesCol)
} else {
Array()
}但是Array是Java类型,所以如果可能的话,更倾向于使用Vector或List。
发布于 2022-08-16 10:04:33
对于if表达式,结果类型是分支的最小上限.如果没有最终的else,就有一个隐含的
else () // where () is the singleton value of the Unit type因此,在没有最终else的else中,整个表达式将具有Array[String]、Array[String]和Unit最不上界的类型,即Any,它将成为normNumericalColNameArray的推断类型。
正如Tim和gifa注意到的那样,您可以通过添加最后的else子句来修复这个问题。也不需要将内部if提升到外部条件检查:
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]()
}发布于 2022-08-16 06:11:09
问题在if语句中。你有:
val normNumericalColNameArray = {
if (condition1){
Array[String]()
}
else if (condition2){
Array[String]()
}
}现在,变量的类型为Any,因为如果condition1不是真,而condition2不是真,那么if语句(函数)就不会返回任何内容:实际上创建了一个部分函数。尝试添加最后一个else语句,例如:
val normNumericalColNameArray = {
if (condition1){
Array[String]()
}
else if (condition2){
Array[String]()
}
else{
Array[String]()
}
}那就行了。此外,我建议将您的normNumericalColNameArray设置为惰性val,因此只在需要时才进行评估,因为您正在修改if语句中的stages。
https://stackoverflow.com/questions/73369331
复制相似问题