diff options
author | Paul Duncan <pabs@pablotron.org> | 2019-02-05 00:22:15 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2019-02-05 00:22:15 -0500 |
commit | f557d1f49a2914c6084dd18efc783395228d8ce0 (patch) | |
tree | 51111fc899f01956cbab948b87c241478576870d /src/km-init-forgy.c | |
parent | b5065ea43cb13c0b553874305b53963176c70f59 (diff) | |
download | kmeans-f557d1f49a2914c6084dd18efc783395228d8ce0.tar.bz2 kmeans-f557d1f49a2914c6084dd18efc783395228d8ce0.zip |
mv *.[hc] src/
Diffstat (limited to 'src/km-init-forgy.c')
-rw-r--r-- | src/km-init-forgy.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/km-init-forgy.c b/src/km-init-forgy.c new file mode 100644 index 0000000..5ff0c12 --- /dev/null +++ b/src/km-init-forgy.c @@ -0,0 +1,51 @@ +#include <stdbool.h> // bool +#include <string.h> // memset() +#include "util.h" +#include "km.h" + +// init a set with num_clusters clusters of shape num_floats by picking +// random initial points from the set +bool +km_init_forgy( + km_set_t * const cs, + const size_t num_clusters, + const km_set_t * const set, + km_rand_t * const rs +) { + const size_t num_floats = set->shape.num_floats, + stride = sizeof(float) * num_floats; + + // init cluster shape + const km_shape_t shape = { + .num_floats = num_floats, + .num_ints = 1, + }; + + // get random row offsets + size_t rows[num_clusters]; + if (!km_rand_get_sizes(rs, num_clusters, rows)) { + // return failure + return false; + } + + // generate random cluster centers + float floats[num_floats * num_clusters]; + for (size_t i = 0; i < num_clusters; i++) { + const size_t row_num = rows[i] % set->num_rows; + const float * const row_floats = km_set_get_row(set, row_num); + memcpy(floats + i * num_floats, row_floats, stride); + } + + // FIXME: should probably be heap-allocated + int ints[num_clusters]; + memset(ints, 0, sizeof(ints)); + + // init cluster set + if (!km_set_init(cs, &shape, num_clusters)) { + // return failure + return false; + } + + // add data, return result + return km_set_push(cs, num_clusters, floats, ints); +} |