10

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 ?

1
  • 1
    Please share some code you have written and the specific problem you find
    – lsmor
    Commented Oct 26, 2018 at 9:10

2 Answers 2

19

What about replacing range by np.arange ?

Try:

meancsmdr100 = cumsummdr100 / np.arange(1,51)
3
  • Thanks for advice. I changed that line with yours. I made so much and simple calculations to get these results. But i wanna learn that, is there a more professional way to get the results? Commented Oct 26, 2018 at 10:21
  • What do you mean by professional? There is no cummean function shipped in numpy but by using two builtins cumsum and arange the code is fairly clean, readable and reliable. Commented Oct 26, 2018 at 10:22
  • Thanks very much. Commented Oct 26, 2018 at 10:26
0
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.