numpy.packbits() – The NumPy packbits Python Function

Introduction to NumPy packbits

In NumPy, numpy.packbits() is another function for doing binary operations. In an uint8 array, it is use to pack the items of a binary-valued array into bits. By introducing zero bits at the end, the result is pade to full bytes.

Syntax:

numpy.packbits(arr, axis=None, bitorder='big')

Parameters:

  • 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 a flattened array.
  • bitorder: [‘big’, ‘little’, optional] The order of the input bits. ‘big’ will mimic bin(val), [0, 0, 0, 0, 0, 0, 1, 1] => 3 = 0b00000011, ‘little’ will reverse the order so [1, 1, 0, 0, 0, 0, 0, 0] => 3. Defaults to ‘big’. New in version 1.17.0.

Return: [packed ndarray] Array of type uint8 whose elements represent bits corresponding to the logical (0 or nonzero) value of the input elements.

Code Examples of NumPy packbits

Example 01: Packed array along default axis

# welcome to softhunt.net
# Python program explaining
# numpy.packbits() function

# importing numpy
import numpy as np

# creating input array using
# array function
arr = np.array([[[1, 1, 1],[1, 0, 0]],[[0, 1, 0],[0, 0, 1]]])

print ("Input array : \n", arr)

# packing elements of an array
# using packbits() function
ans = np.packbits(arr)

print ("Output packed array : ", ans)

Output:

Input array : 
 [[[1 1 1]
  [1 0 0]]

 [[0 1 0]
  [0 0 1]]]
Output packed array :  [241  16]

Example 02: Packed array along axis 1

# welcome to softhunt.net
# Python program explaining
# numpy.packbits() function

# importing numpy
import numpy as np

# creating input array using
# array function
arr = np.array([[[1, 1, 1],[1, 0, 0]],[[0, 1, 0],[0, 0, 1]]])

print ("Input array : \n", arr)

# packing elements of an array
# using packbits() function
ans = np.packbits(arr, axis = 1)

print ("Output packed array : \n", ans)

Output:

Input array : 
 [[[1 1 1]
  [1 0 0]]

 [[0 1 0]
  [0 0 1]]]
Output packed array : 
 [[[192 128 128]]

 [[  0 128  64]]]

FAQs

What does numpy.unpackbit() do?

numpy.unpackbits() is another binary operations function in NumPy. It converts the elements of an uint8 array to a binary-valued output array.

What is the difference between NumPy and Python?

  • Unlike Python lists, NumPy arrays have a fixed size when created (which can grow dynamically). When the size of a ndarray is change, a new array is create and the old one is delete.
  • The elements of a NumPy array must all be of the same data type and so have the same memory usage. The exception is that arrays of (Python, including NumPy) objects can be use, allowing for arrays of various sizes.
  • NumPy arrays make it easier to do complex mathematical and other operations on massive amounts of data. Such actions are often perform more quickly and with less code than utilizing Python’s built-in sequences.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. NumPy Shift Functions

Leave a Comment