获取集群和集群 ids kmeans spark 的第一个元素

get first elements of clusters and clusters ids kmeans spark

我用我的数据集训练了一个 k 均值模型,现在我想从每个集群中获取一些元素以及集群 ID

val clusters = KMeans.train(data, numClusters, numIterations)

val vectorsAndClusterIdx = data.map{ point =>
  val prediction = clusters.predict(point)
  (point.toString, prediction)
} 

但是一旦有了它,我就不知道如何打印这些元素及其簇 ID

如果我理解你的话,你想打印每个点及其分配的簇 ID。

您可以尝试这样的操作:

    vectorsAndClusterIdx.collect().foreach(println(_))

或者这样会更好:

    println(dataClustered.collect().mkString("\n"))