我有一个多个本地商店排名的数据集,我希望通过编程将其聚合/合并到一个国家排名中。我知道当地的排名是按销售量排列的,但我没有得到销量,所以必须使用相对排名尽可能准确地创造一个全国性的排名。
作为一个简短的例子,假设我们有3个本地排名列表,从最佳排名(第一)到最差排名(最后),它们代表着不同的地理边界,可以相互重叠。
ranking_1 = ['J','A','Z','B','C']
ranking_2 = ['A','H','K','B']
ranking_3 = ['Q','O','A','N','K']我们知道J或Q是排名最高的商店,因为两者在ranking_1和ranking_3中都是最高的,它们都出现在A之上,在ranking_2中最高。我们知道O是其次,就像它在ranking_3中高于A。a次之,等等……
如果我在纸上正确地这样做,这个简短的例子的输出将是:
global_ranking = [('J',1.5),('Q',1.5),('O',3),('A',4),('H',6),('N',6),('Z',6),('K',8),('B',9),('C',10)]请注意,当我们没有足够的数据来确定哪一家商店排名更高时,我们认为这是一种平分(即我们知道J或Q中的一家是排名最高的商店,但不知道哪一家更高,所以我们把它们都放在1.5)。在实际数据集中,每个.中都有1000+项的100+列表。
我很高兴尝试找出这个问题,我很好奇是否有人有聪明的方法来解决这个问题。
发布于 2020-01-28 01:18:27
修改后的合并排序算法在这里会有所帮助。修改应该考虑到无与伦比的商店,并建立一组不可比较的元素,您愿意认为它们是相等的(比如Q和J)。
发布于 2020-01-29 02:16:02
这种方法试图分析排名前面的所有商店。如果它们没有位于任何其他排名列表中的第一位,那么它们属于这一级别,并被添加到“级别”列表中。接下来,他们被从领跑者中移除,所有的列表都被调整,这样就有了新的领跑者。重复这个过程,直到没有剩下的商店。
def rank_stores(rankings):
"""
Rank stores with rankings by volume sales with over lap between lists.
:param rankings: list of rankings of stores also in lists.
:return: Ordered list with sets of items at same rankings.
"""
rank_global = []
# Evaluate all stores in the number one postion, if they are not below
# number one somewhere else, then they belong at this level.
# Then remove them from the front of the list, and repeat.
while sum([len(x) for x in rankings]) > 0:
tops = []
# Find out which of the number one stores are not in a lower position
# somewhere else.
for rank in rankings:
if not rank:
continue
else:
top = rank[0]
add = True
for rank_test in rankings:
if not rank_test:
continue
elif not rank_test[1:]:
continue
elif top in rank_test[1:]:
add = False
break
else:
continue
if add:
tops.append(top)
# Now add tops to total rankings list,
# then go through the rankings and pop the top if in tops.
rank_global.append(set(tops))
# Remove the stores that just made it to the top.
for rank in rankings:
if not rank:
continue
elif rank[0] in tops:
rank.pop(0)
else:
continue
return rank_global关于排名,规定:
ranking_1 = ['J','A','Z','B','C']
ranking_2 = ['A','H','K','B']
ranking_3 = ['Q','O','A','N','K']
rankings = [ranking_1, ranking_2, ranking_3]然后调用该函数:
rank_stores(rankings)在以下方面的成果:
[{'J', 'Q'}, {'O'}, {'A'}, {'H', 'N', 'Z'}, {'K'}, {'B'}, {'C'}]在某些情况下,可能没有足够的信息来确定确定的排名。试试这个命令。
['Z', 'A', 'B', 'J', 'K', 'F', 'L', 'E', 'W', 'X', 'Y', 'R', 'C']我们可以得出以下排名:
a = ['Z', 'A', 'B', 'F', 'E', 'Y']
b = ['Z', 'J', 'K', 'L', 'X', 'R']
c = ['F', 'E', 'W', 'Y', 'C']
d = ['J', 'K', 'E', 'W', 'X']
e = ['K', 'F', 'W', 'R', 'C']
f = ['X', 'Y', 'R', 'C']
g = ['Z', 'F', 'W', 'X', 'Y', 'R', 'C']
h = ['Z', 'A', 'E', 'W', 'C']
i = ['L', 'E', 'Y', 'R', 'C']
j = ['L', 'E', 'W', 'R']
k = ['Z', 'B', 'K', 'L', 'W', 'Y', 'R']
rankings = [a, b, c, d, e, f, g, h, i, j, k]调用该函数:
rank_stores(rankings)在以下方面的成果:
[{'Z'},
{'A', 'J'},
{'B'},
{'K'},
{'F', 'L'},
{'E'},
{'W'},
{'X'},
{'Y'},
{'R'},
{'C'}]在这种情况下,没有足够的信息来确定'J‘相对于'A’和'B‘应该在哪里。只是它在'Z‘和'K’之间的范围内。
当在数百个排名和商店中成倍增加时,一些商店将无法按绝对数量进行适当的排名。
https://stackoverflow.com/questions/59940357
复制相似问题