There is a matrix and I want to perform the dot product of it with a vector. Following is the Scala code:
val matrix = sc.parallelize(List(
(("v1","v1"),2),(("v1","v2"),4),(("v1","v3"),1),(("v2","v2"),5),
(("v2","v3"),1),(("v3","v3"),2)))
val vector = sc.parallelize(List(("v1",4),("v2",1),("v3",5)))
val dotproduct = matrix.flatMap{x => {
vector.flatMap { y => {
if(x._1._2 == y._1) Tuple2(x._1._1, x._2 * y._2)
}}
}}.reduceByKey((_,_) => _+_)
But the following error occurred:
<console>:25: error: type mismatch;
found : (String, Int)
required: TraversableOnce[?]
val dotproduct = matrix.flatMap{ x => { vector.flatMap { y => { if(x._1._2 == y._1) (x._1._1, x._2 * y._2) }}}}.reduceByKey((_,_) => _+_)
^
I don't know if the nesting operation in RDD is OK. Does Spark MLlib provide any API to perform the dot product between matrix and vector?