K-means is a clustering algorithm, so it's one of the unsupervised machine learning algorithms.
This algorithm aims to group the data into K clusters.
Firstly, we predefine a K, and choose K data points arbitrarily as the centers of K clusters.
Secondly, for each data, we calculate its distances with the K centers. Select its nearest center and distribute it to this cluster.
Thirdly, after distributing all the data points, we compute the new centers within each cluster.
Then iterate step 2 and 3 until the centers are no longer change.
In a word, K-means is to get K clusters, use means to calculate the centers, and assign each sample to its nearest center.
Start with k equals to 1, and calculate its variation which is the worst. Then try k equals to 2, and compare its variation with k=1. ... Each time we increase k, the total variation is smaller than before. Let's plot the reduction in variance per value for k, and find the largest reduction point, like an elbow. For example, this figure shows that when k=3, the variation has the hugest reduction, so we can set k equals to 3.