Search⌘ K

K-Means Clustering Implementation Steps: 4 to 6

Explore how to implement critical steps of K-Means clustering, including predicting cluster assignments, visualizing groupings with centroids in scatterplots, and using scree plots to determine the optimal number of clusters. Understand how these steps aid in data simplification and preparation for further analysis.

4) Predict

By using the predict function under a new variable (model_predict), you can execute the model and generate the centroid coordinates using cluster_centers_.

Python 3.5
#4) Predict
model_predict = model.predict(X)
centroids = model.cluster_centers_
print(model.cluster_centers_)

5) Visualize the output

It’s now time to plot the clusters on a scatterplot using two sets of elements. The first is the four color-coded clusters produced using the k-means model, which are stored under the variable named model_predict. The second is the cluster centroids, which are stored under the variable named centroids.

The centroids are black with a marker size (s) of 200 and an alpha of 1. Alpha can take any float number between 0 and 1.0, with 0 equal to ...