编辑:这个问题的答案在大量讨论中:Spark中的Sum变坏了
在“计算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;
}
我在这里找到的。
因此,想法是tot
(总计)等于分配给聚类的点数(即等于我们的数据集的大小),而uf
(针对不平衡因数)则等于分配给聚类的点数的平方。
最后,他用uf = uf * n / (tot * tot);
它来计算。
在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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句