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))