Implementation of k-means clustering algorithm in C (standard C89/C90)
The k-means algorithm is an iterative data clustering algorithm developed by Stuart Lloyd of Bell Labs in the 1950s as a technique for pulse-code modulation.
The main idea of the algorithm is that at each iteration, based on the existing partitioning, the cluster centers are recalculated, then the instances are divided into clusters according to which of the new centers turned out to be closer to a specific instance according to a pre-selected metric.
-
$X=\mathrm{x}_{i=1,j=1}^{n,m}$ — description of instances, where n is number of instances, m is number of features; -
$k \in \left \{1, \ldots, n \right \}$ — number of clusters.
-
$Y = \left\{ y_i|y_i\in\left\{0,\ldots,k-1\right\}, i = \overline{\left(1,n\right)}\right\}$ — cluster labels.
- Low algorithmic complexity;
- Easy to implement;
- The possibility for effective parallelization;
- The presence of many modifications.
- Sensitivity to initial cluster centers;
- Algorithm k-means poorly separates closely spaced clusters with a complex structure;
- The need for preliminary determination of the number of clusters.
Step 1. Data preparing (autoscaling):
Step 2. Set initial cluster centers:
Step 3. Calculate the initial partition:
Step 4. Calculate new cluster centers:
Step 5. Calculate a new split:
Step 6. Repeat steps 4, 5 until the split changes.
A visualisation of the first four iterations of the algorithm is shown in figure 1.
Figure 1 — Processing the Old Faithful Geyser dataset using the k-means algorithm
Cloning project and changing current directory:
git clone https://github.com/KlimentLagrangiewicz/k-means-in-C
cd k-means-in-C
Building from source (Linux):
make
Building from source (Windows):
make windows
If building was ok, you can find executable file in bin
subdirectory.
Run the program:
./bin/k-means-in-C ./datasets/iris/data.txt 150 4 3 ./datasets/iris/new_result.txt ./datasets/iris/res.txt