Answers to How to convert a binary classes column to numpy array ( 1 )

  1. 2016-12-31 14:12

    You could use NumPy broadcasting -

    (a[:,None] != np.arange(2)).astype(int)
    

    Sample run -

    In [7]: a = np.array([0,1,0,1,1])
    
    In [8]: (a[:,None] != np.arange(2)).astype(int)
    Out[8]: 
    array([[0, 1],
           [1, 0],
           [0, 1],
           [1, 0],
           [1, 0]])
    
    # Convert to list if needed
    In [14]: (a[:,None] != np.arange(2)).astype(int).tolist()
    Out[14]: [[0, 1], [1, 0], [0, 1], [1, 0], [1, 0]]
    

Leave a reply to - How to convert a binary classes column to numpy array

◀ Go back