np.where
np.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.select
np.select([a <= .5, a>.5], [np.zeros_like(a), np.ones_like(a)])# array([ 0., 0., 0., 1., 1., 1.])
特殊情况: np.round
如果您的数组值是介于0和1之间的浮动值并且您的阈值为0.5,则这是最佳解决方案。
a.round()# array([0., 0., 0., 1., 1., 1.])



