Harun Horasanlı
Harun Horasanlı

Reputation: 165

Calculating the Cumulative Mean in Python

i am new on programming and python. I made a simulation mm1 queue. I ran it properly. I took the results. I have an 5000 output. But now i should calculate the cumulative mean of average delays for every 100 period(1 to 100, 1 to 200... until 1 to 5000).

#data 4 (delay time) set assign to list of numpy array
npdelaytime = np.array(data[4][0:5000])  
#reshape the list of delay time 100 customer in each sample
npdelayreshape100 = np.reshape(npdelaytime, (-1,100))     
#mean of this reshape matrix
meandelayreshape100 = np.mean(npdelayreshape100, axis=1)  
cumsummdr100 = np.cumsum(meandelayreshape100)
a = range(1,51)     
meancsmdr100 = cumsummdr100 / a

I can figure this out like this. First reshape the 5000 sample point into to 100*50. Then taking the means of these matrix. Lastly cumsum of these means.

My Question : Is there a easy way to do this ?

Upvotes: 10

Views: 17883

Answers (2)

morteza omidipoor
morteza omidipoor

Reputation: 119

def cum_mean(arr):
    cum_sum = np.cumsum(arr, axis=0)    
    for i in range(cum_sum.shape[0]):       
        if i == 0:
            continue        
        print(cum_sum[i] / (i + 1))
        cum_sum[i] =  cum_sum[i] / (i + 1)
    return cum_sum

Upvotes: 0

Learning is a mess
Learning is a mess

Reputation: 8277

What about replacing range by np.arange ?

Try:

meancsmdr100 = cumsummdr100 / np.arange(1,51)

Upvotes: 19

Related Questions