Jan-21-2020, 09:45 PM
Hi,
I need to use a function called 'bin_data(data, binsize)' to 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.
Bin ranges are inclusive of the lower value and exclusive of the upper value. i.e '0-4.99' etc
example output:
{'0.0 - 5.0': 4, '5.0 - 10.0': 6, '10.0 - 15.0': 4}
The function needs to be able to sort the data into the dictionary depending on what number is assigned to 'binsize' which may change.
I have managed to create a dictionary whose keys represent the bins, but i can't figure out how to sort the data in the list 'data' into the range determined by the bins, count the number of values in each bin and then append that number to the values of the dictionary.
Here's what i have so far:
I need to use a function called 'bin_data(data, binsize)' to 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.
Bin ranges are inclusive of the lower value and exclusive of the upper value. i.e '0-4.99' etc
example output:
{'0.0 - 5.0': 4, '5.0 - 10.0': 6, '10.0 - 15.0': 4}
The function needs to be able to sort the data into the dictionary depending on what number is assigned to 'binsize' which may change.
I have managed to create a dictionary whose keys represent the bins, but i can't figure out how to sort the data in the list 'data' into the range determined by the bins, count the number of values in each bin and then append that number to the values of the dictionary.
Here's what i have so far:
data = [90.0, 69.0, 58.0, 57.0, 49.0, 55.0, 52.0, 35.0, 43.0, 43.0, 50.0, 0.0, 32.0, 74.0, 48.0, 59.0, 53.0, 80.0, 61.0, 59.0, 67.0, 48.0, 65.0, 30.0, 60.0, 61.0, 37.0, 39.0, 60.0, 41.0, 53.0, 58.0, 37.0, 44.0, 80.0, 55.0, 42.0, 41.0, 26.0, 45.0, 28.0, 46.0, 33.0, 74.0, 42.0, 65.0, 80.0, 38.0, 82.0, 64.0, 33.0, 61.0, 32.0, 51.0, 34.0, 20.0, 47.0, 99.0, 51.0, 53.0, 72.0, 74.0, 22.0, 38.0, 56.0, 61.0, 92.0, 63.0, 69.0, 81.0, 30.0, 14.0, 13.0, 30.0, 30.0, 89.0, 37.0, 25.0, 7.0, 65.0, 47.0, 76.0, 41.0, 43.0, 54.0, 64.0, 78.0, 45.0, 27.0, 41.0, 25.0, 0.0, 63.0, 69.0, 48.0, 74.0, 28.0, 77.0, 47.0] data.sort() bin_size = '5' binsize = float(bin_size) binned_data = {} def bin_data(data, binsize): numberofbins = int(round((max(data) - min(data)) / binsize)+0.5)#why is it not rounding without the 0.5 for i in range(numberofbins): bin_lower = (i * binsize) bin_upper = bin_lower + binsize binlower = int(bin_lower) binupper = int(bin_upper) binrange = (str(binlower) + '-' + str(binupper)) binned_data.update({binrange: []}) bin_data(data,binsize) print(binned_data)