I implemented the following code to calculate weighted avg with for loops, how can I be more func programming style and use map and zip?
val aggAvg = (emb: Seq[Seq[Float]], weights: Seq[Float]) => {
val embSize = emb.head.size
val len = emb.size
(0 until embSize)
.map { i =>
(0 until len).map { j =>
emb(j)(i) * weights(j)
}.sum / weights.sum
}
}
Example: Given
val emb: Seq[Seq[Float]] = Seq(Seq(1,2,3), Seq(4,5,6))
val weights: Seq[Float] = Seq(2, 8)
the output would be Seq(3.4, 4.4, 5.4) because
(1 * 2 + 4 * 8) / (2 + 8) = 3.4 and so on.