Python – How to normalize a list list using list inference in python

How to normalize a list list using list inference in python… here is a solution to the problem.

How to normalize a list list using list inference in python

f = [[7.0, 5.0, 9.0, 7.0], [7.0, 7.0, 8.0, 6.0], [12.0, 6.0, 5.0, 7.0], [5.0, 7.0, 9.0, 9.0], [9.0, 5.0, 6.0, 10.0], [2.0, 0.0, 0.0, 28.0], [0.0, 0.0, 0.0, 30.0], [0.0, 0.0, 0.0, 30.0], [0.0, 30.0, 0.0, 0.0], [2.0, 21.0, 4.0, 3.0], [4.0, 14.0, 11.0, 1.0], [3.0, 4.0, 20.0, 3.0], [0.0, 0.0, 30.0, 0.0], [30.0, 0.0, 0.0, 0.0], [30.0, 0.0, 0.0, 0.0], [30.0, 0.0, 0.0, 0.0], [4.0, 8.0, 5.0, 12.0], [7.0, 6.0, 9.0, 6.0], [5.0, 8.0, 8.0, 5.0], [6.0, 8.0, 2.0, 10.0], [9.0, 3.0, 7.0, 7.0]]

This is my list. I need to normalize this data. I’ve tried using this code

a=[(float(i) for i in d) for d in f)/(sum(j) for j in f)]

But I get an error. Can anyone tell me how to fix this?

Solution

The confusion can come from the fact that you are trying to mix two very different nested lists to understand the syntax.

All examples assume that you have a list of float lists. If this is not the case, you can convert the nested list first:

f = [[float(i) for i in j] for j in f]

Flat list with double understanding

The first one returns a flat list:

f = [[7.0, 5.0, 9.0, 7.0], [7.0, 7.0, 8.0, 6.0], [12.0, 6.0, 5.0, 7.0], [5.0, 7.0, 9.0, 9.0], [9.0, 5.0, 6.0, 10.0], [2.0, 0.0, 0.0, 28.0], [0.0, 0.0, 0.0, 30.0], [0.0, 0.0, 0.0, 30.0], [ 0.0, 30.0, 0.0, 0.0], [2.0, 21.0, 4.0, 3.0], [4.0, 14.0, 11.0, 1.0], [3.0, 4.0, 20.0, 3.0], [0.0, 0.0, 30.0, 0.0], [30.0, 0.0, 0.0, 0.0], [30.0, 0.0, 0.0, 0.0], [30.0, 0.0, 0.0, 0.0], [4.0, 8.0, 5.0, 12.0], [7.0, 6.0, 9.0, 6.0], [5.0, 8.0, 8.0, 5.0], [6.0, 8.0, 2.0, 10.0], [9.0, 3.0, 7.0, 7.0]]

a1 = [i / sum(j) for j in f for i in j]
print(a1)
# [0.25, 0.17857142857142858, 0.32142857142857145, 0.25, 0.25, 0.25, 0.2857142857142857, 0.21428571428571427, 0.4, 0.2, 0.16666666666666666, 0.23333333333333334, 0.16666666666666666, 0.23333333333333334, 0.3, 0.3, 0.3, 0.16666666666666666, 0.2, 0.3333333333333333, 0.06666666666666667, 0.0, 0.0, 0.9333333333333333, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.06666666666666667, 0.7, 0.13333333333333333, 0.1, 0.13333333333333333, 0.4666666666666667, 0.36666666666666664, 0.03333333333333333, 0.1, 0.13333333333333333, 0.6666666666666666, 0.1, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.13793103448275862, 0.27586206896551724, 0.1724137931034483, 0.41379310344827586, 0.25, 0.21428571428571427, 0.32142857142857145, 0.21428571428571427, 0.19230769230769232, 0.3076923076923077, 0.3076923076923077, 0.19230769230769232, 0.23076923076923078, 0.3076923076923077, 0.07692307692307693, 0.38461538461538464, 0.34615384615384615, 0.11538461538461539, 0.2692307692307692, 0.2692307692307692]

Nested lists with double understanding

The second returns a nested list:

a2 = [[i / sum(j) for i in j] for j in f]
print(a2)
# [[0.25, 0.17857142857142858, 0.32142857142857145, 0.25], [0.25, 0.25, 0.2857142857142857, 0.21428571428571427], [0.4, 0.2, 0.16666666666666666, 0.23333333333333334], [ 0.16666666666666666, 0.23333333333333334, 0.3, 0.3], [0.3, 0.16666666666666666, 0.2, 0.3333333333333333], [0.06666666666666667, 0.0, 0.0, 0.9333333333333333], [0.0, 0.0, 0.0, 1.0], [ 0.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 0.0], [0.06666666666666667, 0.7, 0.13333333333333333, 0.1], [0.13333333333333333, 0.4666666666666667, 0.36666666666666664, 0.03333333333333333], [0.1, 0.13333333333333333, 0.6666666666666666, 0.1], [0.0, 0.0, 1.0, 0.0], [1.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0], [0.13793103448275862, 0.27586206896551724, 0.1724137931034483, 0.41379310344827586], [0.25, 0.21428571428571427, 0.32142857142857145, 0.21428571428571427], [0.19230769230769232, 0.3076923076923077, 0.3076923076923077, 0.19230769230769232], [0.23076923076923078, 0.3076923076923077, 0.07692307692307693, 0.38461538461538464], [0.34615384615384615, 0.11538461538461539, 0.2692307692307692, 0.2692307692307692]]

Note the difference in the order of for i and for j.

In addition, both syntaxes calculate sum(j) for each iteration.

Normalization function

To avoid this, you can define a normalize function:

def normalize(l):
    s = sum(l)
    return [i/s for i in l]

print([normalize(j) for j in f])

Map and compression

Finally, you can use a combination of map and zip:

sums = map(sum, f)
print([[i/s for i in j] for j,s in zip(f, sums)])

Related Problems and Solutions