Scaling an array of occurences in python

59 Views Asked by At

I have an array of ints which describes the absolute occurrence of unique items in a data set. eg. a = [5, 3, 1] means there are three unique items with a data set length of 9, perhaps x y and z, and they occur

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

How can I "stretch" array a to a smaller or larger sized int array by maintaining proportions between the ints? Since exact proportions can't be maintained, I think about rounding things up, eg an array of 3 items shrunk from a would look like:

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

There are 1 best solutions below

8
Karol Adamiak On

You could use list multiplying. Let me know if this example is enough for you to continue with your work.

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]