Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can use numpy's built-in function numpy.mean(array, axis) to calculate the average of each pair of consecutive elements in a numpy array:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])
arr_average = np.mean(arr.reshape(-1, 2), axis=1)
print(arr_average)

This will output:

[1.5 3.5 5.5]

Here, we first reshape the array into sub-arrays of length 2 using arr.reshape(-1, 2). Then we calculate the mean along the first axis (axis=1), which corresponds to the mean of each pair of consecutive elements.