Ad

Run And Access For Loop Over Numpy Array With Multiplication And Division In Python

- 1 answer

I have following list of arrays. Now I would like to leave the first elements of each array as it is and then multiply each first element within each array with all other numbers in the same array (same row).

myarray:

[array([0.9870343 , 0.52241888, 0.36668887, 0.56469955]),
 array([1.06875891, 0.47363532, 0.34032307, 0.54298871]),
 array([1.07251434, 0.48067476, 0.38125151, 0.53832131]),
 array([3.00953588, 0.8133002 , 1.44178431, 0.48336691])]

So for example for the first array (first line) I would like to have 4 numbers: The first one should stay as it is, the second number should be multiplied with the first: 0.9870343*0.52241888=0.51564535. The third and fourth number should also multiplied with the first: 0.9870343*0.36668887=0.36193449 and 0.9870343*0.56469955=0.5573778250.

Expected output in the same format, but multiplied:

        [array([0.9870343 , 0.51564535, 0.36193449, 0.55737782]),
         array([1.06875891, 0.50620196, 0.36372331, 0.58032402]),
         array([1.07251434, 0.51553057, 0.40889771, 0.57735732]),
         array([3.00953588, 2.44765613, 4.3391016, 1.45471005])]

Afterwards, I would like to get the max value for each array (row) and divide all values from one array through the max value. So in the first: array([0.9870343 , 0.51564535, 0.36193449, 0.55737782] the maximum value is 0.9870343, so I want to divide all numbers from this array through 0.9870343. Then for the next row the same step. There the highest number is 1.06875891. So I want to divide all values in that row through 1.06875891. And so on.

Expected output for step 2:

[array([1 , 0.52241887, 0.36668886, 0.56469954]),
 array([1, 0.4736353, 0.3403230, 0.54298870]),
 array([1, 0.48067475, 0.38125150, 0.53832130]),
 array([0.69358502, 0.56409283, 1, 0.33525604])]

I know how to access each element and multiply or divide it by the others but I don't know exactly how to run a loop over the numpy array. My code:

A = myarray[0][0]
B = myarray[0][1]
C = myarray[0][2]
D = myarray[0][3]

A=a
multiplyAB=A*B
multiplyAC=A*C
multiplyAD=A*D

print(A,multiplyAB,multiplyAC,multiplyAD)

norm=max(A,multiplyAB,multiplyAC,multiplyAD)

A=A/norm
multiplyAB=multiplyAB/norm
multiplyAC=multiplyAC/norm
multiplyAD=multiplyAD/norm

print(A,multiplyAB,multiplyAC,multiplyAD)
Ad

Answer

You could cast myarray as a numpy array and use numpy broadcasting to multiply by a column vector and divide by a column vector (edited to use more intuitive ways to keep the second dimension as suggested by @MadPhysicist):

myarray = np.array(myarray)
myarray[:, 1:] *= myarray[:, [0]]
myarray /= myarray.max(axis=1, keepdims=True)
print(myarray)

Output:

array([[1.        , 0.52241888, 0.36668887, 0.56469955],
       [1.        , 0.47363532, 0.34032307, 0.54298871],
       [1.        , 0.48067476, 0.38125151, 0.53832131],
       [0.69358502, 0.56409284, 1.        , 0.33525605]])
Ad
source: stackoverflow.com
Ad