Friday, September 5, 2025
HomeLanguagesnumpy.unpackbits() in Python

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)


Output :

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) 


Output :

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]]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS