Cyrine Abdelkafi
Cyrine Abdelkafi

Reputation: 159

Python: Compute the average of a big numbet of arrays without saving them

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

Answers (1)

Paul Panzer
Paul Panzer

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

Related Questions