numpy.packbits() in Python (original) (raw)
Last Updated : 20 Feb, 2019
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] An array of integers or booleans whose elements should be packed to bits.
axis : [ int, optional] The dimension over which bit-packing is done.If none then packing is done in flattened array.Return : [packed ndarray] Array of type uint8 whose elements represent bits corresponding to the logical (0 or nonzero) value of the input elements.
Code #1 :
import
numpy as geek
in_arr
=
geek.array([[[
1
,
0
,
1
],
`` [
0
,
1
,
0
]],
`` [[
1
,
1
,
0
],
`` [
0
,
0
,
1
]]])
print
(
"Input array : "
, in_arr)
out_arr
=
geek.packbits(in_arr)
print
(
"Output packed array : "
, out_arr)
Output :
Input array :
[[[1 0 1]
[0 1 0]]
[[1 1 0] [0 0 1]]] Output packed array : [171 16]
Code #2 :
import
numpy as geek
in_arr
=
geek.array([[[
0
,
0
,
1
],
`` [
1
,
1
,
0
]],
`` [[
1
,
0
,
0
],
`` [
1
,
1
,
1
]]])
print
(
"Input array : "
, in_arr)
out_arr
=
geek.packbits(in_arr, axis
=
1
)
print
(
"Output packed array along axis 1 : "
, out_arr)
Output :
Input array : [[[0 0 1] [1 1 0]]
[[1 0 0] [1 1 1]]] Output packed array along axis 1 : [[[ 64 64 128]]
[[192 64 64]]]
Similar Reads
- numpy.place() in Python The numpy.place() method makes changes in the array according the parameters - conditions and value(uses first N-values to put into array as per the mask being set by the user). It works opposite to numpy.extract(). Syntax: numpy.place(array, mask, vals) Parameters : array : [ndarray] Input array, w 2 min read
- numpy.pad() function in Python numpy.pad() function is used to pad the Numpy arrays. Sometimes there is a need to perform padding in Numpy arrays, then numPy.pad() function is used. The function returns the padded array of rank equal to the given array and the shape will increase according to pad_width. Syntax: numpy.pad(array, p 2 min read
- numpy.unpackbits() in Python 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. axi 2 min read
- numpy.load() in Python numpy.load() function return the input array from a disk file with npy extension(.npy). Syntax : numpy.load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding='ASCII') Parameters: file : : file-like object, string, or pathlib.Path.The file to read. File-like objects must support the 2 min read
- Python NumPy Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Besides its obvious scientific uses, Numpy can also be used as an efficient 6 min read
- numpy.floor() in Python The numpy.floor() function returns the largest integer less than or equal to each element in the input array. It effectively rounds numbers down to the nearest whole number. Let's understand with an example: [GFGTABS] Python import numpy as np a = [0.5, 1.5, 2.5, 3, 4.5, 10.1] res = np.floor(a) prin 1 min read
- NumPy Array in Python NumPy (Numerical Python) is a powerful library for numerical computations in Python. It is commonly referred to multidimensional container that holds the same data type. It is the core data structure of the NumPy library and is optimized for numerical and scientific computation in Python. Table of C 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.append() in Python numpy.append() function is used to add new values at end of existing NumPy array. This is useful when we have to add more elements or rows in existing numpy array. It can also combine two arrays into a bigger one. Syntax: numpy.append(array, values, axis = None) array: Input array. values: The value 2 min read
- 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