Home > Back-end >  Normalization of a nested dictionary in python
Normalization of a nested dictionary in python

Time:03-23

I am new to Python and I have a nested dictionary for which I want to normalize the values of the dictionary. For example:

nested_dictionary={'D': {'D': '0.33', 'B': '0.17', 'C': '0.00', 'A': '0.17', 'K': '0.00', 'J': '0.03'}, 'A': {'A': '0.50', 'K': '0.00', 'J': '0.08'}}

And I would like to get the normalization as

Normalized_result={'D': {'D': '0.47', 'B': '0.24', 'C': '0.00', 'A': '0.24', 'K': '0.00', 'J': '0.04'}, 'A': {'A': '0.86', 'K': '0.00', 'J': '0.14'}}

I have seen the example in Normalizing dictionary values which only for one dictionary but I want to go further with nested one. I have tried to flatten the nested_dictionary and apply the normalization as

import flatdict
d =  flatdict.FlatDict(nested_dictionary, delimiter='_')
dd=dict(d)
newDict = dict(zip(dd.keys(), [float(value) for value in dd.values()]))

def normalize(d, target=1.0):
    global factor
    raw = sum(d.values())
    print(raw)
    if raw==0:
        factor=0
        #print('ok')
    else:
       # print('kok')
        factor = target/raw
    return {key:value*factor for key,value in d.items()}

normalize(newDict)

And I get the result as

{'D_D': 0.2578125,
 'D_B': 0.1328125,
 'D_C': 0.0,
 'D_A': 0.1328125,
 'D_K': 0.0,
 'D_J': 0.023437499999999997,
 'A_A': 0.39062499999999994,
 'A_K': 0.0,
 'A_J': 0.06249999999999999}

But what I want is the Normalized_result as above Thanks in advance.

CodePudding user response:

  1. Turn the string-values in your inner dicts into floats.
  2. Take one of the solutions from the the duplicate, for example really_safe_normalise_in_place.
  3. Use the solution on each dict.

Example:

d = {'D': {'D': '0.33', 'B': '0.17', 'C': '0.00', 'A': '0.17', 'K': '0.00', 'J': '0.03'}, 'A': {'A': '0.50', 'K': '0.00', 'J': '0.08'}}
d = {k: {kk: float(vv) for kk, vv in v.items()} for k, v in d.items()}
for v in d.values():
    really_safe_normalise_in_place(v)

CodePudding user response:

This code would do:

def normalize(d, target=1.0):
    raw = sum(float(number) for number in d.values())
    factor = (target/raw if raw else 0)
    return {key: f'{float(value)*factor:.2f}' for key, value in d.items()}


{key: normalize(dct) for key, dct in nested_dictionary.items()}

CodePudding user response:

nested_dictionary = {'D': {'D': '0.33', 'B': '0.17', 'C': '0.00', 'A': '0.17', 'K': '0.00', 'J': '0.03'},
                     'A': {'A': '0.50', 'K': '0.00', 'J': '0.08'}}

In this example, your dict values are str type, so we need to convert to float:

nested_dictionary = dict([b, dict([a, float(x)] for a, x in y.items())] for b, y in nested_dictionary.items())

nested_dictionary

{'D': {'D': 0.33, 'B': 0.17, 'C': 0.0, 'A': 0.17, 'K': 0.0, 'J': 0.03},
 'A': {'A': 0.5, 'K': 0.0, 'J': 0.08}}

The function below is adapted from the link you provided. It loops through the dictionaries, calculates the factor and updates the values inplace.

for _, d in nested_dictionary.items():
    factor = 1.0/sum(d.values())
    for k in d:
        d[k] = d[k] * factor

nested_dictionary

{'D': {'D': 0.47142857142857136,
  'B': 0.24285714285714285,
  'C': 0.0,
  'A': 0.24285714285714285,
  'K': 0.0,
  'J': 0.04285714285714285},
 'A': {'A': 0.8620689655172414, 'K': 0.0, 'J': 0.13793103448275865}}

If you need to convert back to str, use the function below:

nested_dictionary = dict([b, dict([a, "{:.2f}".format(x)] for a, x in y.items())] for b, y in nested_dictionary.items())

nested_dictionary

{'D': {'D': '0.47',
  'B': '0.24',
  'C': '0.00',
  'A': '0.24',
  'K': '0.00',
  'J': '0.04'},
 'A': {'A': '0.86', 'K': '0.00', 'J': '0.14'}}
  • Related