Jul-21-2020, 11:17 AM
Hi there, I need help with binning some data for a homework question.
Say I have a list, [2, 3, 4, 5, 6, 8, 10, 12, 14, 16, 19, 21], I need to sort them in bins increasing in 4's, starting with the lowest value.
The end format should be a dictionary, with how often the numbers appear inside each group:
Ideal output: {'2-6': 5, '6-10': 3, '10-14': 3, '14-18': 2, '18-22': 2}
I've written the following code but I just don't know how to count how often a number 'fits' into a bin, that is, adding the counted value to the key in the dictionary. 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.
That is, I can't figure out how to count the number of values in each bin, and then append that number to the values of the dictionary.
Can someone please help? Greatly appreciated :)
Say I have a list, [2, 3, 4, 5, 6, 8, 10, 12, 14, 16, 19, 21], I need to sort them in bins increasing in 4's, starting with the lowest value.
The end format should be a dictionary, with how often the numbers appear inside each group:
Ideal output: {'2-6': 5, '6-10': 3, '10-14': 3, '14-18': 2, '18-22': 2}
I've written the following code but I just don't know how to count how often a number 'fits' into a bin, that is, adding the counted value to the key in the dictionary. 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.
That is, I can't figure out how to count the number of values in each bin, and then append that number to the values of the dictionary.
Can someone please help? Greatly appreciated :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
def bin_data(): data = [ 2 , 3 , 4 , 5 , 6 , 8 , 10 , 12 , 14 , 16 , 19 , 21 ] data.sort() binsize = 4 binned_data = {} def bin_data(data, binsize): numberofbins = int ( round (( max (data) - min (data)) / binsize) + 0.5 ) for i in range (numberofbins): bin_lower = (i * binsize) + data[ 0 ] bin_upper = bin_lower + binsize binlower = int (bin_lower) binupper = int (bin_upper) binrange = ( str (binlower) + '-' + str (binupper)) binned_data.update({binrange: 1 }) ###Ideal Output: {'2-6': 5, '6-10': 3, '10-14': 3, '14-18': 2, '18-22': 2} ###x is count of frequency |