Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

NumPy Binary operations: packbits() function

numpy.packbits() function

The packbits() function is used to pack 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.

Version: 1.15.0

Syntax:

numpy.packbits(myarray, axis=None)

Parameter:

Name Description Required /
Optional
myarray An array of integers or booleans whose elements should be packed to bits. Required
axis The dimension over which bit-packing is done. None implies packing the flattened array. Optional

Return value:

packed [ndarray]
Array of type uint8 whose elements represent bits corresponding to the logical (0 or nonzero) value of the input elements. The shape of packed has the same number of dimensions as the input (unless axis is None, in which case the output is 1-D).

Example-1: numpy.packbits() function

>>> import numpy as np
>>> x = np.array([[[1,1,0], [0,1,0]], [[1,0,1], [0,0,1]]])
>>> y = np.packbits(x, axis=-1)
>>> y
array([[[192],
        [ 64]],

       [[160],
        [ 32]]], dtype=uint8)

Python Code Editor:

Previous: right_shift()
Next: unpackbits()