I would like to normalize the values of a Python dictionary, while maintaining a lower bound for each value, where the lower bound is given by 1/(10 * n), where n is the number of values in the dictionary.
The values of a dictionary d can be normalized as below.
from __future__ import division
d = {1:0.5, 2: 1.5, 3: 2.0, 4: 0.02, 5: 3.1}
total = sum(d.values())
d = {k: v / total for k, v in d.iteritems()}
print d
The lower bound in this case is 0.02
lb = 1 / (10 * len(d.keys()))
print lb
And some values are less than the lower bound after normalization. The problem is that if I set the values less than the lb to lb, the sum of values becomes greater than one. The following approach does not work
D = dict(zip(d.keys(),[lb if i/total < lb else i/total for i in d.values()]))
print sum(D.values())
We need a way to repeatedly normalize and set lb till all values are more than lb and the sum of values is 1. What would the Python way solve this problem?
– Chandu Sep 24 '18 at 09:33