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]]]
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python - Read blob object in python using wand library
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- Python | Merge Python key values to list
- Python | PRAW - Python Reddit API Wrapper
- Reading Python File-Like Objects from C | Python
- twitter-text-python (ttp) module - Python
- Important differences between Python 2.x and Python 3.x with examples
- MySQL-Connector-Python module in Python
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Visualizing O(n) using Python
- Use of min() and max() in Python
- Python vs PHP
- Python if else
- What is CGI 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 firstname.lastname@example.org. 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.