在Spark ML中将Python转换为Scala?

时间:2016-08-02 16:52:17

标签: python scala apache-spark apache-spark-ml

问题是关于 Logistic regression with spark ml (data frames)

当我想将Python代码更改为Scala

的Python:

[stage.coefficients for stage in model.stages
    if isinstance(stage, LogisticRegressionModel)]

的Scala:(改变)

   for (stage<-model.stages){
        if(stage.isInstanceOf[LogisticRegressionModel]{
            val a = Array(stage.coefficients)
    }}

我已经检查了stage.isInstanceOf[LogisticRegressionModel],它返回了True。但是,stage.coefficients有错误消息。它说"value coefficients is not a member of org.apache.spark.ml.Transformer"

我只检查舞台,它会返回

org.apache.spark.ml.Transformer= logreg 382456482

当isInstanceOf返回true时,为什么类型不同?我该怎么办?感谢

1 个答案:

答案 0 :(得分:2)

  

当isInstanceOf返回true时,为什么类型不同?

嗯,Scala是一种静态类型语言,stagesArray[Transformer],因此您访问的每个元素都是TransformerTransformers通常没有coefficients,因此错误。

  

我该怎么办?

具体说明类型。

import org.apache.spark.ml.classification.LogisticRegressionModel

model.stages.collect { 
  case lr: LogisticRegressionModel => lr.coefficients
}.headOption