0
def moving_average_forecast(series, window_size):
  """Forecasts the mean of the last few values.
     If window_size=1, then this is equivalent to naive forecast"""
  forecast = []
  for time in range(len(series) - window_size):
    forecast.append(series[time:time + window_size].mean())
  return np.array(forecast)

moving_avg = moving_average_forecast(series, 30)[split_time - 30:]

What does this [split_time - 30:] mean after the function call moving_average_forecast(series, 30)?

PS: The series is an numpy array.

Thanks

Will Wang
  • 123
  • 1
  • 1
  • 11
  • It's called [array slicing](https://stackoverflow.com/questions/509211/understanding-slice-notation). – Equinox Jun 07 '20 at 14:50
  • 1
    Does this answer your question? [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – jonrsharpe Jun 07 '20 at 14:50
  • 1
    It is simply a shorthand for doing: `arr = moving_average_forecast(series, 30) ; moving_avg = arr[split_time - 30:]` – Tomerikoo Jun 07 '20 at 14:51
  • Hi @venky__ I can understand the array slicing, but I didn't understand why it write like this: moving_average_forecast(series, 30)[split_time - 30:], I think it should like moving_average_forecast(series[split_time - 30:], 30). – Will Wang Jun 07 '20 at 14:51
  • Thanks you @Tomerikoo, now I can understand. – Will Wang Jun 07 '20 at 14:53

1 Answers1

0

It is simply a shorthand for doing: arr = moving_average_forecast(series, 30) ; moving_avg = arr[split_time - 30:] Thank you @Tomerikoo

Will Wang
  • 123
  • 1
  • 1
  • 11