在python中缩放事件数组



我有一个int数组,它描述了数据集中唯一项的绝对出现。例如,a = [5, 3, 1]意味着有三个数据集长度为9的唯一项,可能是x、y和z,并且它们发生

x -> 5 times
y -> 3 times
z -> once

我怎么能"拉伸";通过保持int之间的比例,将数组a转换为更小或更大大小的int数组?由于无法保持精确的比例,我考虑将其四舍五入,例如,从a缩小的3个项目的数组看起来像:

x -> 2 times
y -> once
z -> none (because it's the least probable to occur in the original array)

您可以使用列表乘法。让我知道这个例子是否足以让你继续你的工作。

from collections import Counter
from math import ceil
init_list = [3, 4, 5, 5, 5, 4, 4, 4]
occur_dict = Counter(init_list)
new_length = 20
old_length = len(init_list)
new_occur_dict = {num: ceil(occur / old_length * new_length)
for (num, occur) in occur_dict.items()}
# new occurrences dict, rounded up so sum should be bigger than new _length
sorted_nums = [num for (num, occur) in sorted(occur_dict.items(),
key=lambda x: x[1])]
# sorting keys by occurrences, so lowest number will be first
while sum(new_occur_dict.values()) > new_length:
for number in sorted_nums:
new_occur_dict[number] -= 1 #removing extra occurrences to match new_length
if sum(new_occur_dict.values()) == new_length:
break
new_list = []
for item in occur_dict:
new_list += [item] * new_occur_dict[item]

最新更新