[英]Breeze argmax on Vector: could not find implicit value
There are plenty of other questions round the error message could not find implicit value
in Scala. 还有很多其他问题,导致错误消息
could not find implicit value
在Scala中could not find implicit value
。 The answers are very specific and are not applicable to this specific issue. 答案非常具体,不适用于此特定问题。
In Scala Breeze, I am trying to apply argmax
to a SparseVector[Int]
. 在Scala Breeze中,我试图将
argmax
应用于SparseVector[Int]
。 According to the documentation (and intuition), this works easily with 根据文档(和直觉),这很容易
argmax(SparseVector.zeros[Int](5))
after importing breeze.linalg._
. 导入
breeze.linalg._
。
My actual testing code looks like this: 我的实际测试代码如下所示:
import breeze.linalg.{Vector, argmax, sum}
val counts: Map[Int, Vector[Int]] = ...
counts
.filter(e => sum(e._2) > 10)
.take(100)
.map(e => (e._1, argmax(e._2)))
.foreach(println)
However, the compiler throws the following error message: 但是,编译器将引发以下错误消息:
Error:(41, 37) could not find implicit value for parameter impl: breeze.linalg.argmax.Impl[breeze.linalg.Vector[Int],VR]
.map(e => (e._1, argmax(e._2)))
^
Some more or less surprising observations: 一些或多或少令人惊讶的发现:
sum(e._2)
seems to compile fine. sum(e._2)
似乎可以编译。 DenseVector
instead of of SparseVector
internally does not change anything DenseVector
而不是SparseVector
不会更改任何内容 How could I solve this or at least narrow down the root cause. 我该如何解决这个问题,或者至少缩小根本原因的范围。
I've solved the issue by explicitly declaring the type as SparseVector
instead of Vector
: 我已经通过将类型明确声明为
SparseVector
而不是Vector
来解决了这个问题:
val counts: Map[Int, SparseVector[Int]] = ...
To my understanding, this solution is far from obvious and it remains unclear to me why argmax()
seems to be unable to handle the more generic Vector
class, but it works. 据我了解,这种解决方案远非显而易见,对我来说还不清楚为什么
argmax()
似乎无法处理更通用的Vector
类,但是它可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.