How it works...

We read a LIBSVM file with a set of coordinates (can be interpreted as a tuple of three numbers) and then created a KMean() object, but changed the default number of clusters from 2 (out of the box) to 3 for demonstration purposes. We used the .fit() to create the model and then used model.summary.predictions.show() to display which tuple belongs to which cluster. In the last step, we printed the cost and the center of the three clusters. Conceptually, it can be thought of as having a set of 3D coordinates as data and then assigning each individual coordinate to one of the three clusters using KMeans algorithms.

KMeans is a form of unsupervised machine learning algorithm, with its root in signal processing (vector quantization) ...

Get Apache Spark 2.x Machine Learning Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.