问题是关于 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时,为什么类型不同?我该怎么办?感谢
答案 0 :(得分:2)
当isInstanceOf返回true时,为什么类型不同?
嗯,Scala是一种静态类型语言,stages
是Array[Transformer]
,因此您访问的每个元素都是Transformer
。 Transformers
通常没有coefficients
,因此错误。
我该怎么办?
具体说明类型。
import org.apache.spark.ml.classification.LogisticRegressionModel
model.stages.collect {
case lr: LogisticRegressionModel => lr.coefficients
}.headOption