So I have this simple function here
def divide(arr):
out = np.where(arr.any() <= 0, 0, 1/arr)
return out
a = np.array([0,1])
print(divide(a))
When running this, the output is [inf 1.]
. I thought that np.where()
would replace the inf with 0, why is this not the case? How do I make it so that what is returned is [0,1]
?
Edit:
So for the above, .any() should not be used. For this following function though, without using .any(), I get "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()".
def entropy(x, n):
return np.where(x.any() <= 0 or n<=0 , 0, -(x/n) * np.log2(x/n))
a = np.array([0,1])
n = 1
print((entropy(a,n)))
>> [nan -0.]
How do I resolve this issue?