1

With two arrays:

X = np.array([[1,2,3], [2,3,1]])
X
array([[1, 2, 3],
       [2, 3, 1]])
Y = np.array([['A','B', 'C'], ['A','B', 'C']])
Y
array([['A', 'B', 'C'],
       ['A', 'B', 'C']], 
     dtype='|S1')

I am trying to sort Y based on the values of X row by row without looping through each row, i.e

xord = X.argsort()

for i in range(X.shape[0]):
    print Y[i][xord[i]]

['A' 'B' 'C']
['C' 'A' 'B']

Is there a more efficient way to sort array Y based on the corresponding row order of X?

2 Answers 2

1

Altering Mazdak's answer based on AdrianBoeh's comment, after I found this similar question here How to using numpy.argsort on a 2D array to sort another 2D array :

    X = np.array([[1,2,3], [2,3,1]])
    Y = np.array([['A','B', 'C'],['X','Y','Z']])
    s=np.argsort(X)
    np.take_along_axis(Y,s,axis=1)

It seems that the np.take(Y,s) applies the indexes s only to the first row of Y, so np.take_along_axis is necessary in this kind of problem if the rows of Y are not all the same.

Sign up to request clarification or add additional context in comments.

Comments

1

First you can use np.argsort to get the indices of X elements based on those position after sorting,then you can get the elements from Y based on the indices of X with np.take():

>>> s=np.argsort(X)
>>> np.take(Y,s)
array([['A', 'B', 'C'],
       ['C', 'A', 'B']], 
      dtype='|S1')

1 Comment

I don't think this answers the question. How does this maintain alignment of rows like the for loop would have? Try using Y as below, it shows the issue more clearly Y = np.array([['A','B', 'C'], ['X','Y', 'Z']])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.