Overview:
-
The numpy.unpackbits() unpacks an ndarray of numpy.uint8 integers into an ndarray of zeros and ones.
-
Note that the data type of the ndarray is not numpy.bool. It remains as numpy.uint8.
-
The numpy.unpackbits() does the reverse of numpy.packbits().
Example 1:
# Example Python program that unpacks an ndarray of numpy.uint8 instances # Create an ndarray. Elements are of type numpy.uint8 # Fill the ndarray with values # Get the binary bits of the integer as an ndarray print("Bit array when axis=1:") |
Output:
Input array: [[10 15] [20 25]] Bit array when axis=1: [[0 0 0 0 1 0 1 0 0 0 0 0 1 1 1 1] [0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 1]] uint8 Bit array when axis=0: [[0 0] [0 0] [0 0] [0 0] [1 1] [0 1] [1 1] [0 1] [0 0] [0 0] [0 0] [1 1] [0 1] [1 0] [0 0] [0 1]] |
Example 2:
# Example Python program that unpacks an ndarray intVals = numpy.ndarray(shape = (2, 2), unpacked = numpy.unpackbits(intVals, bitorder = 'little') |
Output:
An ndarray of numpy.uint8 elements: [[ 1 120] [128 255]] Unpacked ndarray - least significant bits are placed first: [1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1] |