You don't really need to implement an algorithm to achieve this. There are a few tools that will do this for you.
You can get the data assigned to buckets for further processing using Pandas, or simply count how many values fall into each bucket using NumPy.
Assign to buckets
You just need to create a Pandas DataFrame with your data and then call the handy cut
function, which will put each value into a bucket/bin of your definition. From the documentation:
Use cut
when you need to segment and sort data values into bins.
In [1]: import pandas as pd
In [2]: import numpy as np # to create dummy data
Create some dummy data, put it in a dataframe and define the bins:
In [3]: data = np.random.randint(low=1, high=10001, size=1000)
In [4]: df = pd.DataFrame(data=data, columns=["data"])
In [5]: bins = np.array([1,5,25,50,150,250,1000,5000,10000])
Pass the data, along with the bin definitions to the cut
function and assign it back as a new column in the dataframe:
In [6]: df["bucket"] = pd.cut(df.data, bins)
You can then inspect the first few rows to see that the values have now been labelled with the relevant bucket:
In [7]: df.head()
Out[7]:
data bucket
0 8754 (5000, 10000]
1 2970 (1000, 5000]
2 6778 (5000, 10000]
3 2550 (1000, 5000]
4 5226 (5000, 10000]
Counting how many in each bucket
Here is an example using NumPy, to get an idea of the distribution, as a histogram.
Using the data
and bins
as defined above, we pass them to the numpy histogram
function, which will count how many data points fall into each bin:
In [8]: np.histogram(data, bins)
Out[8]:
(array([ 0, 2, 1, 8, 6, 61, 417, 505]),
array([ 1, 5, 25, 50, 150, 250, 1000, 5000, 10000]))
Where the first row tells you how many values fell into each bin, and the second row confirms the bins used.
You can get your dictionary of data into the same form as my dummy data above (into a numpy array) by doing this:
data = np.array([v for v in your_dict.values()])