What is the numpy.all() in Numpy?
The functions in NumPy help to make our code easier to read and accessible. Suppose you were asked to write a program to check whether the elements of an array are even/odd. The traditional way to do this is to loop through each element and check if they are even/odd.
With the help of the NumPy all() function in NumPy, we can test/evaluate all of the elements of the array in a single-go. NumPy all() function tests if all the array elements are True. All we need to do is put the condition as a parameter when we use the all() function.
Hence, the use of the NumPy all() function has single-handedly removed the use of a for loop and has made our code more accessible and easier to understand. Let's understand in-depth the all() function.
Build an AI-First Career, Master the Complete Skillset
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
AI Forward Deployed Engineer Program
Full-stack engineering, production AI and client-facing consulting
+1000 moreSyntax
The syntax for NumPy all() function is:
Parameters
The various parameters in NumPy all() are:
- arr: This mandatory parameter represents the input array to be tested.
- axis: This optional parameter represents the axes along which a logical AND reduction is performed. The default value is None.
- out: This optional parameter represents an alternative output array to place to the result. It must have the same shape as the expected output array.
Return Value
The NumPy all() function always returns a boolean value or an array of booleans.
How Scaler Transformed Careers in Different Fields
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Examples
-
NumPy all() on a one-dimensional array
In this example, we will use the NumPy all() function to evaluate an array that contains only True values ([True, True, True]).
Output
As we can see, since the array contained only True elements, the output was returned as True. This indicates that all of the values in the array are True.
-
Test an array for a specific condition
Output
Since every element in the input array is greater than 2, the output comes out to be True.
-
Using NumPy all() on two-dimensional arrays
Output
When we use the NumPy all() function on a two-dimensional array, it checks if every element of the two-dimensional array is greater than 2 (given condition).
The conditional logic generates a NumPy array with just boolean values, which NumPy all() then uses to get the final True/False answer.
-
Apply NumPy.all() along axis-0
Output
In this example, we use the axis parameter to enforce our condition on all the elements of the particular axis. In the 0th row, we have the elements [1, 2]. Since only 2 abide by our condition, it is returned as True.
-
Apply NumPy.all() along axis-1
Output
When we select our axis as 1, we consider the column values only. In the input array, the top column values are 1 and 4. Hence, our condition is only applicable to 4.
Conclusion
In this article, we understood the NumPy all() function, how it works, and its applications.
- NumPy all() is a function that is used to evaluate every element of an array in a single go.
- Since the function evaluates every element, the return value for NumPy all() is boolean.
- With the help of some examples, we explored the use of NumPy all() function on 1-D arrays, 2-D arrays, etc.