convert numpy array to 0 or 1

前端 未结 6 1558
小鲜肉
小鲜肉 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]])
    
    0 讨论(0)
  • 2020-12-29 06:54

    I think you need vectorized function np.where:

    B = np.where(A > 0.5, 1, 0)
    print (B)
    [[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]]
    

    B = np.where(A <= 0.5, 0, 1)
    print (B)
    [[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]]
    

    But better is holdenweb solution if need convert to 0 and 1 only.

    np.where is better if need convert to another scalars like 5 and 10 or a and b:

    C = np.where(A > 0.5, 5, 10)
    print (C)
    [[ 5  5  5  5  5  5 10  5  5  5 10 10  5  5 10  5 10  5 10 10  5 10 10  5
       5  5  5 10 10  5 10  5  5 10  5 10 10  5 10 10  5 10  5 10  5 10 10  5
       5 10]]
    
    D = np.where(A > 0.5, 'a', 'b')
    print (D)
    [['a' 'a' 'a' 'a' 'a' 'a' 'b' 'a' 'a' 'a' 'b' 'b' 'a' 'a' 'b' 'a' 'b' 'a'
      'b' 'b' 'a' 'b' 'b' 'a' 'a' 'a' 'a' 'b' 'b' 'a' 'b' 'a' 'a' 'b' 'a' 'b'
      'b' 'a' 'b' 'b' 'a' 'b' 'a' 'b' 'a' 'b' 'b' 'a' 'a' 'b']]
    

    Timings:

    np.random.seed(223)
    A = np.random.rand(1,1000000)
    
    #jez
    In [64]: %timeit np.where(A > 0.5, 1, 0)
    100 loops, best of 3: 7.58 ms per loop
    
    #holdenweb
    In [65]: %timeit (A > 0.5).astype(int)
    100 loops, best of 3: 3.47 ms per loop
    
    #stamaimer
    In [66]: %timeit element_wise_round(A)
    1 loop, best of 3: 318 ms per loop
    
    0 讨论(0)
  • 2020-12-29 06:54

    You can make the built-in round function element wise use np.vectorize.

    import numpy as np
    
    element_wise_round = np.vectorize(round, otypes=[np.int])
    
    print element_wise_round(A)
    
    0 讨论(0)
  • 2020-12-29 07:01

    this may solve your issue

    a=1
    b=0
    predict=list()
    for i in A:
        if i>0.5:
            predict.append(a)
        else:
            predict.append(b)
    print(predict)
    

    we first assign value a to 0 and b to 1 but you can directly use 1 and 0 on append function 'A' contains the list in form of probability from which we assign the new list name predict and after comparing each items in list we put value into list by using append methord predict list contain your ans

    0 讨论(0)
  • 2020-12-29 07:13
    np.random.seed(223)
    A = np.random.rand(1000000)
    A = [0 if i <=0.5 else 1 for i in A]
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题