I have a function that receives some arguments, plus some optional arguments. In it, the action taken is dependent upon whether the optional argument c
was filled:
def func(a, b, c = None):
doStuff()
if c != None:
doOtherStuff()
If c
is not passed, then this works fine. However, in my context, if c
is passed, it will always be a numpy
array. And comparing numpy
arrays to None
yields the following warning:
FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
So, what is the cleanest and most general way to check whether or not c
was passed or not without comparing to None
?
In numpy, we can check that whether none of the elements of given array is zero or not with the help of numpy. all() function. In this function pass an array as parameter. If any of one element of the passed array is zero then it returns False otherwise it returns True boolean value.
The [:, :] stands for everything from the beginning to the end just like for lists. The difference is that the first : stands for first and the second : for the second dimension. a = numpy. zeros((3, 3)) In [132]: a Out[132]: array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]])
Method 1: We generally use the == operator to compare two NumPy arrays to generate a new array object. Call ndarray. all() with the new array object as ndarray to return True if the two NumPy arrays are equivalent.
WeldNumpy is a Weld-enabled library that provides a subclass of NumPy's ndarray module, called weldarray, which supports automatic parallelization, lazy evaluation, and various other optimizations for data science workloads.
Use if c is not None
instead. In addition to avoiding the warning, this is generally considered best-practice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With