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

  • برمجة بايثون

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

الأجوبة

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.]
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...