Search code examples
pythonnumpybinaryinteger

Converting numpy array (or python list) to integer


I THINK BELOW QUESTION IS NOT POSSIBLE TO CALCULATE, SIMPLY BECAUSE 2^250 to BIG TO CALCULATE.


I have a numpy array, which has a shape of (1000,250) with binary values and I want to convert the whole array to integer values;

>>>in_[0]
array([1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1,
       0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1,
       0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0,
       1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0,
       1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1,
       0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0,
       0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1,
       1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1,
       1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0,
       1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1,
       1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1,
       1, 1, 0, 0, 1, 1, 0, 1], dtype=int64)


>>>in_.shape
(1000, 250)

What is expected:

>>>out
array([<big-value>], [<big-value>], [<big-value>], ...)
# array of size (1000,1)

Last element of the in_[0] is the 2^0*element_value:

...1, 1, 0, 1], dtype=int64)
# ... + 2^3*1 + 2^2*1 + 2^1*0 + 2^0*1]

Solution

  • You can use packbits:

    a = np.array([0, 1, 1, 1])
    
    out = np.packbits(np.pad(a, (8-len(a), 0)))
    
    # or
    out = np.packbits(a[::-1], bitorder='little')
    

    Output: array([7], dtype=uint8)

    Or manually performing the computation with powers of 2:

    out = ((2**np.arange(len(a)))*a[::-1]).sum()
    

    Output: 7

    using pure python:
    a = in_[0]
    out = sum(2**i for i, x in enumerate(a[::-1]) if x)
    

    Output with the updated example:

    1351989656832611222849160586388676483515836127975177046080313769140774859213
    

    For the whole array, which should be reasonably fast for (1000, 256):

    out = [sum(2**i for i, x in enumerate(a[::-1]) if x) for a in in_]