JoeyPython - 2 months ago 5

Python Question

Let's say I have an array:

`myArray = [[-1.58, -1.09, -0.41, 0.22, -0.95],`

[-1.16, -1.27, -1.89, -1.01, 1.11],

[-0.73, -0.81, -0.47, -0.46, -0.04],

[-1.46, -0.82, 0.40, -0.22, -1.82],

[-1.12 , -0.97, -0.89, -0.18, 0.06]]

I wish to convert each line of this array into values that sum to 1 for each row.

Is this easily possible?

My actual array is several thousand lines long, so I would like a solution that optimizes well if possible. Thank you very much!

I realize that I'm not being clear.

I want the resulting values to be

I can give you a sample using positive values (That's total at the end):

Row1 1.10 2.20 3.30 4.40 5.50 (Total = 16.50)

Row2 2.20 3.30 4.40 5.50 6.60 (Total = 22.00)

Row3 4.20 5.01 2.50 3.30 1.10 (Total = 16.11)

to (again total at the end)::

Row1 0.07 0.13 0.20 0.27 0.33 (Total = 1.00)

Row2 0.10 0.15 0.20 0.25 0.30 (Total = 1.00)

Row3 0.26 0.31 0.16 0.20 0.07 (Total = 1.00)

And i achieve this by simply adding a row, then diving each cell in each row by the total of that row. I don't know how to achieve this in python with an array, with negative values.

Answer

First using min-max normalization to transform original data, this could be one approach:

```
myArray = [[-1.58, -1.09, -0.41, 0.22, -0.95],
[-1.16, -1.27, -1.89, -1.01, 1.11],
[-0.73, -0.81, -0.47, -0.46, -0.04],
[-1.46, -0.82, 0.40, -0.22, -1.82],
[-1.12 , -0.97, -0.89, -0.18, 0.06]]
#Transform data
normalizedArray = []
for row in range(0, len(myArray)):
list = []
Min = min(myArray[row])
Max = max(myArray[row])
for element in myArray[row]:
list.append( float(element-Min)/float(Max- Min) )
normalizedArray.append(list)
#Normalize to 1
newArray = []
for row in range(0, len(normalizedArray)):
list = [x / sum(normalizedArray[row]) for x in normalizedArray[row]]
newArray.append(list)
```

Source (Stackoverflow)

Comments