NumPy unpackbits expands unsigned bytes into explicit zero and one entries
A stored byte can represent eight logical positions rather than one ordinary measurement. NumPy unpackbits exposes those positions as an array containing zeros and ones. Academia Master Trader presents this English lesson for readers in Cabo Verde, using constructed unsigned bytes to explain bit order, output length and unpacking along a chosen axis.
The input dtype identifies unsigned bytes
After import numpy as np, define stored = np.array([13],dtype=np.uint8). The call np.unpackbits(stored) returns [0,0,0,0,1,1,0,1].
The output entries have dtype uint8, but each entry is now either zero or one. A single input byte has expanded into eight output entries.
Under the default big bit order, those positions correspond to weights 128, 64, 32, 16, 8, 4, 2 and 1. The active weights eight, four and one add to thirteen.
Use the required unsigned eight-bit input type. An array of arbitrary integers is not the same representation, even when its displayed numbers look small enough to fit in a byte.

Bit order controls which position comes first
The call np.unpackbits(stored,bitorder='little') returns [1,0,1,1,0,0,0,0]. The least significant position appears first rather than last.
Both sequences describe the same stored byte under different position conventions. The choice does not change thirteen into a different input value or sort the resulting bits.
Match bit order to the convention used by the producer of the packed data. A sequence containing the expected number of entries can still be interpreted incorrectly when significance runs in the wrong direction.
Count controls how much output is retained
For packed = np.array([160],dtype=np.uint8), the default output is [1,0,1,0,0,0,0,0]. The call np.unpackbits(packed,count=3) instead returns [1,0,1].
This is useful when only three logical entries were originally packed and trailing zeros merely completed the byte. The original length must come from retained metadata; the byte does not mark which zeros were padding.
A negative count removes positions from the end. Here count=-5 also leaves [1,0,1]. A positive count of ten produces the eight available bits followed by two extra zeros.
Those added zeros are output padding, not newly recovered source information. Count controls length rather than discovering how many meaningful entries the producer intended.
Axis determines which dimension expands
Define rows = np.array([[128],[1]],dtype=np.uint8). The call np.unpackbits(rows,axis=1) returns two rows of eight entries, with shape (2,8).
The first row starts with one followed by seven zeros. The second starts with seven zeros followed by one. Each input row remains separate while its byte dimension expands.
With axis omitted, the same two input bytes produce one flat array of sixteen entries. Record the intended output grouping instead of assuming that unpacking preserves every original dimension automatically.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Keep the byte dtype, bit order, original logical length and unpacking axis together when reading packed data.