简体   繁体   中英

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. 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] . According to the documentation (and intuition), this works easily with

argmax(SparseVector.zeros[Int](5))

after importing 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.
  • using a DenseVector instead of of SparseVector internally does not change anything

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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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