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
|
/*********************************************************************
MLDemos: A User-Friendly visualization toolkit for machine learning
Copyright (C) 2010 Basilio Noris
Contact: mldemos@b4silio.com
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*********************************************************************/
#include "public.h"
#include "clustererKM.h"
using namespace std;
ClustererKM::~ClustererKM()
{
DEL(kmeans);
}
void ClustererKM::Train(std::vector< fvec > samples)
{
if(!samples.size()) return;
int dim = samples[0].size();
if(!bIterative)
{
DEL(kmeans);
}
bool bInit = false;
if(kmeans && kmeans->GetClusters() != nbClusters) DEL(kmeans);
if(!kmeans)
{
bInit = true;
kmeans = new KMeansCluster(nbClusters);
kmeans->AddPoints(samples);
kmeans->SetPlusPlus(kmeansPlusPlus);
kmeans->InitClusters();
}
kmeans->SetSoft(bSoft);
kmeans->SetGMM(bGmm);
kmeans->SetBeta(beta);
kmeans->SetPower(power);
kmeans->Update(bInit);
if(!bIterative)
{
int iterations = 20;
FOR(i, iterations) kmeans->Update();
}
}
fvec ClustererKM::Test( const fvec &sample)
{
fvec res;
res.resize(nbClusters,0);
if(!kmeans) return res;
kmeans->Test(sample, res);
float sum = 0;
FOR(i, res.size()) sum += res[i];
FOR(i, res.size()) res[i] /= sum;
return res;
}
fvec ClustererKM::Test( const fVec &sample)
{
fvec res;
res.resize(nbClusters,0);
if(!kmeans) return res;
kmeans->Test(sample, res);
float sum = 0;
FOR(i, res.size()) sum += res[i];
FOR(i, res.size()) res[i] /= sum;
return res;
}
void ClustererKM::SetParams(u32 clusters, int method, float beta, int power, bool kmeansPlusPlus)
{
this->nbClusters = clusters;
this->beta = beta;
this->power = power;
this->kmeansPlusPlus = kmeansPlusPlus;
switch(method)
{
case 0:
this->bSoft = false;
this->bGmm = false;
break;
case 1:
this->bSoft = true;
this->bGmm = false;
break;
case 2:
this->bSoft = false;
this->bGmm = true;
break;
}
}
const char *ClustererKM::GetInfoString()
{
char *text = new char[1024];
sprintf(text, "K-Means\n");
sprintf(text, "%sClusters: %d\n", text, nbClusters);
sprintf(text, "%sType:", text);
if(!bSoft && !bGmm) sprintf(text, "%sK-Means (plusplus: %i)\n", text, kmeansPlusPlus);
else if(bSoft) sprintf(text, "%sSoft K-Means (beta: %.3f, plusplus: %i)\n", text, beta, kmeansPlusPlus);
else sprintf(text, "%sGMM\n", text);
sprintf(text, "%sMetric: ", text);
switch(power)
{
case 0:
sprintf(text, "%sinfinite norm\n", text);
break;
case 1:
sprintf(text, "%s1-norm (Manhattan)\n", text);
break;
case 2:
sprintf(text, "%s2-norm (Euclidean)\n", text);
break;
default:
sprintf(text, "%s%d-norm\n", text, power);
break;
}
return text;
}
|