Overview:
-
The numpy.packbits() function creates integers of type numpy.uint8 from the rows and columns of an ndarray. The reverse is provided by the numpy.unpackbits().
-
If the number of bits are less than eight in a row or column zeros are added to the right.
-
If the axis parameter is 1 bits are taken along the rows. Each byte in a row is packed into an numpy.uint8. Assuming there are 10 bytes in a row - first 8 bits will form an integer and the next two bits will form another integer. Since there are only two bits at the end, six zeroes will be added to the right.
-
If the axis parameter is 0 the uint objects are formed by taking the bits along the columns of the numpy ndarray. If there are 12 bits in a column the first 8 bits of the column go into a numpy.uint8 object. The remaining 4 bits are padded with zeros to the right and the next numpy.uint8 is formed.
Example 1:
# Example Python program that uses numpy packbits() import numpy as np a1 = np.ndarray(shape = (3, 3), # Populate array elements # Create int8 elements print("Array of int8 elements:") |
Output:
Input ndarray: [[0 0 1] [0 1 0] [5 0 0]] Shape of input ndarray: (3, 3) Data type of the input ndarray: int32 Array of int8 elements: [[ 32] [ 64] [128]] Shape of the int8 array: (3, 1) Data type of the resultant ndarray: uint8 |
Example 2 - From multiple bytes:
The example has an ndarray of shape (1,9) – one row and nine columns. The bit pattern starts with one and it alternates to zero and the pattern continues till all the nine columns of the row is filled. When numpy.packbits() is called to create integers by packing the bits along the row, it returns an ndarray containing two integers of type numpy.uint8. When numpy.packbits() is called using the same input array with 0 as the value of the axis parameter, it returns one numpy.uint8 for each column thus returning an ndarray of shape (1,9).
# Example program that uses numpy.packbits() import numpy # Create an ndarray with two bytes # Contains 2 bytes - (0 to 8 -> 1 byte, # When packed row-wise one uint8 is created for each byte # When packed column-wise one uint8 is created for each column |
Output:
[[1 0 1 0 1 0 1 0 1]] [[170 128]] [[128 0 128 0 128 0 128 0 128]] |
Example3 - packing in little endian format:
By default the numpy.packbits() interprets the bits in big endian format(a.k.a network byte order or internet byte order). The example below creates numpy.uint8 instances in little endian format by specifying the parameter value bitorder="little".
# Example Python program that use numpy.packbits() # Another ndarray with two bytes - this time on axis 0 # Populate the array print("Input array:") print("The uints packed column-wise in little endian format:") print("Shape of the returned array:") print("The uints packed row-wise in little endian format:") print("Shape of the returned array:") |
Output:
Input array: [[1 0] [0 1] [1 0] [0 1] [1 0] [0 1] [1 0] [0 1] [1 0] [0 1]] The uints packed column-wise in little endian format: [[ 85 170] [ 1 2]] Shape of the returned array: (2, 2) The uints packed row-wise in little endian format: [[1] [2] [1] [2] [1] [2] [1] [2] [1] [2]] Shape of the returned array: (10, 1) |