Home > Enterprise >  How to convert n*1 array into n*m array where m is unique values in the array?
How to convert n*1 array into n*m array where m is unique values in the array?

Time:08-26

I have an array of array( [1,2,3, 4, 1,2 ,3 ,3,3,3]) having shape (10,)

a = np.array([1,2,3, 4, 1,2 ,3 ,3,3,3])
print(a)
print(a.shape)

which has unique values 1,2,3,4 ie m = 4 How to pivot it to get an array of shape (10,4)

Expected output

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

CodePudding user response:

np.eye(a.max(),dtype=int)[a - 1]

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

CodePudding user response:

IIUC, You can use pandas.get_dummies.

>>> import pandas as pd

# use '.values' for converting result to 'numpy.array'
>>> b = pd.get_dummies(a).values

>>> b.shape
(10, 4)

>>> b
array([[1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1],
       [1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 1, 0],
       [0, 0, 1, 0],
       [0, 0, 1, 0]], dtype=uint8)

CodePudding user response:

Using broadcasting comparison:

out = (a[:,None] == np.unique(a)).astype(int)

output:

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

alternative

If you want to ensure having empty columns on missing numbers:

out = (a[:,None] == np.arange(a.max()) 1).astype(int)

To see the difference, let's use a = np.array([1,3,5,3,1]) as input:

a = np.array([1,3,5,3,1])

array([1, 3, 5, 3, 1])


(a[:,None] == np.unique(a)).astype(int)

#       1  3  5
array([[1, 0, 0],
       [0, 1, 0],
       [0, 0, 1],
       [0, 1, 0],
       [1, 0, 0]])


(a[:,None] == np.arange(a.max()) 1).astype(int)

#       1  2  3  4  5
array([[1, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 0, 1],
       [0, 0, 1, 0, 0],
       [1, 0, 0, 0, 0]])
  • Related