1

Given such a list:

mylist = [1, 2, 3, 4, 5, 6, 7]

with N = 3 as the size of running mean at each step.

What is the fastest way to calculate the average and standard deviation of this list?

If it was only average np.convolve could do the job, but what about the standard deviation? or standard error?

Vigor831
  • 105
  • 1
  • 10

2 Answers2

1
import numpy as np

mylist = [1, 2, 3, 4, 5, 6, 7]
double = mylist * 2
N = 3
mean_std = [(np.mean(double[i:i+N]), np.std(double[i:i+N])) for i in range(len(mylist))]
schlodinger
  • 537
  • 3
  • 14
1

Try:

import numpy as np
N=3
mylist = [1, 2, 3, 4, 5, 6, 7]

res=np.vstack([mylist[i:]+mylist[:i] for i in range(N)])

ma=res.mean(axis=0)
std=res.std(axis=0)

Just for moving average you can do: https://stackoverflow.com/a/14314054/11610186

Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34
  • This is correct for non-circulat data, what about circular ones? I also need to compute the average for [6,7,1] and [7,1,2] – Vigor831 Feb 15 '20 at 17:00