convert numpy array to 0 or 1

前端 未结 6 1571
小鲜肉
小鲜肉 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 07:13

    The problem with your code is the dimension of A[i] in your code. A is initialised as matrix[1,n] (1 row and n columns - you have double [[]] brackets in np.array([[]])), so you reference of type A[i] actually means "the whole i'th row, or an array(n) in this case). I'm quite new to python and numpy, but to access individual cells and set each to 1 or 0 could be coded this way (of course, only if you are forced to use FOR loops here):

     for i in range(A.shape[1]):
     if A[0,i]>0.5:
        Y_prediction[0,i] = 1
     else:
        Y_prediction[0,i] = 0
    

    But,definitely, the best solution, as others described above, is to avoid for loop.

提交回复
热议问题