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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-09 05:34:06 +0000
Seen: 20 times
Last updated: Jun 09 '23
Can an Array of Elements be made in Android?
What is the process of placing specific data at the beginning of an array?
How can user input be added to an array using a loop in C# and then displayed through printing?
How can the objects that are duplicated within a nested array be removed?
How can I resolve the ValueError regarding incompatible shapes between (None, 20, 9) and (None, 9)?
What is a more effective method to verify the type of an Array containing elements of any type?
What is the way to add up items in an array with two dimensions using Google Apps Script?
How can an array be filtered using the values in another array?