Search code examples
pythoncombinatoricspython-itertools

binning data and inclusive result


Suppose I've binned some data in a structure like this:

data = {(1,1): [...] # list of float,
        (1,2): [...],
        (1,3): [...],
        (2,1): [...],
        ... }

here I've only two axis for the binning, but suppose I've N of them. Now suppose for example I have N=3 axis and I want the data where the second bin is 1, so I want a function

(None, 1, None) -> [(1, 1, 1), (1, 1, 2), (1, 1, 3), ...
                    (2, 1, 1), (2, 1, 2), (2, 1, 3), ...]

so I can use itertools.chain for the result

you know the range of every axis from:

axes_ranges = [(1, 10), (1, 8), (1, 3)]

other examples:

(None, 1, 2) -> [(1, 1, 2), (2, 1, 2), (3, 1, 2), ...]
(None, None, None) -> all the combinations
(1,2,3) -> [(1,2,3)]

Solution

  • Seems very much like you reinvent the wheel. What you probably want to use is numpy.ndarray:

        import numpy as np
        >>> x = np.arange(0,27)
        >>> x
        array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
        17, 18, 19, 20, 21, 22, 23, 24, 25, 26])
        >>> x.reshape(3,3,3)
        array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],
    
        [[ 9, 10, 11],
         [12, 13, 14],
         [15, 16, 17]],
    
        [[18, 19, 20],
         [21, 22, 23],
         [24, 25, 26]]])
    
        >>> x[0]
        array([[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]])
        >>> x[:,1,:]
        array([[ 3,  4,  5],
        [12, 13, 14],
        [21, 22, 23]])
        >>> x[:,1,1]
        array([ 4, 13, 22])
    

    This can have N dimensions. In the example the indexing is threedimensional, you can see it as a cube with x[a,b,c] = x[layer,row,column]. Using a ":" as index simply means "all"