[英]Extract K-means cluster information Apache Spark into JSON
KMeansModel.clusterCenters
只是一個本地數據結構(Scala/Java 中的Array[Vector]
,Python 中的 NumPy 數組列表)。 這意味着您可以使用標准的 JSON 處理庫。 以 Python 為例:
import json
json.dumps({"centers": [list(x) for x in clusters.clusterCenters]})
如果 JSONL 可以接受,您還可以並行化中心並使用DataFrameWriter.write.json
。
val clusters: org.apache.spark.mllib.clustering.KMeansModel = ???
sc.parallelize(clusters.clusterCenters.zipWithIndex, 1)
.toDF("center", "id")
.write.json("centers.jsonl")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.