使用Scala将Matrix中的Matrix转换为RowMatrix

时间:2015-05-11 14:07:45

标签: scala matrix apache-spark distributed

我真的想将我的org.apache.spark.mllib.linalg.Matrix转换为org.apache.spark.mllib.linalg.distributed.RowMatrix

我可以这样做:

val xx = X.computeGramianMatrix()  //xx is type org.apache.spark.mllib.linalg.Matrix
val xxs = xx.toString()
val xxr = xxs.split("\n").map(row => row.replace("   "," ").replace("  "," ").replace("  "," ").replace("  "," ").replace(" ",",").split(","))
val xxp = sc.parallelize(xxr)
val xxd = xxp.map(ar => Vectors.dense(ar.map(elm => elm.toDouble)))
val xxrm: RowMatrix = new RowMatrix(xxd)

但是,这真的很糟糕,而且完全是黑客攻击。有人能告诉我一个更好的方法吗?

注意我使用Spark版本1.3.0

1 个答案:

答案 0 :(得分:0)

上面代码中的小修正:我们需要使用Vectors.dense而不是new DenseVector

val vectors = rows.map(row =>  Vectors.dense(row.toArray))