在 Spark ML 中将 Python 转换为 Scala?

Converting Python to Scala in 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],return 为真。但是,stage.coefficients 有错误消息。它说 "value coefficients is not a member of org.apache.spark.ml.Transformer"

我只检查阶段,它会return

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

为什么当 isInstanceOf return 为真时类型不同?我应该怎么办?谢谢

Why the type is different when the isInstanceOf returns true?

好吧,Scala 是一种静态类型语言,stages 是一个 Array[Transformer],因此您访问的每个元素都是一个 TransformerTransformers一般没有coefficients,所以报错。

What should I do?

具体说明类型。

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

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