27 using namespace shogun;
60 int32_t num=lhs->get_num_vectors();
64 for (int32_t i=0; i<num; i++)
132 #ifndef DOXYGEN_SHOULD_SKIP_THIS
142 #endif // DOXYGEN_SHOULD_SKIP_THIS
148 struct thread_data *TD=(
struct thread_data*) P;
159 for (j=js; j<je; j++)
169 sum = sum +
CMath::sq(x[i*m + k] - vec[k]);
183 ASSERT(lhs && lhs->get_num_features()>0 && lhs->get_num_vectors()>0);
185 int32_t XSize=lhs->get_num_vectors();
187 int32_t i, changed=1;
196 int32_t *ClList=
SG_CALLOC(int32_t, XSize);
209 memset(ClList, 0,
sizeof(int32_t)*XSize);
211 memset(weights_set, 0,
sizeof(
float64_t)*k);
214 memset(mus.matrix, 0,
sizeof(
float64_t)*XDimk);
218 for (i=0; i<XSize; i++)
224 weights_set[Cl]+=weight;
227 vec=lhs->get_feature_vector(i, vlen, vfree);
230 mus.matrix[Cl*dimensions+j] += weight*vec[j];
232 lhs->free_feature_vector(vec, i, vfree);
238 if (weights_set[i]!=0.0)
240 mus.matrix[i*dimensions+j] /= weights_set[i];
251 for(int32_t idx=0;idx<XSize;idx++,p_dists+=
k)
255 for (i=0; i<XSize; i++)
262 if (dists[i*k+j]<mini)
273 for (i=0; i<XSize; i++)
275 const int32_t Cl = ClList[i];
277 weights_set[Cl]+=weight;
279 vec=lhs->get_feature_vector(i, vlen, vfree);
282 mus.matrix[Cl*dimensions+j] += weight*vec[j];
284 lhs->free_feature_vector(vec, i, vfree);
291 if (weights_set[i]!=0.0)
295 mus.matrix[i*dimensions+j] /= weights_set[i];
307 SG_WARNING(
"kmeans clustering changed throughout %d iterations stopping...\n",
max_iter-1);
310 SG_INFO(
"Iteration[%d/%d]: Assignment of %i patterns changed.\n", iter,
max_iter, changed);
315 memset(mus.matrix, 0,
sizeof(
float64_t)*XDimk);
317 for (i=0; i<XSize; i++)
320 int32_t Cl=ClList[i];
323 vec=lhs->get_feature_vector(i, vlen, vfree);
326 mus.matrix[Cl*dimensions+j] += weight*vec[j];
328 lhs->free_feature_vector(vec, i, vfree);
334 if (weights_set[i]!=0.0)
336 mus.matrix[i*dimensions+j] /= weights_set[i];
342 for (i=0; i<XSize; i++)
346 const int32_t ClList_Pat=ClList[Pat];
350 weight=Weights.
vector[Pat];
353 for(int32_t idx_k=0;idx_k<
k;idx_k++)
357 imini=0 ; mini=dists[0];
365 if (imini!=ClList_Pat)
367 changed= changed + 1;
370 weights_set[imini]+= weight;
372 weights_set[ClList_Pat]-= weight;
374 vec=lhs->get_feature_vector(Pat, vlen, vfree);
378 mus.matrix[imini*dimensions+j]-=(vec[j]
379 -mus.matrix[imini*dimensions+j])
380 *(weight/weights_set[imini]);
383 lhs->free_feature_vector(vec, Pat, vfree);
387 if (weights_set[ClList_Pat]!=0.0)
389 vec=lhs->get_feature_vector(Pat, vlen, vfree);
393 mus.matrix[ClList_Pat*dimensions+j]-=
395 -mus.matrix[ClList_Pat
397 *(weight/weights_set[ClList_Pat]);
399 lhs->free_feature_vector(vec, Pat, vfree);
404 mus.matrix[ClList_Pat*dimensions+j]=0;
418 bool first_round=
true;
420 for (int32_t j=0; j<
k; j++)
430 mus.matrix[i*dimensions+l]
431 -mus.matrix[j*dimensions+l]);
442 if ((dist<rmin2) && (dist>=rmin1))
virtual void copy_feature_matrix(SGMatrix< ST > src)
virtual bool save(FILE *dstfile)
Class Distance, a base class for all the distances used in the Shogun toolbox.
int32_t max_iter
maximum number of iterations
int32_t dimensions
number of dimensions
SGVector< float64_t > R
radi of the clusters (size k)
ST * get_feature_vector(int32_t num, int32_t &len, bool &dofree)
void free_feature_vector(ST *feat_vec, int32_t num, bool dofree)
A generic DistanceMachine interface.
void clustknb(bool use_old_mus, float64_t *mus_start)
void add(bool *param, const char *name, const char *description="")
SGVector< float64_t > get_radiuses()
virtual void destroy_vector()
virtual bool load(FILE *srcfile)
void set_max_iter(int32_t iter)
void distances_rhs(float64_t *result, int32_t idx_b1, int32_t idx_b2, int32_t idx_a)
CFeatures * replace_rhs(CFeatures *rhs)
float64_t distance(int32_t idx_a, int32_t idx_b)
The class Features is the base class of all feature objects.
void set_distance(CDistance *d)
virtual void store_model_features()
virtual EFeatureType get_feature_type()=0
static float32_t sqrt(float32_t x)
x^0.5
virtual bool train_machine(CFeatures *data=NULL)
virtual bool init(CFeatures *lhs, CFeatures *rhs)
virtual void free_matrix()
int32_t k
the k parameter in KMeans
#define SG_CALLOC(type, len)
void * sqdist_thread_func(void *P)
SGMatrix< float64_t > get_cluster_centers()