NumPy compress selects array positions using a one dimensional condition

A condition can describe which positions to keep without listing their integer indices. NumPy compress uses a one dimensional condition to select entries or whole slices from an array. Academia Master Trader presents this English lesson for readers in Turkey, using constructed arrays to explain the relationship between a condition, an axis and the resulting shape.

True keeps a position and False leaves it out

After import numpy as np, define v = np.array([6,12,18,24]). The call np.compress([True,False,True,False],v) returns [6,18]. Each condition entry corresponds to a position in the input. The selected values retain their original order.

The condition is a positional instruction, not a list of values to find. True in the third position keeps eighteen because eighteen occupies that position. Compress does not reorder the selected positions or repeat them. To request positions in a different order, use an integer index operation such as take instead.

NumPy condition selection

A selected axis determines the size of each retained slice

Define m = np.array([[2,5,8],[11,14,17]]). The call np.compress([True,False,True],m,axis=1) returns [[2,8],[11,17]], with shape (2,2). The condition keeps the first and third columns across both rows.

The same idea applies to rows. The call np.compress([False,True],m,axis=0) returns [[11,14,17]], with shape (1,3). One True entry retains one complete row. The result still has a row axis even though only one row remains.

These examples illustrate why an axis belongs beside every condition when explaining a matrix selection. A condition attached to columns has a different meaning from one attached to rows. Read the selected axis length before interpreting the condition's positions.

An omitted axis refers to a flattened sequence

The call np.compress([False,True,False,True,False,True],m) returns [5,11,17]. With no axis argument, NumPy selects individual entries from the flattened input. It does not preserve the original two by three matrix shape.

The flattened order here is [2,5,8,11,14,17]. Writing that sequence beside the condition makes each choice explicit. The three True positions select five, eleven and seventeen in that order. No averaging or addition is involved.

A shorter condition limits the positions considered

The call np.compress([True,False],v) returns [6]. It considers only the first two positions. The later entries eighteen and twenty four are not automatically retained or covered by repeating the condition.

For a matrix, np.compress([True,False],m,axis=1) returns [[2],[11]], with shape (2,1). Again, the third column is outside the shorter condition. Matching condition length to the selected axis length can make the intended coverage easier to inspect.

A condition can be calculated from values before calling compress. For this vector, v > 12 gives [False,False,True,True], and np.compress(v > 12,v) returns [18,24]. First explain how the condition was formed, then explain which positions it retains.

Compress returns a copy. An all False condition selects no entries along the chosen axis; for example, np.compress([False,False],m,axis=0) has shape (0,3). The empty row axis does not remove the three column dimension.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. State the condition, axis and expected output shape together.