【问题标题】:Scala <console>:24: error: could not find implicit value for evidence parameter of type breeze.storage.DefaultArrayValue[Any]Scala <console>:24: 错误: 找不到类型为微风.storage.DefaultArrayValue[Any] 的证据参数的隐式值
【发布时间】:2014-04-25 01:00:34
【问题描述】:

我正在使用 Spark 和 Scala(两者都是新手)。我有这个代码:

val indices: List[Int] = List()
val featValues: List[Double] = List()
...
val vector = SparseVector(100000, indices.toArray, featValues.toArray)

但我不断收到此错误:

<console>:24: error: could not find implicit value for evidence parameter of type breeze.storage.DefaultArrayValue[Any]
                      val vector = SparseVector(100000, indices.toArray, featValues.toArray)

我确信只有熟悉 Scala 的人可以帮助我。 SparseVector 的文档是:

http://people.apache.org/~pwendell/catalyst-docs/api/mllib/index.html#org.apache.spark.mllib.linalg.SparseVector

我搜索了谷歌并找到了这个: Could not find implicit value for evidence parameter of type scala.reflect.ClassManifest[T]

但我不知道如何解释答案。

编辑: 我实际上是从微风中导入另一个 SparseVector,而我应该从 org.apache.spark.mllib.linalg.SparseVector 导入。

我再次启动交互式shell,输入:import org.apache.spark.mllib.linalg.SparseVector

现在我在重复上面相同的代码后得到error: not found: value SparseVector

编辑2: 我应该清楚“...”暗示我正在初始化列表。

【问题讨论】:

  • 如果编译器正在寻找隐式,那么文档应该有类似def something[T: Whatever](foo: Foo)def something[T](foo: Foo)(implicit evidence: Whatever[T]) 的内容。在您链接的文档中,我没有找到与 SparseVector 类似的东西,所以我不确定问题出在哪里。据我所知,您发布的所有代码看起来都不需要隐式。

标签: scala apache-spark


【解决方案1】:

看起来你还没有初始化索引和值,所以 scala 有时会尝试使用它们的默认值。同样根据您链接的文档,您需要使用 new 创建 SparseVector(也许没有伴随对象?)

我无法测试,但我建议尝试以下方法:

 val indices = Array(1,2,3) // indices is expected to be an array of Int
 val values = Array(1.0d,2.0d,3.0d) // values is an array of doubles       
 val v2 = new SparseVector(3, indices, values)

这行得通吗?如果是这样,问题可能出在您在val featValues之后省略的代码部分

【讨论】:

  • 我在“...”下添加值。但是我添加了“新”并且它起作用了。知道为什么吗?
  • 使用SparseVector(args..)实际上是调用SparseVector伴生对象的apply方法(并且没有SparseVector伴生对象)。使用new SparseVector(args..) 调用常规类构造函数。
  • SparseVector 显然是一个常规类。就像在 java 中一样,您需要使用 new 创建它的实例。在 scala 中,您可以拥有其他可以在不(显式)使用 new 的情况下返回类的东西(对象、伴生对象、案例类...),但这里似乎没有为您定义它们。
猜你喜欢
  • 2015-05-14
  • 1970-01-01
  • 2015-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 2018-07-25
  • 2013-01-25
相关资源
最近更新 更多