Reputation: 159
I have a huge number of lists and I want to compute the average without storing all of them. Is there any python implementation to do this ?
Thanks
Upvotes: 0
Views: 251
Reputation: 53029
If you want to average entire arrays (must be compatible shapes):
running_sum = None
count = 0
for a in yourarraysource:
if running_sum is None:
running_sum = a
else:
running_sum = running_sum + a
count += 1
average = running_sum / count
If you want to average all elements:
running_sum = 0
count = 0
for a in yourarraysource:
running_sum += a.sum()
count += a.size
average = running_sum / count
Upvotes: 1