steeeve - 6 months ago 42

Python Question

I have a vector

`X`

`N = 10e6`

`for i in range(0,N-1):`

Ex[i] = X[0:i+1].mean()

This is a terribly inefficient way of doing this. What would be a more intelligent algorithm for Python? Note

`Ex`

`X`

Answer

A `numpy`

-centric solution could be as follows:

```
X = np.random.rand(10**6)
EX = np.cumsum(X) / np.arange(1, X.shape[0]+1)
```

Source (Stackoverflow)