27 using namespace shogun;
59 int32_t num=lhs->get_num_vectors();
63 for (int32_t i=0; i<num; i++)
130 #ifndef DOXYGEN_SHOULD_SKIP_THIS
140 #endif // DOXYGEN_SHOULD_SKIP_THIS
146 struct thread_data *TD=(
struct thread_data*) P;
157 for (j=js; j<je; j++)
167 sum = sum +
CMath::sq(x[i*m + k] - vec[k]);
181 ASSERT(lhs && lhs->get_num_features()>0 && lhs->get_num_vectors()>0);
183 int32_t XSize=lhs->get_num_vectors();
185 int32_t i, changed=1;
193 int32_t *ClList=
SG_CALLOC(int32_t, XSize);
206 memset(ClList, 0,
sizeof(int32_t)*XSize);
208 memset(weights_set, 0,
sizeof(
float64_t)*k);
211 memset(mus.matrix, 0,
sizeof(
float64_t)*XDimk);
215 for (i=0; i<XSize; i++)
221 weights_set[Cl]+=weight;
224 vec=lhs->get_feature_vector(i, vlen, vfree);
227 mus.matrix[Cl*dimensions+j] += weight*vec[j];
229 lhs->free_feature_vector(vec, i, vfree);
235 if (weights_set[i]!=0.0)
237 mus.matrix[i*dimensions+j] /= weights_set[i];
248 for(int32_t idx=0;idx<XSize;idx++,p_dists+=
k)
252 for (i=0; i<XSize; i++)
259 if (dists[i*k+j]<mini)
270 for (i=0; i<XSize; i++)
272 const int32_t Cl = ClList[i];
274 weights_set[Cl]+=weight;
276 vec=lhs->get_feature_vector(i, vlen, vfree);
279 mus.matrix[Cl*dimensions+j] += weight*vec[j];
281 lhs->free_feature_vector(vec, i, vfree);
288 if (weights_set[i]!=0.0)
292 mus.matrix[i*dimensions+j] /= weights_set[i];
304 SG_WARNING(
"kmeans clustering changed throughout %d iterations stopping...\n",
max_iter-1);
307 SG_INFO(
"Iteration[%d/%d]: Assignment of %i patterns changed.\n", iter,
max_iter, changed);
312 memset(mus.matrix, 0,
sizeof(
float64_t)*XDimk);
314 for (i=0; i<XSize; i++)
317 int32_t Cl=ClList[i];
320 vec=lhs->get_feature_vector(i, vlen, vfree);
323 mus.matrix[Cl*dimensions+j] += weight*vec[j];
325 lhs->free_feature_vector(vec, i, vfree);
331 if (weights_set[i]!=0.0)
333 mus.matrix[i*dimensions+j] /= weights_set[i];
339 for (i=0; i<XSize; i++)
343 const int32_t ClList_Pat=ClList[Pat];
347 weight=Weights.
vector[Pat];
350 for(int32_t idx_k=0;idx_k<
k;idx_k++)
354 imini=0 ; mini=dists[0];
362 if (imini!=ClList_Pat)
364 changed= changed + 1;
367 weights_set[imini]+= weight;
369 weights_set[ClList_Pat]-= weight;
371 vec=lhs->get_feature_vector(Pat, vlen, vfree);
375 mus.matrix[imini*dimensions+j]-=(vec[j]
376 -mus.matrix[imini*dimensions+j])
377 *(weight/weights_set[imini]);
380 lhs->free_feature_vector(vec, Pat, vfree);
384 if (weights_set[ClList_Pat]!=0.0)
386 vec=lhs->get_feature_vector(Pat, vlen, vfree);
390 mus.matrix[ClList_Pat*dimensions+j]-=
392 -mus.matrix[ClList_Pat
394 *(weight/weights_set[ClList_Pat]);
396 lhs->free_feature_vector(vec, Pat, vfree);
401 mus.matrix[ClList_Pat*dimensions+j]=0;
415 bool first_round=
true;
417 for (int32_t j=0; j<
k; j++)
427 mus.matrix[i*dimensions+l]
428 -mus.matrix[j*dimensions+l]);
439 if ((dist<rmin2) && (dist>=rmin1))