- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Unpack elements and set the unpack count larger than the available number of bits in Numpy

To unpack elements of a uint8 array into a binary-valued output array, use the **numpy.unpackbits()** method in Python Numpy. The result is binary-valued (0 or 1). The axis is the dimension over which bit-unpacking is done. The axis is set using the "axis" parameter. To set the number of elements to unpack, use the "count" parameter.

Each element of the input array represents a bit-field that should be unpacked into a binary-valued output array. The shape of the output array is either 1-D (if axis is None) or the same shape as the input array with unpacking done along the axis specified.

The axis is the dimension over which bit-unpacking is done. None implies unpacking the flattened array. The count parameter is the number of elements to unpack along 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.

The order is the order of the returned bits. 'big' will mimic bin(val), 3 = 0b00000011 ⇒ [0, 0, 0, 0, 0, 0, 1, 1], 'little' will reverse the order to [1, 1, 0, 0, 0, 0, 0, 0]. Defaults to 'big'.

## Steps

At first, import the required library −

import numpy as np

Create a 2d array. We have set uint8 type using the "dtype" parameter −

arr = np.array([[4, 8], [6, 19], [27, 35]], dtype=np.uint8)

Displaying our array −

print("Array...

",arr)

Get the datatype −

print("

Array datatype...

",arr.dtype)

Get the dimensions of the Array −

print("

Array Dimensions...

",arr.ndim)

Get the shape of the Array −

print("

Our Array Shape...

",arr.shape)

Get the number of elements of the Array −

print("

Elements in the Array...

",arr.size)

To unpack elements of a uint8 array into a binary-valued output array, use the numpy.unpackbits() method. The result is binary-valued (0 or 1). The axis is the dimension over which bit-unpacking is done. The axis is set using the "axis" parameter. To set the number of element to unpack, use the "count" parameter. We have set count 25 i.e. Counts larger than the available number of bits will add zero padding to the output −

res = np.unpackbits(arr, axis = 1, count = 25) print("

Result...

",res)

## Example

import numpy as np # Create a 2d array # We have set uint8 type using the "dtype" parameter arr = np.array([[4, 8], [6, 19], [27, 35]], dtype=np.uint8) # Displaying our array print("Array...

",arr) # Get the datatype print("

Array datatype...

",arr.dtype) # Get the dimensions of the Array print("

Array Dimensions...

",arr.ndim) # Get the shape of the Array print("

Our Array Shape...

",arr.shape) # Get the number of elements of the Array print("

Elements in the Array...

",arr.size) # To unpack elements of a uint8 array into a binary-valued output array, use the numpy.unpackbits() method in Python Numpy # The result is binary-valued (0 or 1). # The axis is the dimension over which bit-unpacking is done. # The axis is set using the "axis" parameter # To set the number of element to unpack, use the "count" parameter # We have set count 25 i.e. Counts larger than the available number of bits will add zero padding to the output res = np.unpackbits(arr, axis = 1, count = 25) print("

Result...

",res)

## Output

Array... [[ 4 8] [ 6 19] [27 35]] Array datatype... uint8 Array Dimensions... 2 Our Array Shape... (3, 2) Elements in the Array... 6 Result... [[0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0] [0 0 0 1 1 0 1 1 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0]]

- Related Articles
- Unpack elements of a uint8 array and only unpack some bits in Numpy
- Unpack elements of a uint8 array and trim off that many bits from the end in Numpy
- Unpack elements of a uint8 array into a binary-valued output array in Numpy
- unpack() function in PHP
- Unpack elements of a uint8 array into a binary-valued output array over specific axis in Numpy
- Unpack elements of a uint8 array into a binary-valued output array over axis 0 in Numpy
- How to unpack array elements into separate variables using JavaScript?
- Count the number of masked elements in Numpy
- Shift the bits of an integer to the left and set the count of shifts as an array in Numpy
- How to unpack using star expression in Python?
- Count the number of masked elements along axis 0 to count in Numpy
- Count the number of masked elements along axis 1 in Numpy
- Check if bits of a number has count of consecutive set bits in increasing order in Python
- Shift the bits of integer array elements to the left in Numpy
- Shift the bits of integer array elements to the right in Numpy