Laure D
Laure D

Reputation: 897

get first elements of clusters and clusters ids kmeans spark

I trained a k means model with my dataset and now I would like to get a few elements from each clusters along with the cluster id

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

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

But then once I have that, I don't know how to print these elements with their clusters ids

Upvotes: 1

Views: 480

Answers (1)

If I understood you, you want to print each point with its assigned cluster id.

You could try something like this:

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

Or maybe it would be better:

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

Upvotes: 1

Related Questions