I would like to normalize all values in the dictionary data
and store them again in another dictionary with the same keys and for each key the values should be store in 1D array so I did the following:
>>> data = {1: [0.6065306597126334], 2: [0.6065306597126334, 0.6065306597126334, 0.1353352832366127], 3: [0.6065306597126334, 0.6065306597126334, 0.1353352832366127], 4: [0.6065306597126334, 0.6065306597126334]}
>>> norm = {k: [v / sum(vals) for v in vals] for k, vals in data.items()}
>>> norm
{1: [1], 2: [0.4498162176582741, 0.4498162176582741, 0.10036756468345168], 3: [0.4498162176582741, 0.4498162176582741, 0.10036756468345168], 4: [0.5, 0.5]}
Now suppose the dictionary data
contains only a zero value for one of it's keys like the value of the first key 1
:
>>> data = {1: [0.0], 2: [0.6065306597126334, 0.6065306597126334, 0.1353352832366127], 3: [0.6065306597126334, 0.6065306597126334, 0.1353352832366127], 4: [0.6065306597126334, 0.6065306597126334]}
then normalizing the values of this dictionary will result by [nan]
values because of the division by zero
>>> norm = {k: [v / sum(vals) for v in vals] for k, vals in data.items()}
__main__:1: RuntimeWarning: invalid value encountered in double_scalars
>>> norm
{1: [nan], 2: [0.4498162176582741, 0.4498162176582741, 0.10036756468345168], 3: [0.4498162176582741, 0.4498162176582741, 0.10036756468345168], 4: [0.5, 0.5]}
So I inserted an if statement
to overcome this issue but I can't store the values for each key as a ID array
the code
>>> norm = {}
>>> for k, vals in data.items():
... values = []
... if sum(vals) == 0:
... values.append(list(vals))
... else:
... for v in vals:
... values.append(list([v/sum(vals)]))
... norm[k]=values
...
>>> norm
{1: [[1.0]], 2: [[0.4498162176582741], [0.4498162176582741], [0.10036756468345168]], 3: [[0.4498162176582741], [0.4498162176582741], [0.10036756468345168]], 4: [[0.5], [0.5]]}
I would like to get the norm
dictionary as
norm = {1: [1.0], 2: [0.4498162176582741, 0.4498162176582741, 0.10036756468345168], 3: [0.4498162176582741, 0.4498162176582741, 0.10036756468345168], 4: [0.5, 0.5]}
Also, For the dictionary data
, while it contains a zero value for one if it's keys, is there a better solution to normalize it because I think that my solution is not efficient!
P.S: I tried at the end of the for loop norm[k]= np.array(values)
instead of norm[k]=values
but the result was not as required.