Histogram counting in Python
1 Code from class
import random
# generate random data using a uniform distribution
# fake dataset that represents a number of vehicles per day passing at one point on the highway
# you collected data for a year
# you would have 365 integer numbers
num_cars = []
for i in range(365):
num_cars.append(random.randint(500, 1000))
# visualize num_cars?
# day: 1, 2, 3, 4, 5, ..., 365
# 501, 560, 900, 932, 514, ..., 657
# bin i=0: T, F, F, F, T, ..., F
# bin i=1: F, T, F, F, F, ..., F
# bin i=2: ...
# bin i=3: ...
# ...
# bin i=9: ...
# let's implement a counting algorithm for generating histograms
num_bins = 10 # bin width is 50 because (1000 - 500) / 10 = 50; you'll have to count the number of vehicles between 500 and 549, between 550 and 599, between 600 and 649, ...
min_cars = min(num_cars)
max_cars = max(num_cars)
range_cars = max_cars - min_cars
delta_cars = range_cars / num_bins
# f(i): f(0)=500, f(1)=550, f(2)=600, ..., f(9)=950
# g(i): g(0)=550, g(1)=600, g(2)=650, ..., g(9)=1000
count_days = []
count_cars = [] # number of cars across different days in each bin
for i in range(num_bins): # we already know how many times we have to count; range(10) = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
sta_cars = min_cars + delta_cars * i # 500 for i=0, 550 for i=1, 600 for i=2, ... inclusively
end_cars = sta_cars + delta_cars # 550 for i=0, 600 for i=1, 650 for i=2, ... exclusively except for the last end_cars (1000)
count_days.append(0)
count_cars.append(0) # create count_cars[i] = 0
for j in range(len(num_cars)):
daily_cars = num_cars[j]
if daily_cars >= sta_cars and daily_cars < end_cars:
count_days[i] = count_days[i] + 1
count_cars[i] = count_cars[i] + 1
2 Homework: Debug the counting algorithm
- Debug the above code so that
sum(count_cars) == sum(num_cars)
. - Handle the last bin for
i = num_bins - 1
such thatmax_cars
can be counted in the last bin. We will include days withmax_cars
in the last bin. Fix bothcount_days
andcount_cars
.
Submit FirstLastname_histocount.py
.