python - How can I get the mean value of an array? -
i trying calculate mean value of last 10 elements. first reading out data file:
np.genfromtxt(filename,skip_header=6, names=true)
first tried use numpy.mean function getting following error:
typeerror: cannot perform reduce flexible type
so decided of writing function:
def get_mean_values( marray, nr ): """ """ # creating empty array tmp = np.delete( marray[-1:], 0, 0 ) key in marray.dtype.fields: tmp[key] = np.append( tmp[key], np.mean( marray[key][nr:] ) ) print tmp return tmp
but tmp array empty. how can calculate , store mean value of last 10 elements.
this how data looks
array([ (99.9, 9.0), (100.0, 9.0) ....], dtype=[('time', '<f8'), ('horwind', '<f8')])
the mean function works fine long specific enough :)
import numpy x = numpy.array([(99.9, 9.0), (100.0, 9.0)], dtype=[('time', '<f8'), ('horwind', '<f8')]) print 'time', x['time'].mean() print 'horwind', x['horwind'].mean()
output:
time 99.95 horwind 9.0
Comments
Post a Comment