In Python, what would be a clear, efficient way to count things in regions? -


i looping on objects called events. each event has particular object in it. calculating fraction of objects have particular characteristic. imagine approach being following:

for event in events:     countcars =+ 1     if event.car.isblue() true:         countcarsblue =+ 1  print("fraction of cars blue: {fraction}".format(     fraction = countcarsblue / countcars)) 

now, imagine want calculate fraction of objects have particular characteristic in regions of of object's characteristics. so, in example, counting fraction of cars blue. now, want calculate fraction of cars blue in range of car lengths 0 m 1 m, fraction of cars blue in range of car lengths 1 m 2 m, 2 m 3 m , 3 m 4 m , on.

given dealing lot of statistics , many more bins 4 bins of simple example, way structure code type of calculation, assuming constant bin width?

(would there sensible way variable bin widths?)

first, code recreate example:

import random  class event(object):     def __init__(self):         self.car = none  class car(object):     def __init__(self, isblue, length):         self._isblue = isblue         self._length = length      def isblue(self):         return self._isblue      def length(self):         return self._length      def __str__(self):         return '{} car of {} m long.'.format('blue' if self.isblue() else 'non-blue ', self.length()) 

ok, randomly create ten carobjects , add them event:

totalnumberofcars = 10 events = [] _ in range(totalnumberofcars):     car = car(random.choice([true, false]), random.randrange(5, 40)/10.)     print car     event = event()     event.car = car     events.append(event) 

for me, output follows (your output can of course different):

blue car of 0.5 m long. non-blue  car of 2.3 m long. non-blue  car of 3.8 m long. blue car of 2.1 m long. non-blue  car of 0.6 m long. blue car of 0.8 m long. blue car of 0.5 m long. blue car of 2.3 m long. blue car of 3.3 m long. blue car of 2.1 m long. 

now, if want count our events region, follows:

allbluecars = sum(1 event in events if event.car.isblue()) print "number of blue cars: {}".format(allbluecars)  maxcarlen = 4 region in zip(range(maxcarlen ), range(1, maxcarlen +1)):     minlen, maxlen = region     print "cars between {} , {} m blue:".format(minlen, maxlen)     bluecarsinregion = [str(event.car) event in events if event.car.isblue() , minlen <= event.car.length() < maxlen]     if bluecarsinregion:         print '\n'.join(['\t{}'.format(car) car in bluecarsinregion])     else:         print 'no blue cars in region'     fraction = float(len(bluecarsinregion)) / allbluecars     print "fraction of cars blue , between {} , {} m long: {}".format(minlen, maxlen, fraction)     print 

for above sample data, print:

number of blue cars: 7 cars between 0 , 1 m blue:     blue car of 0.5 m long.     blue car of 0.8 m long.     blue car of 0.5 m long. fraction of cars blue , between 0 , 1 m long: 0.428571428571  cars between 1 , 2 m blue: no blue cars in region fraction of cars blue , between 1 , 2 m long: 0.0  cars between 2 , 3 m blue:     blue car of 2.1 m long.     blue car of 2.3 m long.     blue car of 2.1 m long. fraction of cars blue , between 2 , 3 m long: 0.428571428571  cars between 3 , 4 m blue:     blue car of 3.3 m long. fraction of cars blue , between 3 , 4 m long: 0.142857142857 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -