0

I want to go through the list_ function within the numpy array and much like a for loop I want the mean to be calculated of every 3 numbers in the list. So the y_mean would be calculated with the first 3 values 457.334015,424.440002,394.795990 and then it will be calculated with the next 3 values 424.440002,394.795990,408.903992 and so on. The y_mean function does not work below is there a way I could fix it.

import numpy as np
list_= np.array([457.334015,424.440002,394.795990,408.903992,398.821014,402.152008,435.790985,423.204987,411.574005,
404.424988,399.519989,377.181000,375.467010,386.944000,383.614990,375.071991,359.511993,328.865997,
320.510010,330.079010,336.187012,352.940002,365.026001,361.562012,362.299011,378.549011,390.414001,
400.869995,394.773010,382.556000])

number = 3
start = np.arange(start=1, stop=len(list_)-number, step=1)
stop = np.arange(start=number+1, stop=len(list_), step=1)
y_mean = list_[start:stop].mean()
print(y_mean)

Julien
  • 13,986
  • 5
  • 29
  • 53
fire fireeyyy
  • 71
  • 1
  • 8
  • @Julien I have updated the details. The `y_mean` does not go through the ranged `list_` – fire fireeyyy Jan 12 '21 at 04:24
  • You have posted the same question twice, please wait for an answer for that and if you want to change something about the question, please edit that instead of starting a new post. Other people might be trying to solve your original post. – Ananda Jan 12 '21 at 04:41
  • @firefireeyyy please let me know if my answer is useful :) – Pablo C Jan 12 '21 at 04:54
  • Does this answer your question? [Moving average or running mean](https://stackoverflow.com/questions/13728392/moving-average-or-running-mean) – mtrw Jan 12 '21 at 05:04

1 Answers1

2

You can use numpy.convolve:

np.convolve(list_, np.ones(3), "valid")/3
Pablo C
  • 4,661
  • 2
  • 8
  • 24