0

I am trying to sort each column of a list using the numpy.argsort. However, I am getting an output that does not match the correct sorting. The name of the list that I am trying to sort is "CD". Below, I am providing the code that I am using so far and the incorrect output.

print(numpy.argsort(CD,axis=0))

The CD list the one below.

[[90, 85, 71, 48], 
[28, 75, 2, 71], 
[5, 93, 15, 56], 
[59, 91, 29, 43], 
[28, 24, 82, 35], 
[13, 102, 77, 21], 
[85, 102, 33, 64], 
[80, 66, 64, 30], 
[91, 78, 41, 1], 
[77, 33, 30, 50]]

and the output is

[[2 4 1 8]
 [5 9 2 5]
 [1 7 3 7]
 [4 1 9 4]
 [3 8 6 3]
 [9 0 8 0]
 [7 3 7 9]
 [6 2 0 2]
 [0 5 5 6]
 [8 6 4 1]]

I would really appreciate if you could give a hint of what possibly goes wrong.

EDIT

I want to get the index of the sorted columns. Not the actual number. Sorry for not clearing this out in the first place

1
  • What element do you want to sort the columns on? The first element? Or do you want to keep the columns in place and sort each one? The question is still very unclear; can you give an example of what CD looks like once it is sorted? Commented Nov 7, 2019 at 13:24

3 Answers 3

1

A working (but slow) method of getting what you want:

np.argsort(np.argsort(x, axis = 0), axis = 0)
Out[]: 
array([[8, 5, 7, 5],
       [2, 3, 0, 9],
       [0, 7, 1, 7],
       [4, 6, 2, 4],
       [3, 0, 9, 3],
       [1, 8, 8, 1],
       [7, 9, 4, 8],
       [6, 2, 6, 2],
       [9, 4, 5, 0],
       [5, 1, 3, 6]], dtype=int64)

What you want is actually an inverse argsort, which you can find a lot of information on here

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

Comments

0

numpy.argsort returns the index of the sorted array

I think what you want is numpy.sort

print(numpy.sort(CD,axis=0))
# [[  5  24   2   1]
#  [ 13  33  15  21]
#  [ 28  66  29  30]
#  [ 28  75  30  35]
#  [ 59  78  33  43]
#  [ 77  85  41  48]
#  [ 80  91  64  50]
#  [ 85  93  71  56]
#  [ 90 102  77  64]
#  [ 91 102  82  71]]

Comments

0

Maybe you just want to sort (Return a sorted copy of an array) the array instead of argsort (Returns the indices that would sort an array)?

np.sort(cd, axis=1)
#array([[ 48,  71,  85,  90],
#       [  2,  28,  71,  75],
#       [  5,  15,  56,  93],
#       [ 29,  43,  59,  91],
#       [ 24,  28,  35,  82],
#       [ 13,  21,  77, 102],
#       [ 33,  64,  85, 102],
#       [ 30,  64,  66,  80],
#       [  1,  41,  78,  91],
#       [ 30,  33,  50,  77]])
np.sort(cd, axis=0)
#array([[  5,  24,   2,   1],
#       [ 13,  33,  15,  21],
#       [ 28,  66,  29,  30],
#       [ 28,  75,  30,  35],
#       [ 59,  78,  33,  43],
#       [ 77,  85,  41,  48],
#       [ 80,  91,  64,  50],
#       [ 85,  93,  71,  56],
#       [ 90, 102,  77,  64],
#       [ 91, 102,  82,  71]])

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.