- 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 of a uint8 array into a binary-valued output array 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).
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) −
res = np.unpackbits(arr) 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). res = np.unpackbits(arr) 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 specific axis in Numpy
- Unpack elements of a uint8 array into a binary-valued output array over axis 0 in Numpy
- Pack the elements of a binary-valued Numpy array into bits in a uint8 array
- 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 array into bits in a uint8 array over specific axis in Numpy
- 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
- How to unpack array elements into separate variables using JavaScript?
- Transform a masked array into a flexibletype array in Numpy
- Repeat elements of a masked array in Numpy
- Transform a masked array into a flexibletype array with torecords() in Numpy
- Create a record array from binary data in Numpy
- Java Program to output fixed number of array elements in a line
- Unpack elements and set the unpack count larger than the available number of bits in Numpy
