PointSampler library (C++)
Loading...
Searching...
No Matches
kmeans_clustering.hpp
Go to the documentation of this file.
1/* Copyright (c) 2025 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4#pragma once
5#include "dkm.hpp" // https://github.com/genbattle/dkm
6
9
10namespace ps
11{
12
40template <typename T, size_t N>
41std::pair<std::vector<Point<T, N>>, std::vector<size_t>> kmeans_clustering(
42 const std::vector<Point<T, N>> &points,
43 size_t k_clusters,
44 bool normalize_data = true,
45 size_t max_iterations = 100)
46{
47 using DKMPoint = std::array<T, N>;
48 std::vector<DKMPoint> data;
49 data.reserve(points.size());
50
52 {
55
56 for (const auto &p : points_normalized)
57 data.push_back(p.coords);
58 }
59 else
60 {
61 for (const auto &p : points)
62 data.push_back(p.coords);
63 }
64
65 auto result = dkm::kmeans_lloyd<T>(data, k_clusters, max_iterations);
66
67 auto &centroids_dkm = std::get<0>(result);
68 auto &labels_dkm_u32 = std::get<1>(result); // uint32_t labels from DKM
69
70 // Convert centroids
71 std::vector<Point<T, N>> centroids;
72 centroids.reserve(centroids_dkm.size());
73 for (const auto &c : centroids_dkm)
74 centroids.emplace_back(c);
75
76 // Convert labels to size_t
77 std::vector<size_t> labels;
78 labels.reserve(labels_dkm_u32.size());
79 for (auto v : labels_dkm_u32)
80 labels.push_back(static_cast<size_t>(v));
81
82 return {std::move(centroids), std::move(labels)};
83}
84
85} // namespace ps
Definition dbscan_clustering.hpp:11
std::vector< Point< T, N > > random(size_t count, const std::array< std::pair< T, T >, N > &axis_ranges, std::optional< unsigned int > seed=std::nullopt)
Generates a specified number of uniformly distributed random points in N-dimensional space.
Definition random.hpp:66
void normalize_points(std::vector< Point< T, N > > &points)
Normalize the coordinates of a set of points along each axis to the range [0, 1].
Definition utils.hpp:298
std::pair< std::vector< Point< T, N > >, std::vector< size_t > > kmeans_clustering(const std::vector< Point< T, N > > &points, size_t k_clusters, bool normalize_data=true, size_t max_iterations=100)
Perform k-means clustering on a set of points.
Definition kmeans_clustering.hpp:41
A fixed-size N-dimensional point/vector class.
Definition point.hpp:39