Home > Net >  average list of arrays within a list for each number
average list of arrays within a list for each number

Time:12-13

How to average this 2 list consists of array and list, i mean to get average on each number?

I tried below: c = (a b)/2 but results in error 'unsupported operand types'

Please help me in acheiving this like the expected output below

a
[array([2.945202e-02, -5.945202e-02, 7.9454265e-03],dtype=float32),
array([2.945202e-02, -5.945202e-02, 1.9454265e-03],dtype=float32),
array([2.945202e-02, -5.945202e-02, 7.9454265e-03],dtype=float32)]

b
[array([4.945202e-02, 5.945202e-02, 5.9454265e-03],dtype=float32),
array([5.945202e-02, -7.945202e-02, 6.9454265e-03],dtype=float32),
array([6.945202e-02, -8.945202e-02, 7.9454265e-03],dtype=float32)]

Expected output average sample format on each number (Not the right answer of above average):

c
[array([1.945202e-02, 2.945202e-02, 2.9454265e-03],dtype=float32),
array([5.945202e-02, -4.945202e-02, 6.9454265e-03],dtype=float32),
array([6.945202e-02, -5.945202e-02, 7.9454265e-03],dtype=float32)]

CodePudding user response:

If you want to find the average of a and b, you can zip a and b and iterate over the pairs of arrays and find the average of each:

avg = [(i j)/2 for i,j in zip(a,b)]

Output:

[array([0.03945202, 0.        , 0.00694543], dtype=float32),
 array([ 0.04445202, -0.06945202,  0.00444543], dtype=float32),
 array([ 0.04945202, -0.07445202,  0.00794543], dtype=float32)]

or find the average using np.mean:

avg = np.mean([a, b], axis=0)

Output:

array([[ 0.03945202,  0.        ,  0.00694543],
       [ 0.04445202, -0.06945202,  0.00444543],
       [ 0.04945202, -0.07445202,  0.00794543]], dtype=float32)

But these answers don't match the expected output.

  • Related