KMeans的不平衡因素?

时间:2016-08-30 19:25:01

标签: apache-spark machine-learning pyspark k-means bigdata

编辑:在Sum in Spark gone bad

中详细讨论了这些问题的答案

Compute Cost of Kmeans中,我们看到了如何计算他的KMeans模型的成本。我想知道我们是否能够计算不平衡因子?

如果Spark没有提供这样的功能,有没有简单的方法来实现它?

我无法找到不平衡因子的参考,但它应该类似于Yael的unbalanced_factor(我的评论):

// @hist: the number of points assigned to a cluster
// @n:    the number of clusters
double ivec_unbalanced_factor(const int *hist, long n) {
  int vw;
  double tot = 0, uf = 0;

  for (vw = 0 ; vw < n ; vw++) {
    tot += hist[vw];
    uf += hist[vw] * (double) hist[vw];
  }

  uf = uf * n / (tot * tot);

  return uf;

}

我找到了here

所以我的想法是tot(总计)将等于分配给聚类的点数(即等于我们数据集的大小),而uf(对于不平衡因子)保持分配给集群的点数的平方。

最后他使用uf = uf * n / (tot * tot);来计算它。

1 个答案:

答案 0 :(得分:2)

python中,它可能类似于:

# I suppose you are passing an RDD of tuples, where the key is the cluster and the value is a vector with the features.
def unbalancedFactor(rdd):
  pdd = rdd.map(lambda x: (x[0], 1)).reduceByKey(lambda a, b: a + b) # you can obtain the number of points per cluster
  n = pdd.count()
  total = pdd.map(lambda x: x[1]).sum() 
  uf = pdd.map(lambda x: x[1] * float(x[1])).sum()

  return uf * n / (total * total)