Python has a powerful module named Numpy. At the same time, its specialization is in Data science, used in many ways. The in-built functions are versatile. So we provide tutorials for its various functions for you to practice. In this, we will see about numpy any a boolean function. Its working is based on conditions, so conditional programming becomes easy.
What is numpy any?
As per NumPy v1.19 Manual, numpy any tests whether any array element along a given/mention axis evaluates to be true. In other words, this is a boolean function. This function returns True when ndarray passed to the first parameter contains at least one True element and returns False otherwise.
This function works on the logic of OR. To say that, this function adds all false as 0 and others as 1. Then it adds all if the sum is 0, then it returns False otherwise, it is True
Syntax of numpy any
The proper syntax of the function is:
numpy.any(a, axis=None, out=None, keepdims=<no value>)
But the most commonly used way is:
|Parameter||Mandatory or Not|
‘a’ is the prime parameter. This input is all that is enough for evaluation. Input array or object( can be converted to an array). The array whose elements are tested. The function test in the elements of this array. With input containing even one True value, it makes the result True.
The data type used is int or tuple of ints. The function performs tests along the axis provided. In other words, axis or axes along which a logical OR reduction is achieved. The default (axis=None) is to perform a logical OR over all the input array dimensions. Axis may be negative, in which case it counts from the last to the first axis.
While the evaluation happens for a single axis or all the axes, if there is a tuple of ints, then evaluation happens on multiple axes.
The data type is array_like and optional to use. This parameter makes the output array with the same dimensions as the Input array, placed with the result. It must have the same shape as the planned performance and maintain its form.
This has one value, ‘True.’ If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
If the default value is passed, then keepdims will not be passed through to all methods of sub-classes of ndarray. However, any non-default value will be. If the sub-classes sum method does not implement keepdims any exceptions will be raised.
Return value of numpy any
The output is a boolean, either True or False. But when out is used, then output is a ndarray with multiple booleans.
Remember, only 0 and False are the same. Not a Number (NaN), positive infinity, and negative infinity evaluate to True because these are not equal to zero.
Examples for practice
First we import module of Numpy
import numpy as np
Now we see multiple examples:
np.any([[True, False], [True, False]]) True np.any([ -1, 0, 2, 0]) True np.any(np.nan) True
In above all lines , we see result True as only 0 and False is considered for False as output
np.any([[True, False], [False, Fasle]], axis=0) array([ True, False]) np.any([[True, False], [False, True]], axis=0) array([ True, False]) np.any([[True, False], [False, False]], axis=1) array([ True, False])
We have multiple output now . The boolean is compared as per the axis. 0 for row wise and 1 for columnwise tests
np.any([[1, 0],[0, 4]], keepdims=True) array([[ True]]) np.any([[1, 0],[0, 4]], keepdims=False) True np.any([[0, 0],[0, 0]], keepdims=True) array([[False]]) np.any([[0, 0],[0, 0]], keepdims=False) False
This is use keepdims which brings output with dimensions.
o=np.array(False) z=np.any([-1, 4, 5], out=o) z,o (array(True), array(True))
this is use of out parameter where o array is used by reference
This is also a boolean function in the numpy module, like numpy any. This returns True only if all values are 1 or True or non zero or NaN. This is just the opposite of numpy any.
numpy any is based on any function used in basic Python. Syntax :
It returns False if all elements are false or if an iterable is empty, otherwise True. Same as numpy any except it is for one dimension only.
NumPy is very powerful and incredibly essential for information science in Python. That being true, if you are interested in data science in Python, you really ought to find out more about Python.
You might like our following tutorials on numpy.
- Mean: Implementation and Importance
- Using Random Function to Create Random Data
- Reshape: Reshaping Arrays With Ease
- In-depth Explanation of np.power() With Examples
- Clip Function
In conclusion, we can see numpy any is used in multiple ways and is a broader aspect of any in Python for better computing and calculations. We can use numpy in an inclusive way with different functions like this.
Still have any doubts or questions do let me know in the comment section below. I will try to help you as soon as possible.