python - Numpy : Calculating the average of values between two indices -


i need calculate average of values between 2 indices. lets indices 3 , 10, , sum values between them , divide number of values.

easiest way using loop starting 3, going until 10, summing 'em up, , dividing. seems non-pythonic way , considering functionalities numpy offers, thought maybe there shorter way using numpy magic. suggestion appriciated

to access elements between 2 indices i , j can use slicing:

slice_of_array = array[i: j+1] # use j if not want index j included 

and average calculated np.average, in case want weight number of elements, can use np.mean:

import numpy np mean_of_slice = np.mean(slice_of_array) 

or in 1 go (using indices):

i = 3 j = 10 np.mean(array[i: j+1]) 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -