numpy.unpackbits() in Python (original) (raw)
Last Updated : 21 Feb, 2019
numpy.unpackbits()
is another function for doing binary operations in numpy. It is used to unpacks elements of a uint8 array into a binary-valued output array.
Syntax : numpy.unpackbits(arr, axis=None)
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 flattened array.Return : [unpacked ndarray] Array of type uint8 whose elements are binary-valued (0 or 1).
Code #1 :
import
numpy as geek
in_arr
=
geek.array([
171
,
16
], dtype
=
geek.uint8)
print
(
"Input array : "
, in_arr)
out_arr
=
geek.unpackbits(in_arr)
print
(
"Output unpacked array : "
, out_arr)
Output :
Input array : [171 16] Output unpacked array : [1 0 1 0 1 0 1 1 0 0 0 1 0 0 0 0]
Code #2 :
import
numpy as geek
in_arr
=
geek.array([[
64
,
128
], [
17
,
25
]], dtype
=
geek.uint8)
print
(
"Input array : "
, in_arr)
out_arr
=
geek.unpackbits(in_arr, axis
=
0
)
print
(
"Output unpacked array along axis 0 : "
, out_arr)
Output :
Input array : [[ 64 128] [ 17 25]] Output unpacked array along axis 0 : [[0 1] [1 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [1 1] [0 1] [0 0] [0 0] [1 1]]
Similar Reads
- numpy.signbit() in Python numpy.signbit(array, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None) : This mathematical function helps user to element - wise check whether the signbit is set or not. Parameters : array : [array_like]Input array or object whose elements, we need to check. out : [ndarray, 2 min read
- numpy.take() in Python The numpy.take() function returns elements from array along the mentioned axis and indices. Syntax: numpy.take(array, indices, axis = None, out = None, mode ='raise') Parameters : array : array_like, input array indices : index of the values to be fetched axis : [int, optional] axis over which we ne 2 min read
- numpy.right_shift() in Python numpy.right_shift() function is used to Shift the bits of an integer to the right. Because the internal representation of numbers is in binary format, this operation is equivalent to dividing arr1 by 2**arr2. For example, if the number is 20 and we want to 2-bit right shift then after right shift 2- 2 min read
- numpy.zeros() in Python numpy.zeros() function creates a new array of specified shapes and types, filled with zeros. It is beneficial when you need a placeholder array to initialize variables or store intermediate results. We can create 1D array using numpy.zeros(). Let's understand with the help of an example: [GFGTABS] P 2 min read
- numpy.left_shift() in Python numpy.left_shift() function is used to Shift the bits of an integer to the left. The bits are shifted to the left by appending arr2 0s(zeroes) at the right of arr1. Since the internal representation of numbers is in binary format, this operation is equivalent to multiplying arr1 by 2**arr2. For exam 2 min read
- numpy.squeeze() in Python The numpy.squeeze() is a useful Python function, which is utilized for the removal of single-dimensional elements from the shape of a NumPy array. It comes in very handy when you have to discard redundant dimensions (like a dimension with size 1) after operations that introduce extra dimensions. Bas 3 min read
- numpy.trim_zeros() in Python numpy.trim_zeros function is used to trim the leading and/or trailing zeros from a 1-D array or sequence. Syntax: numpy.trim_zeros(arr, trim) Parameters: arr : 1-D array or sequence trim : trim is an optional parameter with default value to be 'fb'(front and back) we can either select 'f'(front) and 2 min read
- numpy.reshape() in Python In Python, numpy.reshape() function is used to give a new shape to an existing NumPy array without changing its data. It is important for manipulating array structures in Python. Let's understand with an example: [GFGTABS] Python import numpy as np # Creating a 1D NumPy array arr = np.array([1, 2, 3 3 min read
- numpy.packbits() in Python numpy.packbits() is another function for doing binary operations in numpy.It is used to packs the elements of a binary-valued array into bits in a uint8 array.The result is padded to full bytes by inserting zero bits at the end. Syntax : numpy.packbits(arr, axis=None) Parameters : arr : [array_like] 2 min read
- Python | Numpy matrix.tobytes() With the help of Numpy matrix.tobytes() method, we can find the byte code for the matrix by using the matrix.tobytes() method. Syntax : matrix.tobytes() Return : Return byte code for matrix Example #1 : In this example we can see that by using matrix.tobytes() method we are able to find the byte cod 1 min read