The numpy.packbits() method is used to pack a binary-valued array into an uint8 array. It returns a fully padded ndarray with zero at the end, to the full length of an unsigned integer of 8 bits.
Note: The
packbits()method is also used to deal with numbers at the bit level.
numpy.packbits(array, /, axis=None, bitorder='big')
It takes the following argument values.
array: It can either be an integer or boolean array. The array whose values are going to be packed.axis: This tells the function how to perform packing or in which direction packing will happen. If None, the packing will happen in a flattened array. The default value is None.bitorder: It tells the order of bits. The bitorder can either be big or title. the default value for big is bitorder For instance, if the input is 3, the order will be in both cases as follows:'big': [0 0 0 0 0 0 1 1] => 0x00000011'title': [1 1 0 0 0 0 0 0] => 0x11000000It will return a NumPy ndarray of type uint8, which has the following attributes.
axis=None the returned array will be a 1-D array.# importing numpy libraryimport numpy as np# creating 2D array using array functionarr = np.array([[[1, 1, 1],[0, 1, 0]],[[-1, 1, 0],[0, 0, 1]]])print ("Input array : ", arr)# Invoking packbits() function to pack binary array to decimalprint ("\nOutput packed array : ", np.packbits(arr, axis=-1))
np.array() method.np.packbits() method takes the array & axis=-1 as argument values. The axis=-1 means packing will be performed column-wise.