kmeans
A C++ library for k-means
Loading...
Searching...
No Matches
compute_wcss.hpp
Go to the documentation of this file.
1#ifndef KMEANS_COMPUTE_WCSS_HPP
2#define KMEANS_COMPUTE_WCSS_HPP
3
4#include <algorithm>
5#include "SimpleMatrix.hpp"
6
12namespace kmeans {
13
29template<class Matrix_, typename Cluster_, typename Float_>
30void compute_wcss(const Matrix_& data, Cluster_ ncenters, const Float_* centers, const Cluster_* clusters, Float_* wcss) {
31 auto nobs = data.num_observations();
32 auto ndim = data.num_dimensions();
33 size_t long_ndim = ndim;
34 std::fill_n(wcss, ncenters, 0);
35
36 auto work = data.create_workspace(static_cast<decltype(nobs)>(0), nobs);
37 for (decltype(nobs) obs = 0; obs < nobs; ++obs) {
38 auto cen = clusters[obs];
39 auto curcenter = centers + static_cast<size_t>(cen) * long_ndim;
40 auto& curwcss = wcss[cen];
41
42 auto curdata = data.get_observation(work);
43 for (decltype(ndim) dim = 0; dim < ndim; ++dim, ++curcenter, ++curdata) {
44 Float_ delta = static_cast<Float_>(*curdata) - *curcenter; // cast for consistent precision regardless of Data_.
45 curwcss += delta * delta;
46 }
47 }
48}
49
50}
51
52#endif
Wrapper for a simple dense matrix.
Namespace for k-means clustering.
Definition compute_wcss.hpp:12
void compute_wcss(const Matrix_ &data, Cluster_ ncenters, const Float_ *centers, const Cluster_ *clusters, Float_ *wcss)
Definition compute_wcss.hpp:30