Unpack elements of a uint8 array and trim off that many bits from the end 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. To set the number of element to unpack, use the "count" parameter. A negative number means to trim off that many bits from the end. We have set -4 for count, that means we will trim off 4 bits from the end −

res = np.unpackbits(arr, axis = 1, count = -4)
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 # A negative number means to trim off that many bits from the end # We have set -4 for count, that means we will trim off 4 bits from the end res = np.unpackbits(arr, axis = 1, count = -4) 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]
[0 0 0 0 0 1 1 0 0 0 0 1]
[0 0 0 1 1 0 1 1 0 0 1 0]]

Updated on: 17-Feb-2022

116 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements