scala – 如何在Spark中将RowMatrix更改为Array或将其导出为CSV
发布时间:2020-12-16 09:59:58 所属栏目:安全 来源:网络整理
导读:我在 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 =
我在
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 如何将U更改为uArray或类似类型,可以打印到CSV文件中? 解决方法
这是一个基本的操作,考虑到U是一个RowMatrix,你需要做的就是:
val U = svd.U
您只需要在RowMatrix上应用行并映射RDD [Vector]以创建一个阵列,您可以将其连接成一个创建RDD [String]的字符串. val rdd = U.rows.map( x => x.toArray.mkString(",")) 现在你需要做的就是保存RDD了: rdd.saveAsTextFile(path) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |