numpy boolean array with 1 bit entries

前端 未结 3 1000
灰色年华
灰色年华 2020-12-29 19:24

Is there a way in numpy to create an array of booleans that uses just 1 bit for each entry?

The standard np.bool type is 1 byte, but this way I use 8 ti

相关标签:
3条回答
  • 2020-12-29 19:38

    You might like to take a look at bitstring (documentation here).

    If you create a ConstBitArray or ConstBitStream from a file then it will use mmap and not load it into memory. In this case it won't be mutable so if you want to make changes it will have to be loaded in memory.

    For example to create without loading into memory:

    >>> a = bitstring.ConstBitArray(filename='your_file')
    

    or

    >>> b = bitstring.ConstBitStream(a_file_object)
    
    0 讨论(0)
  • 2020-12-29 19:42

    To do this you can use numpy's native packbits and unpackbits. The first function is straight-forward to use, but to reconstruct you will need additional manipulations. Here is an example:

    import numpy as np
    # original boolean array
    A1 = np.array([
        [0, 1, 1, 0, 1],
        [0, 0, 1, 1, 1],
        [1, 1, 1, 1, 1],
    ], dtype=np.bool)
    
    # packed data
    A2 = np.packbits(A1, axis=None)
    
    # checking the size
    print(len(A1.tostring())) # 15 bytes
    print(len(A2.tostring())) #  2 bytes (ceil(15/8))
    
    # reconstructing from packed data. You need to resize and reshape
    A3 = np.unpackbits(A2, axis=None)[:A1.size].reshape(A1.shape).astype(np.bool)
    
    # and the arrays are equal
    print(np.array_equal(A1, A3)) # True
    
    0 讨论(0)
  • 2020-12-29 19:42

    You want a bitarray:

    efficient arrays of booleans -- C extension

    This module provides an object type which efficiently represents an array of booleans. Bitarrays are sequence types and behave very much like usual lists. Eight bits are represented by one byte in a contiguous block of memory. The user can select between two representations; little-endian and big-endian. All of the functionality is implemented in C. Methods for accessing the machine representation are provided. This can be useful when bit level access to binary files is required, such as portable bitmap image files (.pbm). Also, when dealing with compressed data which uses variable bit length encoding, you may find this module useful...

    0 讨论(0)
提交回复
热议问题