# numpy.unpackbits() – The NumPy unpackbits Python Function

## Introduction to NumPy unpackbits

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

Syntax:

`numpy.unpackbits(arr, axis=None, count=None, bitorder='big')`

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 a flattened array.
• count: [int or none, optional] The number of elements to unpack along an axis, provided as a way of undoing the effect of packing a size that is not a multiple of eight. A non-negative number means to only unpack count bits. A negative number means to trim off that many bits from the end. None means to unpack the entire array (the default). Counts larger than the available number of bits will add zero padding to the output. Negative counts must not exceed the available number of bits. New in version 1.17.0.
• 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: [unpacked ndarray] Array of type uint8 whose elements are binary-valued (0 or 1).

## Code Examples of NumPy unpackbits

Example 01: Unpacked array along default axis

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

# importing numpy
import numpy as np

# creating input array using
# array function
arr = np.array([122, 15], dtype = np.uint8)
print ("Input array : ", arr)

# unpacking elements of an array
# using unpackbits() function
ans = np.unpackbits(arr)

print ("Output unpacked array : ", ans)``````

Output:

```Input array :  [122  15]
Output unpacked array :  [0 1 1 1 1 0 1 0 0 0 0 0 1 1 1 1]```

Example 02: Unpacked array along axis 0

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

# importing numpy
import numpy as np

# creating input array using
# array function
arr = np.array([[122, 15],[ 34, 12]], dtype = np.uint8)
print ("Input array : ", arr)

# unpacking elements of an array
# using unpackbits() function
ans = np.unpackbits(arr, axis=0)

print ("Output unpacked array : \n", ans)``````

Output:

```Input array :  [[122  15]
[ 34  12]]
Output unpacked array :
[[0 0]
[1 0]
[1 0]
[1 0]
[1 1]
[0 1]
[1 1]
[0 1]
[0 0]
[0 0]
[1 0]
[0 0]
[0 1]
[0 1]
[1 0]
[0 0]]```

Example 03: Unpacked array along axis 1

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

# importing numpy
import numpy as np

# creating input array using
# array function
arr = np.array([[122, 15],[ 34, 12]], dtype = np.uint8)
print ("Input array : ", arr)

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

print ("Output unpacked array : \n", ans)``````

Output:

```Input array :  [[122  15]
[ 34  12]]
Output unpacked array :
[[0 1 1 1 1 0 1 0 0 0 0 0 1 1 1 1]
[0 0 1 0 0 0 1 0 0 0 0 0 1 1 0 0]]```

## FAQs

### What does numpy.packbits() do?

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 padded to full bytes.

### Why NumPy is faster than the list?

NumPy is a Python core module for manipulating and operating on high-level mathematical functions, multi-dimensional arrays, linear algebra, Fourier transformations, and random number capabilities, among other things. It offers Python with tools for integrating C, C++, and Fortran programs. NumPy is mostly use for scientific computing in Python.

## 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.packbits() – The NumPy packbits Python Function