python moving average of list

Solutions on MaxInterview for python moving average of list by the best coders in the world

showing results for - "python moving average of list"
Imran
22 Jan 2021
1import numpy
2def running_mean(x, N):
3  """ x == an array of data. N == number of samples per average """
4    cumsum = numpy.cumsum(numpy.insert(x, 0, 0)) 
5    return (cumsum[N:] - cumsum[:-N]) / float(N)
6  
7val = [-30.45, -2.65, 56.61, 47.13, 47.95, 30.45, 2.65, -28.31, -47.13, -95.89]
8print(running_mean(val, 3))
9""" [  7.83666667  33.69666667  50.56333333  41.84333333  27.01666667
10   1.59666667 -24.26333333 -57.11      ]	"""