0
0
NumPydata~5 mins

np.any() and np.all() in NumPy

Choose your learning style9 modes available
Introduction

These functions help check if some or all values in data meet a condition. They make it easy to quickly understand your data.

Check if any sensor reading in a list is above a danger level.
Verify if all students passed an exam from their scores.
Find out if any missing values exist in a dataset.
Confirm if all entries in a column meet a quality standard.
Syntax
NumPy
np.any(array, axis=None, keepdims=False)
np.all(array, axis=None, keepdims=False)

array is your data, usually a NumPy array.

axis lets you check across rows, columns, or the whole array.

Examples
Returns True because at least one value is True.
NumPy
np.any([False, True, False])
Returns False because not all values are True.
NumPy
np.all([True, True, False])
Checks each column; returns [False, True] because the second column has a True (1).
NumPy
np.any([[0, 0], [0, 1]], axis=0)
Checks each row; returns [True, True] because all values in each row are True (1).
NumPy
np.all([[1, 1], [1, 1]], axis=1)
Sample Program

This code checks if any temperature is above 30 degrees and if all temperatures in each row are above 20 degrees. It helps quickly understand temperature data.

NumPy
import numpy as np

# Sample data: temperatures in Celsius
temps = np.array([[22, 25, 19], [30, 35, 28], [15, 18, 20]])

# Check if any temperature is above 30
any_above_30 = np.any(temps > 30)

# Check if all temperatures in each row are above 20
all_above_20_per_row = np.all(temps > 20, axis=1)

print(f"Any temperature above 30? {any_above_30}")
print(f"All temperatures above 20 per row? {all_above_20_per_row}")
OutputSuccess
Important Notes

Use axis=None (default) to check the whole array.

These functions return a boolean or an array of booleans depending on the axis.

They are very useful for quick data quality checks.

Summary

np.any() checks if any value is True.

np.all() checks if all values are True.

Use the axis parameter to check along rows, columns, or the whole array.