使用Scala将Array [DenseVector]转换为CSV [英] Convert Array[DenseVector] to CSV with Scala

查看:468
本文介绍了使用Scala将Array [DenseVector]转换为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Scala中使用了Kmeans Spark函数,我需要将获得的群集中心保存到CSV中.该值的类型为:Array[DenseVector].

I am using Kmeans Spark function with Scala and I need to save the Cluster Centers obtained into a CSV. This val is type: Array[DenseVector].

val clusters = KMeans.train(parsedData, numClusters, numIterations)
val centers = clusters.clusterCenters

我试图将centers转换为RDD文件,然后从RDD转换为DF,但是遇到了很多问题(例如,导入spark.implicits._/SQLContext.implicits._无法正常工作,我无法使用.toDF).我想知道是否还有另一种方法可以简化CSV.

I was trying converting centers to a RDD file and then from RDD to DF, but I get a lot of problems (e.g, import spark.implicits._ / SQLContext.implicits._ is not working and I cannot use .toDF). I was wondering if there is another way to make a CSV easier.

有什么建议吗?

推荐答案

无需使用外部库,您只需编写Java文件即可做到这一点.

Without use of external libraries you can do that by simply writing to the file Java way.

import java.io.{ PrintWriter, File, FileOutputStream }

...

val pw = new PrintWriter(
    new File( "KMeans_centers.csv" )
)

centers
.foreach( vec =>
        pw.write( vec.toString.drop( 1 ).dropRight( 1 ) + "\n" )
    )

pw.close()

结果文件

0.1,0.1,0.1
9.1,9.1,9.1

要删除转换后的向量周围的[],需要

dropdropRight.

drop and dropRight are needed to remove [] around the converted vector.

代码和数据取自正式的示例.

Code and data are taken from the official example.

这篇关于使用Scala将Array [DenseVector]转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆