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

  • 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? – Chris Mueller Nov 07 '19 at 13:24

3 Answers3

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

Daniel F
  • 13,620
  • 2
  • 29
  • 55
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]]
Hongpei
  • 677
  • 3
  • 13
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]])
Albo
  • 1,584
  • 9
  • 27