0

I have a 2D array with shape (35,6004) and I want to sort it based on the sum of the columns. So if I had for example

array([[5, 3, 13], 
       [1, 2, 20],
       [6, 2,  6]])

I want to sort my array to be like so

array([[13, 5, 3], 
       [20, 1, 2],
       [6 , 6, 2]]).

I tried finding the index of the column

def find_max_col(o):
    t = o.sum(axis=0)
    te = t.tolist()
    return te.index(t.max())

then I use the output of that function to sort the array

test = array[array[:, find_max_col(array)].argsort()]

and do this to check and see if it was successful

t1 = test.sum(axis=0)
print(t1)

As I understand if I sort according to the column with the biggest sum, I should get an array that shows me the sums of all the columns in a descending form as the output of the above code.

Is my code for checking if worked wrong, did I make a mistake in the sorting or did I not even find the correct index of the column to sort by?

2 Answers 2

1

If you want to sort the columns by descending order of their sums, do exactly that.

idx = array.sum(axis=0).argsort()[::-1]
print(array[:,idx])

Your test is correct and the fact that it doesn't produce the right answer means you made a mistake somewhere else. Since you want to sort the columns, test should have been at the very least

test = array[:,array[:, find_max_col(array)].argsort()]

It so happens that array[:, find_max_col(array)].argsort() for your exact example produces the right answer, but for the wrong reasons. You are computing the sorted indices of the column of the highest sum, i.e., array[:,2] == array([13, 20, 6]), when you're supposed to sort the indices of the column sums in descending order, i.e., array.sum(axis=0) == array([12, 7, 39]).

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

Comments

0

I'm not sure if your solution is incorrect, but it is certainly more complicated than necessary:

>>> a = np.array([[5, 3, 13],
                  [1, 2, 20],
                  [6, 2,  6]])

>>> a[:, a.sum(axis=0).argsort()]  # sort columns small-to-large
array([[ 3,  5, 13],
       [ 2,  1, 20],
       [ 2,  6,  6]])

>>> a[:, (a.sum(axis=0)*-1).argsort()]  # multiply sums by -1 to sort large-to-small
array([[13,  5,  3],
       [20,  1,  2],
       [ 6,  6,  2]])

Comments

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.