Is there any bulid-in function in python/numpy to convert an array = [1, 3, 1, 2]
to something like this:
array = [[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 1, 0, 0],
[0, 0, 1, 0]]
You can create an identity matrix and then use the indices to create a new re-ordered matrix:
>>> a = np.eye(4)
[Out]: array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
>>> indices = [1, 3, 1, 2]
>>> a[indices]
[Out]: array([[0., 1., 0., 0.],
[0., 0., 0., 1.],
[0., 1., 0., 0.],
[0., 0., 1., 0.]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With