0

I have a temperature data that I keep as an array:

temp[month][day][hour][min]

Is there an easy way to get the hourly average of the data, which is the average of all values in the same hour? Similarly, the daily average?

1
  • Just use a loop (a nested loop if you want to get the average of each hour or day all at once) and change only the index of the item you want (hour or day in this case). Keep adding the current to the total then divide by the length. Commented Mar 6, 2012 at 22:08

2 Answers 2

1

You should be able to use these functions to get the needed averages:

def hourly_average(values, month, day, hour):
  hour_data = values[month][day][hour]
  # extract the values for every minute in the specified hour.
  minute_values = [hour_data[min] for min in xrange(0,60)]
  return sum(minute_values)/60

def daily_average(values, month, day):
  # extract the averages for every hour in the specified day.
  hour_values = [hourly_average(vales,month,day,hour) for hour in xrange(0,24)]
  # the average of the averages of the equally weighted parts is the average 
  # of the thing itself (?).
  return sum(hour_values)/24
Sign up to request clarification or add additional context in comments.

Comments

1

You'll be interested in using numpy.

Something like this becomes as simple as:

import numpy

data_as_numpy_array = numpy.array(original_data)

hourly_averages = numpy.average(data_as_numpy_array, 3)

daily_averages = numpy.average(hourly_averages, 2)

In the second two lines, the second argument is the axis along which you wish to average. Here 3 is the axis of the minute data, and two the axis of the hour data.

You may also be interested in installing pylab and ipython. Pylab emulates the graphing/visualisation functionality of Matlab, and ipython is an enhanced interpreter with tab-completion and full command input (and output) history amongst other things.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.