Home > Mobile >  Transform 2d numpy array into 2d one hot encoding
Transform 2d numpy array into 2d one hot encoding

Time:01-20

How would I transform

a=[[0,6],
   [3,7],
   [5,5]]

into

b=[[1,0,0,0,0,0,1,0],
   [0,0,0,1,0,0,0,1],
   [0,0,0,0,0,1,0,0]]

I want to bring notice to how the final array in b only has one value set to 1 due to the repeat in the final array in a.

CodePudding user response:

Using indexing:

a = np.array([[0,6],
              [3,7],
              [5,5]])

b = np.zeros((len(a), a.max() 1), dtype=int)

b[np.arange(len(a)), tuple(a.T)] = 1

Output:

array([[1, 0, 0, 0, 0, 0, 1, 0],
       [0, 0, 0, 1, 0, 0, 0, 1],
       [0, 0, 0, 0, 0, 1, 0, 0]])

CodePudding user response:

This can also be done using numpy broadcasting and boolean comparision in the following way:

a = np.array([[0,6],
              [3,7],
              [5,5]])
# Convert to 3d array so that each element is present along the last axis
# Compare with max 1 to get the index of values as True.
b = (a[:,:,None] == np.arange(a.max() 1))
# Check if any element along axis 1 is true and convert the type to int
b = b.any(1).astype(int)

Output:

array([[1, 0, 0, 0, 0, 0, 1, 0],
       [0, 0, 0, 1, 0, 0, 0, 1],
       [0, 0, 0, 0, 0, 1, 0, 0]])
  • Related