numpy.unpackbits#

numpy.unpackbits(a,/,axis=None,count=None,bitorder='big')#

Unpacks elements of a uint8 array into a binary-valued output array.

Each element ofa represents a bit-field that should be unpackedinto a binary-valued output array. The shape of the output array iseither 1-D (ifaxis isNone) or the same shape as the inputarray with unpacking done along the axis specified.

Parameters:
andarray, uint8 type

Input array.

axisint, optional

The dimension over which bit-unpacking is done.None implies unpacking the flattened array.

countint or None, optional

The number of elements to unpack alongaxis, provided as a wayof undoing the effect of packing a size that is not a multipleof eight. A non-negative number means to only unpackcountbits. A negative number means to trim off that many bits fromthe end.None means to unpack the entire array (thedefault). Counts larger than the available number of bits willadd zero padding to the output. Negative counts must notexceed the available number of bits.

bitorder{‘big’, ‘little’}, optional

The order of the returned bits. ‘big’ will mimic bin(val),3=0b00000011=>[0,0,0,0,0,0,1,1], ‘little’ will reversethe order to[1,1,0,0,0,0,0,0].Defaults to ‘big’.

Returns:
unpackedndarray, uint8 type

The elements are binary-valued (0 or 1).

See also

packbits

Packs the elements of a binary-valued array into bits in a uint8 array.

Examples

>>>importnumpyasnp>>>a=np.array([[2],[7],[23]],dtype=np.uint8)>>>aarray([[ 2],       [ 7],       [23]], dtype=uint8)>>>b=np.unpackbits(a,axis=1)>>>barray([[0, 0, 0, 0, 0, 0, 1, 0],       [0, 0, 0, 0, 0, 1, 1, 1],       [0, 0, 0, 1, 0, 1, 1, 1]], dtype=uint8)>>>c=np.unpackbits(a,axis=1,count=-3)>>>carray([[0, 0, 0, 0, 0],       [0, 0, 0, 0, 0],       [0, 0, 0, 1, 0]], dtype=uint8)
>>>p=np.packbits(b,axis=0)>>>np.unpackbits(p,axis=0)array([[0, 0, 0, 0, 0, 0, 1, 0],       [0, 0, 0, 0, 0, 1, 1, 1],       [0, 0, 0, 1, 0, 1, 1, 1],       [0, 0, 0, 0, 0, 0, 0, 0],       [0, 0, 0, 0, 0, 0, 0, 0],       [0, 0, 0, 0, 0, 0, 0, 0],       [0, 0, 0, 0, 0, 0, 0, 0],       [0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)>>>np.array_equal(b,np.unpackbits(p,axis=0,count=b.shape[0]))True
On this page