Skip to content

dadezz/kmeans-plus-plus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kmeans++

A minimal C++20 implementation of the K-Means++ clustering algorithm with OpenMP parallelism.

What's in the box

  • kmeans_pp.h: public API
  • kmeans_pp.cpp: implementation
  • main.cpp: example usage (2D synthetic dataset, evaluation, CSV export)
  • plot.py: matplotlib script to visualize results

There are no precompiled libraries to link against. Just drop kmeans_pp.h and kmeans_pp.cpp into your project and compile them alongside your own code.

Building

Requires a C++20 compiler (or later) and OpenMP. CMake 3.8+.

cmake --preset linux-debug   # or x64-release, etc.
cmake --build out/build/linux-debug

Windows presets (x64-debug, x64-release, x86-*) use MSVC + Ninja.

Usage

The entire API is a single function:

#include "kmeans_pp.h"

// data: flat array of N points, each with D coordinates (row-major)
// Returns centroids and per-point cluster assignments.
auto result = kmeans::kmeans_pp(N, D, K, data, max_iterations, seed);

// result.centroids: std::vector<double>, size K*D
// result.assignments: std::vector<int>, size N

seed = 0 uses std::random_device for non-deterministic initialization.

Example

main.cpp loads a 2D dataset in ARFF format, runs K-Means++, evaluates accuracy against ground-truth labels via greedy cluster-to-class matching, and exports results to CSV. It's just a demo, not part of the library.

The dataset used is 2d-10c (2990 points, 10 classes).

./kmeans data.arff        # default seed=42
./kmeans data.arff 123    # custom seed

To plot:

python plot.py datasets/kmeans_result.csv

Dependencies

  • OpenMP (linked via CMake's OpenMP::OpenMP_CXX)

  • C++20 or later

  • plot.py needs pandas and matplotlib.

License

Do whatever you want with it.

About

minimal, parallel and efficient C++20 implementation of Kmeans++ clustering algorithm

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors