Wana_B3_Nerd
Wana_B3_Nerd

Reputation: 643

How to create bins

Currently I have bins created using the following code:

bin_volumes = [((i+1)**3 - i**3) * bin_width**3 *4 * np.pi/3 for i in range(num_bins)]

format of bins :

[(0.0, 0.10000000000000001), (0.10000000000000001, 0.20000000000000001), (0.20000000000000001, 0.30000000000000004), (0.30000000000000004, 0.40000000000000002), (0.40000000000000002, 0.5), (0.5, 0.60000000000000009), (0.60000000000000009, 0.70000000000000007), (0.70000000000000007, 0.80000000000000004), (0.80000000000000004, 0.90000000000000002), (0.90000000000000002, 1.0), (1.0, 1.1000000000000001), (1.1000000000000001, 1.2000000000000002), (1.2000000000000002, 1.3), (1.3, 1.4000000000000001), (1.4000000000000001, 1.5), (1.5, 1.6000000000000001), (1.6000000000000001, 1.7000000000000002), (1.7000000000000002, 1.8), (1.8, 1.9000000000000001), (1.9000000000000001, 2.0), (2.0, 2.1000000000000001), (2.1000000000000001, 2.2000000000000002), (2.2000000000000002, 2.3000000000000003), (2.3000000000000003, 2.4000000000000004), (2.4000000000000004, 2.5), (2.5, 2.6000000000000001), (2.6000000000000001, 2.7000000000000002), (2.7000000000000002, 2.8000000000000003), (2.8000000000000003, 2.9000000000000004), (2.9000000000000004, 3.0), (3.0, 3.1000000000000001), (3.1000000000000001, 3.2000000000000002), (3.2000000000000002, 3.3000000000000003), (3.3000000000000003, 3.4000000000000004), (3.4000000000000004, 3.5), (3.5, 3.6000000000000001), (3.6000000000000001, 3.7000000000000002), (3.7000000000000002, 3.8000000000000003), (3.8000000000000003, 3.9000000000000004)

Format of data:

3.615                                                                                                                                                                                                          
4.42745271008                                                                                                                                                                                                  
2.55619101399                                                                                                                                                                                                  
2.55619101399                                                                                                                                                                                                  
2.55619101399                                                                                                                                                                                                  
4.42745271008                                                                                                                                                                                                  
3.615
2.55619101399
4.42745271008
5.71581687075
5.71581687075
3.615
2.55619101399
2.55619101399
2.55619101399
2.55619101399
2.55619101399
2.55619101399

I want to be able to add one every time a data point fits in the range of a bin so that I can gather the "frequency" of each range and later use that to create a graph.

Code for creating data:

for b in range(2047):
    for a in range(b+1,2048):
        vector1 = (l[b][0],l[b][1],l[b][2])
        vector2 = (l[a][0],l[a][1],l[a][2])

        x = vector1
        y = vector2
        vector3 = list(np.array(x) - np.array(y))

        dotProduct = reduce( operator.add, map( operator.mul, vector3, vector3))

        dp = dotProduct**.5
        data = dp

Upvotes: 0

Views: 1822

Answers (1)

Brionius
Brionius

Reputation: 14098

This creates a list of tuples, each of which defines the bounds of a bin:

bins = [(i*bin_width, (i+1)*bin_width) for i in range(num_bins)]

If you have a list of data values, like this:

data = [0.7, 2.8, 1.3]

Then you can count how many fall into each bin like this:

[sum([(value >= low) and (value < high) for value in data]) for low, high in bins]

Upvotes: 1

Related Questions