【问题标题】:Array within an array of Vectors.dense in SparkSpark 中 Vectors.dense 数组中的数组
【发布时间】:2019-05-01 06:01:30
【问题描述】:

我试图在 Vectors.dense 函数中将 featureD 添加为 Double 数组,但出现此错误:

templates/scala-parallel-classification/reading-custom-properties/src/main/scala/DataSource.scala:58:21: overloaded method value dense with alternatives:
[INFO] [Engine$] [error]   (values: Array[Double])org.apache.spark.mllib.linalg.Vector <and>
[INFO] [Engine$] [error]   (firstValue: Double,otherValues: Double*)org.apache.spark.mllib.linalg.Vector
[INFO] [Engine$] [error]  cannot be applied to (Array[Any])
[INFO] [Engine$] [error]             Vectors.dense(Array(

这是我的代码:

required = Some(List( // MODIFIED
    "featureA", "featureB", "featureC", "featureD", "label")))(sc)
  // aggregateProperties() returns RDD pair of
  // entity ID and its aggregated properties
  .map { case (entityId, properties) =>
    try {
      // MODIFIED
      LabeledPoint(properties.get[Double]("label"),
        Vectors.dense(Array(
          properties.get[Double]("featureA"),
          properties.get[Double]("featureB"),
          properties.get[Double]("featureC"),
          properties.get[Array[Double]]("featureD")
        ))
      )
    } catch {
      case e: Exception => {
        logger.error(s"Failed to get properties ${properties} of" +
          s" ${entityId}. Exception: ${e}.")
        throw e
      }
    }

如何在Vectors.dense 函数的数组中传递数组?

【问题讨论】:

    标签: scala apache-spark apache-spark-mllib predictionio


    【解决方案1】:

    Vectors.dense 将只接受单个 Array[Double] 或作为单独的参数加倍。数组中不可能有数组。由于数组具有混合类型,您会收到错误消息:

    不能应用于 (Array[Any])

    要解决这个问题,解决方案是简单地使用第二个数组扩展该数组,而不是将其添加为单个元素。在这种情况下,将LabeledPoint的创建更改为:

    LabeledPoint(properties.get[Double]("label"),
      Vectors.dense(
        Array(
          properties.get[Double]("featureA"),
          properties.get[Double]("featureB"),
          properties.get[Double]("featureC")
        ) ++ properties.get[Array[Double]]("featureD")
      )
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-31
      • 2020-01-04
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多