numpy.packbits() is another function for doing binary operations in numpy.It is used to packs the elements of a binary-valued array into bits in a uint8 array.The result is padded to full bytes by inserting zero bits at the end.
Syntax : numpy.packbits(arr, axis=None)
arr : [array_like] An array of integers or booleans whose elements should be packed to bits.
axis : [ int, optional] The dimension over which bit-packing is done.If none then packing is done in flattened array.
Return : [packed ndarray] Array of type uint8 whose elements represent bits corresponding to the logical (0 or nonzero) value of the input elements.
Code #1 :
Input array : [[[1 0 1] [0 1 0]] [[1 1 0] [0 0 1]]] Output packed array : [171 16]
Code #2 :
Input array : [[[0 0 1] [1 1 0]] [[1 0 0] [1 1 1]]] Output packed array along axis 1 : [[[ 64 64 128]] [[192 64 64]]]
- Important differences between Python 2.x and Python 3.x with examples
- Python | Index of Non-Zero elements in Python list
- Reading Python File-Like Objects from C | Python
- Python | Merge Python key values to list
- Python | Convert list to Python array
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Visualizing O(n) using Python
- max() and min() in Python
- SHA in Python
- bin() in Python
- gcd() in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.