0

Currently I'm looking for a compact and more efficient solution (rather than multiple nested for loops) to compute mean of values given an index across multiple numpy array.

Specifically given

[array([2.4, 3.5, 2.9]),
array([4.5, 1.8, 1.4])]

I need to compute the following array:

[array([3.45, 2.65, 2.15])]

Any idea? Thank you all.

Nipper
  • 207
  • 2
  • 15

2 Answers2

1

It's possible by just one line command with numpy

import numpy as np

arr=[np.array([2.4, 3.5, 2.9]),
np.array([4.5, 1.8, 1.4])]
np.mean(arr, axis = 0)
BarzanHayati
  • 637
  • 2
  • 9
  • 22
0

Without Numpy you can use map and zip to get it.

lists = [[2.4, 3.5, 2.9],[4.5, 1.8, 1.4]]

li = list(zip(*lists))
sumation = list(map(sum,li))
average = list(map( lambda x: x/len(lists) ,sumation))

print(s)