numpy.unpackbits() in Python
numpy.unpackbits() is another function for doing binary operations in numpy. It is used to unpacks elements of a uint8 array into a binary-valued output array.
Syntax : numpy.unpackbits(arr, axis=None)
Parameters :
arr : [array_like ndarray] An uint8 type array whose elements should be unpacked.
axis : The dimension over which unpacking is done.If none then unpacking is done in flattened array.
Return : [unpacked ndarray] Array of type uint8 whose elements are binary-valued (0 or 1).
Code #1 :
# Python program explaining # numpy.unpackbits() function # importing numpy import numpy as geek # creating input array using # array function in_arr = geek.array([171, 16], dtype = geek.uint8) print ("Input array : ", in_arr) # unpacking elements of an array # using unpackbits() function out_arr = geek.unpackbits(in_arr) print ("Output unpacked array : ", out_arr) |
Input array : [171 16] Output unpacked array : [1 0 1 0 1 0 1 1 0 0 0 1 0 0 0 0]
Code #2 :
# Python program explaining # numpy.unpackbits() function # importing numpy import numpy as geek # creating input array using # array function in_arr = geek.array([[ 64, 128], [ 17, 25]], dtype = geek.uint8) print ("Input array : ", in_arr) # unpacking elements of an array # using unpackbits() function out_arr = geek.unpackbits(in_arr, axis = 0) print ("Output unpacked array along axis 0 : ", out_arr) |
Input array : [[ 64 128] [ 17 25]] Output unpacked array along axis 0 : [[0 1] [1 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [1 1] [0 1] [0 0] [0 0] [1 1]]
Recommended Posts:
- Reading Python File-Like Objects from C | Python
- Python | Index of Non-Zero elements in Python list
- Important differences between Python 2.x and Python 3.x with examples
- Python | Convert list to Python array
- Python | Merge Python key values to list
- Python | Add Logging to a Python Script
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Any & All in Python
- zip() in Python
- set add() in python
- SQL using Python | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.


