如何改变RowMatrix到阵列中的Spark或将其导出为CSV? [英] How to change RowMatrix into Array in Spark or export it as a CSV?
问题描述
我这有code Scala中:
VAL垫:CoordinateMatrix =新CoordinateMatrix(数据)
VAL rowMatrix:RowMatrix = mat.toRowMatrix()VAL SVD:SingularValueDecomposition [RowMatrix,矩阵] = rowMatrix.computeSVD(100,computeU = TRUE)VAL U:RowMatrix = svd.U //将U因素是RowMatrix。
VAL小号:矢量= svd.s //奇异值被存储在一个本地密载体。
VAL五:矩阵= svd.V //在V的因素是当地的密集矩阵。VAL uArray:数组[双] = U.toArray //不起作用,因为没有在RowMatrix型功能的toArray
VAL sArray:数组[双] = S.toArray //好工作
VAL VARRAY:数组[双] = V.toArray //好工作
我怎样才能改变的 U 的成的 uArray 的或相似的类型,可以打印出到CSV文件?
这是一个基本的操作,这里是你必须做的考虑到U是RowMatrix如下内容:
VAL U = svd.U
行()是一个RowMatrix方法,它允许您从RowMatrix按行获得RDD。
块引用>你只需要在你的应用RowMatrix行和映射RDD [矢量]创建,你会连接成一个字符串创建RDD [字符串]数组。
VAL RDD = U.rows.map(X => x.toArray.mkString())
所有你必须现在就做保存RDD:
rdd.saveAsTextFile(路径)
I've got this code in Scala:
val mat: CoordinateMatrix = new CoordinateMatrix(data) val rowMatrix: RowMatrix = mat.toRowMatrix() val svd: SingularValueDecomposition[RowMatrix, Matrix] = rowMatrix.computeSVD(100, computeU = true) val U: RowMatrix = svd.U // The U factor is a RowMatrix. val S: Vector = svd.s // The singular values are stored in a local dense vector. val V: Matrix = svd.V // The V factor is a local dense matrix. val uArray: Array[Double] = U.toArray // doesn't work, because there is not toArray function in RowMatrix type val sArray: Array[Double] = S.toArray // works good val vArray: Array[Double] = V.toArray // works good
How can I change U into uArray or similar type, that could be printed out into CSV file?
解决方案That's a basic operation, here is what you have to do considering that U is a RowMatrix as following :
val U = svd.U
rows() is a RowMatrix method that allows you to get an RDD from your RowMatrix by row.
You'll just need to apply rows on your RowMatrix and map the RDD[Vector] to create an Array that you would concatenate into a string creating an RDD[String].
val rdd = U.rows.map( x => x.toArray.mkString(","))
All you'll have to do now it to save the RDD :
rdd.saveAsTextFile(path)
这篇关于如何改变RowMatrix到阵列中的Spark或将其导出为CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!