Boxcar moving average.
Calculate the moving average of an array with a boxcar of defined width. An odd width is recommended.
averaged – Averaged array.
- Return type
The moving average is calculated at each point of the array by finding the (unweighted) mean of the subarrays of length given by width. These subarrays are centred at the point in the array that the current average is currently being calculated for. If an odd width is chosen, the sub array will include the current point plus an equal number of points on either side. However, if an even width is chosen, the sub array will bias including the extra point to the left of the current index. If the subarray spans past the boundaries, the values beyond the boundary is ignored and the mean is calculated by dividing by the number of points that are inside the boundaries.
>>> x = np.array([1, 2, 3, 4, 5]) >>> moving_average(x, 3) array([1.5, 2. , 3. , 4. , 4.5]) >>> moving_average(x, 2) array([1. , 1.5, 2.5, 3.5, 4.5])