[本文链接:http://www.cnblogs.com/breezedeus/archive/2012/09/05/2671572.html,转载请注明出处]

 

在K-means聚类算法里,我们首先需要在已有的数据点中选取K个点作为初始中心点。这个bug就出现在中心点的随机选取上,mahout的实现不是真的随机。

【位置】:

     org.apache.mahout.clustering.kmeans.RandomSeedGenerator#buildRandom(...) , 行 88 - 110 这段。

 

我简化了一下,mahout的随机抽取逻辑如下:

/**
 * Sample K integers from integer interval [0, N).
 * @param N
 * @param K
 * @return
 */
int K) {
   8:     List<Integer> chosen = Lists. newArrayListWithCapacity(K);
   9:     Random random = RandomUtils. getRandom();
int n = 0; n < N; ++n) {
int currentSize = chosen.size();
if (currentSize < K) {
  13:             chosen.add(n);
if (random.nextInt(currentSize + 1) != 0) {
// evict one chosen randomly
  16:             chosen.remove(indexToRemove);
  17:             chosen.add(n);
  18:         }
  19:     }
return chosen;
  21: }

相关文章:

  • 2022-02-02
  • 2021-10-18
  • 2021-10-24
  • 2022-01-27
  • 2021-08-25
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
  • 2021-09-30
相关资源
相似解决方案