NumPy ndarray.compress()
The numpy.ndarray.compress()
method is used to filter elements of an array along a specified axis based on a boolean condition.
It returns a new array containing only the elements where the condition is True
.
Syntax
ndarray.compress(condition, axis=None, out=None)
Parameters
Parameter | Type | Description |
---|---|---|
condition | 1D array of bool | A boolean array determining which elements to keep. The length must match the size of the specified axis . |
axis | int, optional | The axis along which filtering is applied. If None , the condition is applied to the flattened array. |
out | ndarray, optional | Optional output array to store results. Must have a compatible shape. |
Return Value
Returns a new NumPy array containing only elements where the condition
is True
.
If an axis is specified, elements are selected along that axis.
Examples
1. Filtering Elements from a 1D Array
In this example, we filter values from a one-dimensional array based on a boolean condition.
import numpy as np
# Create a 1D array
arr = np.array([10, 20, 30, 40, 50])
# Define a condition: Keep elements greater than 25
condition = arr > 25
# Apply compress function
result = arr.compress(condition)
# Print the result
print(result) # Output: [30 40 50]
Output:
[30 40 50]
Since the condition is applied element-wise, only elements greater than 25
are included in the output.
2. Filtering Elements Along a Specific Axis
Here, we apply compress()
along an axis in a 2D array.
import numpy as np
# Create a 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Define a boolean condition for rows
condition = np.array([True, False, True])
# Apply compress along axis 0 (row-wise)
result = arr.compress(condition, axis=0)
# Print the result
print(result)
Output:
[[1 2 3]
[7 8 9]]
Since condition = [True, False, True]
, the function keeps only the first and third rows, discarding the second row.
3. Filtering Columns Using the axis
Parameter
We use compress()
to filter specific columns from a 2D array.
import numpy as np
# Create a 2D array
arr = np.array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])
# Define a boolean condition for columns
condition = np.array([False, True, True])
# Apply compress along axis 1 (column-wise)
result = arr.compress(condition, axis=1)
# Print the result
print(result)
Output:
[[20 30]
[50 60]
[80 90]]
Here, we filter columns based on condition = [False, True, True]
, keeping only the second and third columns.
4. Using compress()
on a Flattened Array
If no axis is specified, the method operates on the flattened array.
import numpy as np
# Create a 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Define a condition on the flattened array
condition = arr.flatten() % 2 == 0 # Keep only even numbers
# Apply compress without specifying an axis
result = arr.compress(condition)
# Print the result
print(result)
Output:
[2 4 6 8]
Since compress()
is applied without an axis, the entire array is flattened first, then filtered to include only even numbers.