基于Unbalanced factor of KMeans?,我试图计算不平衡因子,但我失败了。
RDD r2_10
的每个元素都是一对,其中键是簇,值是点的元组。所有这些都是ID。下面我介绍一下会发生什么:
In [1]: r2_10.collect()
Out[1]:
[(0, ('438728517', '28138008')),
(13824, ('4647699097', '6553505321')),
(9216, ('2575712582', '1776542427')),
(1, ('8133836578', '4073591194')),
(9217, ('3112663913', '59443972', '8715330944', '56063461')),
(4609, ('6812455719',)),
(13825, ('5245073744', '3361024394')),
(4610, ('324470279',)),
(2, ('2412402108',)),
(3, ('4766885931', '3800674818', '4673186647', '350804823', '73118846'))]
In [2]: pdd = r2_10.map(lambda x: (x[0], 1)).reduceByKey(lambda a, b: a + b)
In [3]: pdd.collect()
Out[3]:
[(13824, 1),
(9216, 1),
(0, 1),
(13825, 1),
(1, 1),
(4609, 1),
(9217, 1),
(2, 1),
(4610, 1),
(3, 1)]
In [4]: n = pdd.count()
In [5]: n
Out[5]: 10
In [6]: total = pdd.map(lambda x: x[1]).sum()
In [7]: total
Out[7]: 10
和total
应该具有总点数。然而,它是10 ......目标是22岁!
我在这里缺少什么?
答案 0 :(得分:1)
问题是因为您错过了计算每个群集中分组的点数,因此您必须更改pdd
的创建方式。
pdd = r2_10.map(lambda x: (x[0], len(x[1]))).reduceByKey(lambda a, b: a + b)
但是,您可以通过映射pdd
的值然后使用RDD
进行缩减来一次性获得相同的结果(不计算sum
)。
total = r2_10.map(lambda x: len(x[1])).sum()