【问题标题】:Converting Python to Scala in Spark ML?在 Spark ML 中将 Python 转换为 Scala?
【发布时间】:2016-08-02 16:52:16
【问题描述】:

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

当我想将代码 Python 更改为 Scala 时

Python:

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

斯卡拉:(改变)

   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时类型不同?我该怎么办?谢谢

【问题讨论】:

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


    【解决方案1】:

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

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

    我该怎么办?

    具体说明类型。

    import org.apache.spark.ml.classification.LogisticRegressionModel
    
    model.stages.collect { 
      case lr: LogisticRegressionModel => lr.coefficients
    }.headOption
    

    【讨论】:

    • 谢谢。我想再问一个问题。它返回一些。如何将 Some 转换为 Array?
    • 它返回一个Option[o.a.s.mllib.linalg.Vector]。如果您确定存在所需的阶段,您可以简单地使用head(而不是headOption)和o.a.s.mllib.linalg.Vector 具有toArray 方法。
    猜你喜欢
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2015-12-05
    • 2021-07-06
    • 2018-12-05
    相关资源
    最近更新 更多