2 * clueless-kmeans is a variant of k-means which enforces balanced
3 * distribution of classes in every cluster
5 * Copyright (c) 2013 Idiap Research Institute, http://www.idiap.ch/
6 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
8 * This file is part of clueless-kmeans.
10 * clueless-kmeans is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 3 as published by the Free Software Foundation.
14 * clueless-kmeans is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with selector. If not, see <http://www.gnu.org/licenses/>.
24 #include "clusterer.h"
27 Clusterer::Clusterer() {
32 Clusterer::~Clusterer() {
33 deallocate_array<scalar_t>(_cluster_means);
34 deallocate_array<scalar_t>(_cluster_var);
37 scalar_t Clusterer::distance_to_centroid(scalar_t *x, int k) {
39 for(int d = 0; d < _dim; d++) {
40 dist += sq(_cluster_means[k][d] - x[d]) / (2 * _cluster_var[k][d]);
41 dist += 0.5 * log(_cluster_var[k][d]);
42 ASSERT(!isnan(dist) && !isinf(dist));
47 void Clusterer::initialize_clusters(int nb_points, scalar_t **points) {
48 int *used = new int[nb_points];
49 for(int k = 0; k < nb_points; k++) { used[k] = 0; }
50 for(int k = 0; k < _nb_clusters; k++) {
52 do { l = int(drand48() * nb_points); } while(used[l]);
53 for(int d = 0; d < _dim; d++) {
54 _cluster_means[k][d] = points[l][d];
55 _cluster_var[k][d] = 1.0;
62 scalar_t Clusterer::baseline_cluster_association(int nb_points, scalar_t **points,
63 int nb_classes, int *labels,
65 int *associated_clusters = new int[nb_points];
66 scalar_t total_dist = 0;
68 for(int n = 0; n < nb_points; n++) {
69 scalar_t lowest_dist = 0;
70 for(int k = 0; k < _nb_clusters; k++) {
71 scalar_t dist = distance_to_centroid(points[n], k);
72 if(k == 0 || dist <= lowest_dist) {
74 associated_clusters[n] = k;
78 total_dist += lowest_dist;
81 for(int n = 0; n < nb_points; n++) {
82 for(int k = 0; k < _nb_clusters; k++) {
85 gamma[n][associated_clusters[n]] = 1.0;
88 delete[] associated_clusters;
93 scalar_t Clusterer::baseline_lp_cluster_association(int nb_points, scalar_t **points,
94 int nb_classes, int *labels,
98 int *coeff_row = new int[nb_points * _nb_clusters + 1];
99 int *coeff_col = new int[nb_points * _nb_clusters + 1];
100 scalar_t *coeff_wgt = new scalar_t[nb_points * _nb_clusters + 1];
102 lp = glp_create_prob();
104 glp_set_prob_name(lp, "baseline_lp_cluster_association");
105 glp_set_obj_dir(lp, GLP_MIN);
107 glp_add_rows(lp, nb_points);
109 for(int n = 1; n <= nb_points; n++) {
110 glp_set_row_bnds(lp, n, GLP_FX, 1.0, 1.0);
113 glp_add_cols(lp, nb_points * _nb_clusters);
114 for(int k = 1; k <= _nb_clusters; k++) {
115 for(int n = 1; n <= nb_points; n++) {
116 int i = n + nb_points * (k - 1);
117 glp_set_obj_coef(lp, i, distance_to_centroid(points[n-1], k-1));
118 glp_set_col_bnds(lp, i, GLP_DB, 0.0, 1.0);
124 for(int n = 1; n <= nb_points; n++) {
125 for(int k = 1; k <= _nb_clusters; k++) {
126 coeff_row[n_coeff] = n;
127 coeff_col[n_coeff] = n + nb_points * (k - 1);
128 coeff_wgt[n_coeff] = 1.0;
133 glp_load_matrix(lp, nb_points * _nb_clusters, coeff_row, coeff_col, coeff_wgt);
135 glp_simplex(lp, NULL);
137 scalar_t total_dist = glp_get_obj_val(lp);
139 for(int k = 1; k <= _nb_clusters; k++) {
140 for(int n = 1; n <= nb_points; n++) {
141 int i = n + nb_points * (k - 1);
142 gamma[n-1][k-1] = glp_get_col_prim(lp, i);
155 scalar_t Clusterer::uninformative_lp_cluster_association(int nb_points, scalar_t **points,
156 int nb_classes, int *labels,
158 int absolute_proportion) {
161 // dist(n,k) distance of samples n to cluster k
163 // We want to optimize the
165 // \gamma(n,k) is the association of point n to cluster k
169 // \sum_{n,k} \gamma(n,k) dist(n,k)
173 // (A) \forall n, k, \gamma(n, k) >= 0
174 // (B) \forall n, \sum_k \gamma(n, k) = 1
175 // (C) \forall k, \sum_n \gamma(n, k) = N/K
179 // The coefficients for the constraints are passed to the glpk
180 // functions with a sparse representation.
182 // ** GLPK USES INDEXES STARTING AT 1, NOT 0. **
186 if(absolute_proportion) {
187 nb_coeffs = nb_points * _nb_clusters + nb_points * _nb_clusters;
189 nb_coeffs = nb_points * _nb_clusters + nb_points * nb_classes * _nb_clusters;
192 int *coeff_row = new int[nb_coeffs + 1];
193 int *coeff_col = new int[nb_coeffs + 1];
194 scalar_t *coeff_wgt = new scalar_t[nb_coeffs + 1];
198 scalar_t *nb_samples_per_class = new scalar_t[nb_classes];
199 for(int c = 0; c < nb_classes; c++) {
200 nb_samples_per_class[c] = 0.0;
203 for(int n = 0; n < nb_points; n++) {
204 nb_samples_per_class[labels[n]] += 1.0;
207 lp = glp_create_prob();
209 glp_set_prob_name(lp, "uninformative_lp_cluster_association");
210 glp_set_obj_dir(lp, GLP_MIN);
212 // We have one column per coefficient gamma
214 glp_add_cols(lp, nb_points * _nb_clusters);
216 // The column for gamma[n][k] point 1<=n<=nb_points and cluster
217 // 1<=k<=_nb_clusters is nb_points * (k - 1) + n;
219 // The constraints (A) will be expressed by putting directly bounds
220 // on the variables (i.e. one per column). So we need one row per
221 // (B) constraint, and one per (C) constraint.
223 glp_add_rows(lp, nb_points + _nb_clusters * nb_classes);
225 // First, we set the weights for the objective function, and the (A)
226 // constraints on the individual gammas
228 for(int k = 1; k <= _nb_clusters; k++) {
229 for(int n = 1; n <= nb_points; n++) {
230 int col = n + nb_points * (k - 1);
232 // The LP weight on the gammas for the global loss is the
233 // normalized distance of that sample to the centroid of that
236 glp_set_obj_coef(lp, col, distance_to_centroid(points[n-1], k-1));
238 // The (A) constraints: Each column correspond to one of the
239 // gamma, and it has to be in [0,1]
241 glp_set_col_bnds(lp, col, GLP_DB, 0.0, 1.0);
245 // The (B) constraints: for each point, the sum of its gamma is
248 for(int n = 1; n <= nb_points; n++) {
250 glp_set_row_bnds(lp, row, GLP_FX, 1.0, 1.0);
251 for(int k = 1; k <= _nb_clusters; k++) {
252 coeff_row[n_coeff] = row;
253 coeff_col[n_coeff] = nb_points * (k - 1) + n;
254 coeff_wgt[n_coeff] = 1.0;
259 // The (C) constraints: For each pair cluster/class, the sum of the
260 // gammas for this cluster and this class is equal to the number of
261 // sample of that class, divided by the number of clusters
263 if(absolute_proportion) {
264 for(int k = 1; k <= _nb_clusters; k++) {
265 for(int c = 1; c <= nb_classes; c++) {
266 int row = nb_points + (k - 1) * nb_classes + c;
267 scalar_t tau = nb_samples_per_class[c-1] / scalar_t(_nb_clusters);
268 glp_set_row_bnds(lp, row, GLP_FX, tau, tau);
269 for(int n = 1; n <= nb_points; n++) {
270 if(labels[n-1] == c - 1) {
271 coeff_row[n_coeff] = row;
272 coeff_col[n_coeff] = (k-1) * nb_points + n;
273 coeff_wgt[n_coeff] = 1.0;
280 for(int k = 1; k <= _nb_clusters; k++) {
281 for(int c = 1; c <= nb_classes; c++) {
282 int row = nb_points + (k - 1) * nb_classes + c;
283 glp_set_row_bnds(lp, row, GLP_FX, 0.0, 0.0);
284 for(int n = 1; n <= nb_points; n++) {
285 coeff_row[n_coeff] = row;
286 coeff_col[n_coeff] = (k-1) * nb_points + n;
288 (labels[n-1] == c - 1 ? 1.0 : 0.0)
289 - scalar_t(nb_samples_per_class[c-1]) / scalar_t(nb_points);
296 ASSERT(n_coeff == nb_coeffs + 1);
298 glp_load_matrix(lp, nb_coeffs, coeff_row, coeff_col, coeff_wgt);
300 // Now a miracle occurs
302 glp_simplex(lp, NULL);
304 // We retrieve the result
306 scalar_t total_dist = glp_get_obj_val(lp);
308 for(int k = 1; k <= _nb_clusters; k++) {
309 for(int n = 1; n <= nb_points; n++) {
310 int i = n + nb_points * (k - 1);
311 gamma[n-1][k-1] = glp_get_col_prim(lp, i);
315 delete[] nb_samples_per_class;
324 void Clusterer::update_clusters(int nb_points, scalar_t **points, scalar_t **gamma) {
325 for(int k = 0; k < _nb_clusters; k++) {
327 for(int d = 0; d < _dim; d++) {
328 _cluster_means[k][d] = 0.0;
329 _cluster_var[k][d] = 0.0;
332 scalar_t sum_gamma = 0;
333 for(int n = 0; n < nb_points; n++) {
334 sum_gamma += gamma[n][k];
335 for(int d = 0; d < _dim; d++) {
336 _cluster_means[k][d] += gamma[n][k] * points[n][d];
337 _cluster_var[k][d] += gamma[n][k] * sq(points[n][d]);
341 ASSERT(sum_gamma >= 1);
343 for(int d = 0; d < _dim; d++) {
346 (_cluster_var[k][d] - sq(_cluster_means[k][d]) / sum_gamma) / (sum_gamma - 1);
347 _cluster_var[k][d] = max(scalar_t(min_cluster_variance), _cluster_var[k][d]);
349 _cluster_var[k][d] = 1;
352 _cluster_means[k][d] /= sum_gamma;
357 void Clusterer::train(int mode,
358 int nb_clusters, int dim,
359 int nb_points, scalar_t **points,
360 int nb_classes, int *labels,
361 int *cluster_associations) {
362 deallocate_array<scalar_t>(_cluster_means);
363 deallocate_array<scalar_t>(_cluster_var);
364 _nb_clusters = nb_clusters;
366 _cluster_means = allocate_array<scalar_t>(_nb_clusters, _dim);
367 _cluster_var = allocate_array<scalar_t>(_nb_clusters, _dim);
369 scalar_t **gammas = allocate_array<scalar_t>(nb_points, _nb_clusters);
371 if(nb_clusters > nb_points) abort();
373 initialize_clusters(nb_points, points);
375 scalar_t pred_total_distance, total_distance = FLT_MAX;
379 pred_total_distance = total_distance;
383 case STANDARD_ASSOCIATION:
385 baseline_cluster_association(nb_points, points, nb_classes, labels, gammas);
388 case STANDARD_LP_ASSOCIATION:
390 baseline_lp_cluster_association(nb_points, points, nb_classes, labels, gammas);
393 case UNINFORMATIVE_LP_ASSOCIATION:
395 uninformative_lp_cluster_association(nb_points, points, nb_classes, labels, gammas, 0);
398 case UNINFORMATIVE_LP_ASSOCIATION_ABSOLUTE:
400 uninformative_lp_cluster_association(nb_points, points, nb_classes, labels, gammas, 1);
404 cerr << "Unknown sample-cluster association mode." << endl;
408 cout << "TRAIN " << nb_rounds << " " << total_distance << endl;
409 update_clusters(nb_points, points, gammas);
411 } while(total_distance < min_iteration_improvement * pred_total_distance &&
412 nb_rounds < max_nb_iterations);
414 if(cluster_associations) {
415 for(int n = 0; n < nb_points; n++) {
416 for(int k = 0; k < _nb_clusters; k++) {
417 if(k == 0 || gammas[n][k] > gammas[n][cluster_associations[n]]) {
418 cluster_associations[n] = k;
424 deallocate_array<scalar_t>(gammas);
427 int Clusterer::cluster(scalar_t *point) {
428 scalar_t lowest_dist = 0;
429 int associated_cluster = -1;
431 for(int k = 0; k < _nb_clusters; k++) {
434 for(int d = 0; d < _dim; d++) {
435 dist += sq(_cluster_means[k][d] - point[d]) / (2 * _cluster_var[k][d]);
436 dist += 0.5 * log(_cluster_var[k][d]);
437 ASSERT(!isnan(dist) && !isinf(dist));
440 if(k == 0 || dist <= lowest_dist) {
442 associated_cluster = k;
446 ASSERT(associated_cluster >= 0);
448 return associated_cluster;