1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
// This is mul/mbl/mbl_k_means.cxx
#include "mbl_k_means.h"
//:
// \file
#include <vcl_algorithm.h>
#include <vcl_iostream.h>
#include <vcl_cstdlib.h>
#include <vcl_cassert.h>
//: Find k cluster centres
// Uses batch k-means clustering.
// If you provide parameter partition, it will return the
// cluster index for each data sample. The number of iterations
// performed is returned.
//
// \par Initial Cluster Centres
// If centres contain the correct number of centres, they will
// be used as the initial centres, If not, and if partition is
// given, and it is the correct size, then this will be used
// to find the initial centres.
//
// \par Degenerate Cases
// If at any point the one of the centres has no data points allocated to it
// the number of centres will be reduced below k. This is most likely to
// happen if you start the function with one or more centre identical, or
// if some of the centres start off outside the convex hull of the data set.
// In particular if you let the function initialise the centres, it will
// occur if any of the first k data samples are identical.
unsigned mbl_k_means(mbl_data_wrapper<vnl_vector<double> > &data, unsigned k,
vcl_vector<vnl_vector<double> >* cluster_centres,
vcl_vector<unsigned> * partition //=0
)
{
vcl_vector<vnl_vector<double> > & centres = *cluster_centres;
vcl_vector<unsigned> * p_partition;
data.reset();
unsigned dims = data.current().size();
vcl_vector<vnl_vector<double> > sums(k, vnl_vector<double>(dims, 0.0));
vcl_vector<unsigned> nNearest(k,0);
unsigned i;
unsigned iterations =0;
assert(data.size() >= k);
bool initialise_from_clusters = false;
// set up p_partition to point to something sensible
if (partition)
{
p_partition = partition;
if (p_partition->size() != data.size())
{
p_partition->resize(data.size());
vcl_fill(p_partition->begin(), p_partition->end(), 0);
}
else initialise_from_clusters = true;
}
else
p_partition = new vcl_vector<unsigned>(int(data.size()), 0u);
// Calculate initial centres
if (centres.size() != k) // use first k data items as centres
{
centres.resize(k);
for (i=0; i<k; ++i)
{
centres[i] = data.current();
sums[i] += data.current();
nNearest[i]++;
data.next();
}
}
else if (initialise_from_clusters)
{ // calculate centres fro existing
do
{
sums[(*p_partition)[data.index()] ] += data.current();
nNearest[(*p_partition)[data.index()] ]++;
} while (data.next());
// Calculate centres
for (i=0; i<k; ++i)
centres[i] = sums[i]/nNearest[i];
data.reset();
vcl_fill(sums.begin(), sums.end(), vnl_vector<double>(dims, 0.0));
vcl_fill(nNearest.begin(), nNearest.end(), 0);
}
bool changed = true;
while (changed)
{
changed = false;
do
{
unsigned bestCentre = 0;
double bestDist = vnl_vector_ssd(centres[0], data.current());
for (i=1; i<k; ++i)
{
double dist = vnl_vector_ssd(centres[i], data.current());
if (dist < bestDist)
{
bestDist = dist;
bestCentre = i;
}
}
sums[bestCentre] += data.current();
nNearest[bestCentre] ++;
if (bestCentre != (*p_partition)[data.index()])
{
changed = true;
(*p_partition)[data.index()] = bestCentre;
}
} while (data.next());
// reduce k if any centres have no data items assigned to its cluster.
for (i=0; i<k; ++i)
{
if ( nNearest[i] == 0)
{
k--;
centres.erase(centres.begin()+i);
sums.erase(sums.begin()+i);
nNearest.erase(nNearest.begin()+i);
for (unsigned j=0; j<p_partition->size(); ++j)
{
assert ((*p_partition)[j] = i);
if ((*p_partition)[j] > i) (*p_partition)[j]--;
}
changed= true;
}
}
// Calculate new centres
for (i=0; i<k; ++i)
centres[i] = sums[i]/nNearest[i];
// and repeat
data.reset();
vcl_fill(sums.begin(), sums.end(), vnl_vector<double>(dims, 0.0));
vcl_fill(nNearest.begin(), nNearest.end(), 0);
iterations ++;
}
if (!partition)
delete p_partition;
return iterations;
}
static inline void incXbyYv(vnl_vector<double> *X, const vnl_vector<double> &Y, double v)
{
assert(X->size() == Y.size());
assert(X->size() > 0);
int i = ((int)X->size()) - 1;
double * const pX=X->data_block();
while (i >= 0)
{
pX[i] += Y[i] * v;
i--;
}
}
//: Find k cluster centres with weighted data
// Uses batch k-means clustering.
// If you provide parameter partition, it will return the
// cluster index for each data sample. The number of iterations
// performed is returned.
//
// \par Initial Cluster Centres
// If centres contain the correct number of centres, they will
// be used as the initial centres, If not, and if partition is
// given, and it is the correct size, then this will be used
// to find the initial centres.
//
// \par Degenerate Cases
// If at any point the one of the centres has no data points allocated to it
// the number of centres will be reduced below k. This is most likely to
// happen if you start the function with one or more centre identical, or
// if some of the centres start off outside the convex hull of the data set.
// In particular if you let the function initialise the centres, it will
// occur if any of the first k data samples are identical.
//
// \par
// The algorithm has been optimised
unsigned mbl_k_means_weighted(mbl_data_wrapper<vnl_vector<double> > &data, unsigned k,
const vcl_vector<double>& wts,
vcl_vector<vnl_vector<double> >* cluster_centres,
vcl_vector<unsigned> * partition //=0
)
{
vcl_vector<vnl_vector<double> > & centres = *cluster_centres;
vcl_vector<unsigned> * p_partition;
data.reset();
unsigned dims = data.current().size();
vcl_vector<vnl_vector<double> > sums(k, vnl_vector<double>(dims, 0.0));
vcl_vector<double> nNearest(k,0.0);
unsigned i;
unsigned iterations =0;
bool initialise_from_clusters = false;
assert(data.size() >= k);
assert(data.size() == wts.size());
// set up p_partition to point to something sensible
if (partition)
{
p_partition = partition;
if (p_partition->size() != data.size())
{
p_partition->resize(data.size());
vcl_fill(p_partition->begin(), p_partition->end(), 0);
}
else initialise_from_clusters = true;
}
else
p_partition = new vcl_vector<unsigned>(int(data.size()), 0u);
const vnl_vector<double> vcl_vector_double_dims_0(dims, 0.0);
// Calculate initial centres
if (centres.size() != k) // use first k non-zero weighted data items as centres
{
centres.resize(k);
for (i=0; i<k; ++i)
{
while (wts[data.index()] == 0.0) // skip zero weighted data
{
#ifdef NDEBUG
data.next();
#else
if (!data.next())
{
vcl_cerr << "ERROR: mbl_k_means_weighted, while initialising centres from data\n"
<< "Not enough non-zero-weighted data\n";
vcl_abort();
}
#endif //NDEBUG
}
centres[i] = data.current();
incXbyYv(&sums[i], data.current(), wts[data.index()]);
nNearest[i]+= wts[data.index()];
data.next();
}
}
else if (initialise_from_clusters)
{ // calculate centres fro existing
do
{
incXbyYv(&sums[(*p_partition)[data.index()] ], data.current(), wts[data.index()]);
nNearest[(*p_partition)[data.index()] ]+=wts[data.index()];
} while (data.next());
// Calculate centres
for (i=0; i<k; ++i)
centres[i] = sums[i]/nNearest[i];
data.reset();
vcl_fill(sums.begin(), sums.end(), vcl_vector_double_dims_0);
vcl_fill(nNearest.begin(), nNearest.end(), 0.0);
}
bool changed = true;
while (changed)
{
changed = false;
do
{
const double w = wts[data.index()];
if (w != 0.0)
{
unsigned bestCentre = 0;
double bestDist = vnl_vector_ssd(centres[0], data.current());
for (i=1; i<k; ++i)
{
double dist = vnl_vector_ssd(centres[i], data.current());
if (dist < bestDist)
{
bestDist = dist;
bestCentre = i;
}
}
incXbyYv(&sums[bestCentre], data.current(), w);
nNearest[bestCentre] += w;
if (bestCentre != (*p_partition)[data.index()])
{
changed = true;
(*p_partition)[data.index()] = bestCentre;
}
}
} while (data.next());
// reduce k if any centres have no data items assigned to its cluster.
for (i=0; i<k; ++i)
{
if ( nNearest[i] == 0.0)
{
k--;
centres.erase(centres.begin()+i);
sums.erase(sums.begin()+i);
nNearest.erase(nNearest.begin()+i);
for (unsigned j=0; j<p_partition->size(); ++j)
{
if (wts[j] != 0.0)
{
assert ((*p_partition)[j] != i);
if ((*p_partition)[j] > i) (*p_partition)[j]--;
}
}
}
}
// Calculate new centres
for (i=0; i<k; ++i)
centres[i] = sums[i]/nNearest[i];
// and repeat
data.reset();
vcl_fill(sums.begin(), sums.end(), vcl_vector_double_dims_0);
vcl_fill(nNearest.begin(), nNearest.end(), 0.0);
iterations ++;
}
if (!partition)
delete p_partition;
else // assign all the zero weighted samples to their nearest centres.
{
data.reset();
do
{
if (wts[data.index()] == 0.0)
{
unsigned bestCentre = 0;
double bestDist = vnl_vector_ssd(centres[0], data.current());
for (i=1; i<k; ++i)
{
double dist = vnl_vector_ssd(centres[i], data.current());
if (dist < bestDist)
{
bestDist = dist;
bestCentre = i;
}
}
(*p_partition)[data.index()] = bestCentre;
}
} while (data.next());
}
return iterations;
}
|