Q:

Write a NumPy program to create a new array which is the average of every consecutive triplet of elements of a given array

0

Write a NumPy program to create a new array which is the average of every consecutive triplet of elements of a given array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
arr1 = np.array([1,2,3, 2,4,6, 1,2,12, 0,-12,6])
print("Original array:")
print(arr1)
result = np.mean(arr1.reshape(-1, 3), axis=1)
print("Average of every consecutive triplet of elements of the said array:")
print(result)

Sample Output:

Original array:
[  1   2   3   2   4   6   1   2  12   0 -12   6]
Average of every consecutive triplet of elements of the said array:
[ 2.  4.  5. -2.]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now