I have an array below:
a=np.array([0.1, 0.2, 0.3, 0.7, 0.8, 0.9])
What I want is to convert this vector to a binary vector based on a thres
np.wherenp.where(a > 0.5, 1, 0)
# array([0, 0, 0, 1, 1, 1])
astype(a > .5).astype(int)
# array([0, 0, 0, 1, 1, 1])
np.selectnp.select([a <= .5, a>.5], [np.zeros_like(a), np.ones_like(a)])
# array([ 0., 0., 0., 1., 1., 1.])
np.roundThis is the best solution if your array values are floating values between 0 and 1 and your threshold is 0.5.
a.round()
# array([0., 0., 0., 1., 1., 1.])