convert numpy array to 0 or 1

前端 未结 6 1563
小鲜肉
小鲜肉 2020-12-29 06:12
A = np.array([[0.94366988, 0.86095311, 0.88896715, 0.93630641, 0.74075403, 0.52849619
                  , 0.03094677, 0.85707681, 0.88457925, 0.67279696, 0.26601085,         


        
6条回答
  •  遥遥无期
    2020-12-29 06:53

    Standard numpy broadcasting can be used to compare each element with a scalar value, yielding a Boolean for each element. The ndarray.astype method then converts the True values to 1 and the False values to zero.

    In [16]: (A > 0.5).astype(int)
    Out[16]:
    array([[1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0,
            0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0,
            1, 0, 0, 1, 1, 0]])
    

提交回复
热议问题