Jul-24-2020, 06:04 AM
Hi there, I have a homework where I need to create data bins using a dictionary. The instructions are:
Complete the specification of the bin_data function.
This is what I have so far:
Complete the specification of the bin_data function.
- bin_data(data, binsize) should take a list of floats (stored in the variable data) and a float (stored in the variable binsize) and output a dictionary representing the binned data.
- The output of the function is a dictionary whose keys represent the bins and whose values are ints indicating the number of data items in each bin.
- The bins should start from the smallest value in data and increase in multiples of binsize. So, if the smallest value in the dataset was 0.0 and the binsize was 5.0 then the bins would represent the ranges 0.0 - 5.0; 5.0 - 10.0; 10.0 - 15.0; etc. There should be no bins above the largest value in data. All bins between the smallest and largest values should be included - if they do not contain any data items, then they should have a count of 0.
- Bin ranges should be considered inclusive of their minimum value, and exclusive of their maximum. So 4.99 should be placed in the bin corresponding to the range 0.0 - 5.0, but 5.0 should be placed in the bin corresponding to the range 5.0 - 10.0.
This is what I have so far:
def bin_data(data, binsize): bins = {} data.sort() minBinSize = data[0] maxBinSize = binsize while minBinSize < data[-1] for item in data: bins.append((minBinSize, minBinSize + binsize)) lowerBound += binsize pass