Issue
sample_list = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
Required to take the average of every 4
like 1,2,3,4 average is 2.5
followed by 5,6,7,8 is 6.5
followed by 9,10,11,12 is 10.5
followed by 13,14,15,16 is 14,5
expexted output:
[2.5, 6.5, 10.5, 14.5]
so far i tried with refering this questions Average of each consecutive segment in a list
Calculate the sum of every 5 elements in a python array
Solution
Use reshape
. In following example reshape(-1, 4) means 4 elements per row
import numpy as np
sample_list = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
print(np.mean(sample_list.reshape(-1, 4), axis=1))
output
[2.5, 6.5, 10.5, 14.5]
Answered By - Faisal Nazik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.