My code is:-
import numpy as np
def f(x):
if x<=0.5:
return 0
else:
return 1
X=np.array([0.1,0.2,0.6,0.5,0.01,1])
print(f(X))
This code gives an error
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
but I don't know how a.any() or a.all() will be useful for me. I want the output to be:-
[0,0,1,0,0,1]
I can use the for loop, but is there any shortcut syntax so that I get the desired output in just one go?
np.where(X<=0.5, 0, 1). See the docs here.