简体   繁体   English

在Scala示例中使用Spark的MLlib线性回归缺少什么导入工作?

[英]What import is missing to make Spark's MLlib linear regression in Scala example work?

Using Spark v1.0-rc3 - When implementing MLlib's linear regression I get an error. 使用Spark v1.0-rc3 - 实现MLlib的线性回归时出现错误。 So eventually I tried copy/pasting from Spark's MLlib example code for linear regression in Scala and I still receive the error: 所以最终我尝试从Spark的MLlib示例代码中复制/粘贴Scala中的线性回归,但仍然收到错误:

scala> val parsedData = data.map { line => val parts = line.split(',') LabeledPoint(parts(0).toDouble, parts(1).split(' ').map(x => x.toDouble).toArray) } <console>:28: error: polymorphic expression cannot be instantiated to expected type; found : [U >: Double]Array[U] required: org.apache.spark.mllib.linalg.Vector LabeledPoint(parts(0).toDouble, parts(1).split(' ').map(x => x.toDouble).toArray)

The error states that org.apache.spark.mllib.linalg.Vector is required, but importing it does not help. 该错误指出org.apache.spark.mllib.linalg.Vector是必需的,但导入它没有帮助。 Even when trying multiple methods of casting to a Vector I get 即使尝试多种方法投射到Vector,我也会得到

<console>:19: error: type mismatch; found : scala.collection.immutable.Vector[Array[Double]]

The problem is due to changes to the later version. 问题是由于更高版本的更改。 The code that once worked in v0.91 now requires tweaking for v1.0. 曾经在v0.91中工作的代码现在需要调整v1.0。 You can find the latest docs here The solution is to add Vectors not Vector despite what the error tells you. 你可以在这里找到最新的文档解决方案是添加向量而不是向量,尽管错误告诉你。 Try: 尝试:

import org.apache.spark.mllib.regression.LinearRegressionWithSGD
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.linalg.Vectors

// Load and parse the data
val data = sc.textFile("mllib/data/ridge-data/lpsa.data")
val parsedData = data.map { line =>
  val parts = line.split(',')
  LabeledPoint(parts(0).toDouble, Vectors.dense(parts(1).split(' ').map(x => x.toDouble)))
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM