gpt4 book ai didi

python - Spark 中的总和变坏了

转载 作者:太空宇宙 更新时间:2023-11-04 05:23:11 25 4
gpt4 key购买 nike

基于 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岁!

我在这里错过了什么?

最佳答案

问题是因为您没有计算每个集群中分组的点数,因此您必须更改 pdd 的创建方式。

pdd = r2_10.map(lambda x: (x[0], len(x[1]))).reduceByKey(lambda a, b: a + b)

但是,您可以通过映射 RDD 的值,然后使用 进行缩减,在一次通过中获得相同的结果(无需计算 pdd)求和

total = r2_10.map(lambda x: len(x[1])).sum()

关于python - Spark 中的总和变坏了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39627773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com