简体   繁体   English

Vector上的Breeze argmax:找不到隐式值

[英]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: 一些或多或少令人惊讶的发现:

  • the sum(e._2) seems to compile fine. sum(e._2)似乎可以编译。
  • using a 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.

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