K-means is a centroid-based algorithm, or a distance-based algorithm, where we calculate the distances to assign a point to a cluster. In K-means, each cluster is associated with a centroid. The main objective of the K-Means algorithm is to minimize the sum of distances between the points and their respective cluster centroid. Here is an example of how this algorithm works:

K-means algorithm result

To use this work on your researches or projects you need:

  • Python 3.7.0
  • Python packages:
    • matplotlib
    • numpy
    • scikit_learn

To install Python:

First, check if you already have it installed or not.

python3 --version

If you don’t have python 3 in your computer you can use the code below:

sudo apt-get update
sudo apt-get install python3

To install packages via pip install:

sudo pip3 install matplotlib numpy scikit_learn

If you haven’t installed pip, you can use the codes below in your terminal:

sudo apt-get update
sudo apt install python3-pip

You should check and update your pip:

pip3 install --upgrade pip