- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- 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 of a uint8 array into a binary-valued output array over specific axis 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.

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 axis is set using the "axis" parameter −

res = np.unpackbits(arr, axis = 1) 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 res = np.unpackbits(arr, axis = 1) 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 1 1 0 0 0 0 1 0 0 1 1] [0 0 0 1 1 0 1 1 0 0 1 0 0 0 1 1]]

- Related Articles
- Unpack elements of a uint8 array into a binary-valued output array over axis 0 in Numpy
- Unpack elements of a uint8 array into a binary-valued output array in Numpy
- Pack the elements of a binary-valued array into bits in a uint8 array over specific axis in Numpy
- Pack the elements of a binary-valued Numpy array into bits in a uint8 array over axis 1
- Pack the elements of a binary-valued Numpy array into bits in a uint8 array over negative axis
- Pack the elements of a binary-valued Numpy array into bits in a uint8 array
- 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
- Expand the shape of an array over specific axis in Numpy
- Remove axes of length one from an array over specific axis in Numpy
- Reduce a multi-dimensional array and multiply elements along specific axis in Numpy
- Reduce a multi-dimensional array and add elements along specific axis in Numpy
- Return the average of the masked array elements over axis 0 in Numpy
- Compute the maximum of the masked array elements over axis 0 in Numpy
- Compute the maximum of the masked array elements over axis 1 in Numpy