【发布时间】:2015-08-01 05:08:06
【问题描述】:
我正在学习如何使用 spark mllib 计算两个矩阵的乘积。现在我的代码是这样的:
val rdd1=sc.textFile("rdd1").map(line=>line.split("\t").map(_.toDouble)).zipWithIndex().map{case(v,i)=>(i,v)}.map(x=>IndexedRow(x._1,Vectors.dense(x._2)))
val rdd2=sc.textFile("rdd2").map(line=>line.split("\t").map(_.toDouble)).zipWithIndex().map{case(v,i)=>(i,v)}.map(x=>IndexedRow(x._1,Vectors.dense(x._2)))
val matrix1=new IndexedRowMatrix(test1)
val matrix2=new IndexedRowMatrix(test2)
我想要 matrix1 与 matrix2 相乘,我试过了:
matrix1.multiply(matrix2)
但是matrix2必须是局部矩阵,不能是IndexedRowMatrix(API文档中说)
def multiply(B: Matrix): IndexedRowMatrix
Multiply this matrix by a local matrix on the right.
B:a local matrix whose number of rows must match the number of columns of this matrix
returns:an IndexedRowMatrix representing the product, which preserves partitioning
还有其他方法可以做到这一点吗?
【问题讨论】:
-
为什么要创建 IndexedRowMatrix?出于什么目的?为什么不直接创建一个矩阵?
标签: apache-spark rdd apache-spark-mllib