diff options
author | Paul Duncan <pabs@pablotron.org> | 2019-02-03 15:10:44 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2019-02-03 15:10:44 -0500 |
commit | df06fe6084b1c1673348050309a3a0cb99b6634f (patch) | |
tree | bb51c86aee01ca235f7c6feb5fb08808c307e2ed /km-find.c | |
parent | 68a4d7a514578f1360559c84ac08e3a1e2719826 (diff) | |
download | kmeans-df06fe6084b1c1673348050309a3a0cb99b6634f.tar.bz2 kmeans-df06fe6084b1c1673348050309a3a0cb99b6634f.zip |
add cluster support to gen-data.rb, show centroids on generated png
Diffstat (limited to 'km-find.c')
-rw-r--r-- | km-find.c | 23 |
1 files changed, 23 insertions, 0 deletions
@@ -5,6 +5,14 @@ #define MIN_CLUSTER_DISTANCE 0.0001 +static float +get_score( + const float mean_distance, + const size_t num_empty +) { + return 1.0 / (mean_distance + num_empty); +} + typedef struct { float distance_sum, variance_sum; @@ -95,6 +103,7 @@ km_find( return false; } + float best_score = 0.0; for (size_t i = 2; i < cbs->max_clusters; i++) { for (size_t j = 0; j < cbs->num_tests; j++) { // init cluster set @@ -130,6 +139,20 @@ km_find( // emit result cbs->on_data(&result, cb_data); + // score result + float score = get_score(result.mean_distance, result.num_empty_clusters); + + if (score > best_score) { + // emit new best result + if (cbs->on_best && !cbs->on_best(score, &cs, cb_data)) { + // return failure + return false; + } + + // update best score + best_score = score; + } + // finalize cluster set if (!cbs->on_fini(&cs, cb_data)) { // return failure |