0

This seems been asked many times, however the answer I found not work now. Let's be simple, here I have a numpy matrix

data = np.matrix([[9, 8],
             [7, 6],
             [5, 7],
             [3, 2],
             [1, 0]])

Then sort by second column as below

[[1, 0],
[3, 2],
[7, 6],
[5, 7],
[9, 8]])          

I tried a lot examples like Python Matrix sorting via one column but none of them worked.

I wondering maybe because the answers were posted years ago which do not work for newest Python? My Python is 3.5.1.

Example of my failed trial:

data = np.matrix([[9, 8],
             [7, 6],
             [5, 7],
             [3, 2],
             [1, 0]])
temp = data.view(np.ndarray)
np.lexsort((temp[:, 1], ))

print(temp)
print(data)
7
  • 2
    What does "not work" mean, which code exactly was tried and you know that np.matrix != np.array? (I really really really don't think, that these other answers are outdated if they are using basic numpy stuff; this would kill numpy for a lot of reasons!) Commented May 29, 2016 at 22:14
  • While taking another glance at your question: your title Sorting numpy matrix by column is misleading. The working example in your first link does that, but according to your example you only want to sort the column within the matrix (without rearranging the corresponding other columns too)! Commented May 29, 2016 at 22:20
  • @sascha You are right, so I have remove my second link. "Don't work " means the approved answer does not sort my matrix. i have put an example there. Commented May 29, 2016 at 22:24
  • Specify again what you really want: you want to sort all columns by one target-column (as the title says; permuting complete rows) or you want to sort only one column and don't touch the others ones, as your example shows. (the former has a working solution in your link) Commented May 29, 2016 at 22:38
  • 1
    Then why is your example showing something else? Commented May 29, 2016 at 22:43

2 Answers 2

4

You are a moving target.

Sort each column independently:

In [151]: np.sort(data,axis=0)
Out[151]: 
matrix([[1, 0],
        [3, 2],
        [5, 6],
        [7, 7],
        [9, 8]])

Sort on the values of the second column

In [160]: ind=np.argsort(data[:,1],axis=0)

In [161]: ind
Out[161]: 
matrix([[4],
        [3],
        [1],
        [2],
        [0]], dtype=int32)

In [162]: data[ind.ravel(),:]  # ravel needed because of matrix
Out[162]: 
matrix([[[1, 0],
         [3, 2],
         [7, 6],
         [5, 7],
         [9, 8]]])

Another way to get a valid ind array:

In [163]: ind=np.argsort(data.A[:,1],axis=0)

In [164]: ind
Out[164]: array([4, 3, 1, 2, 0], dtype=int32)

In [165]: data[ind,:]

To use lexsort you need something like

In [175]: np.lexsort([data.A[:,0],data.A[:,1]])
Out[175]: array([4, 3, 1, 2, 0], dtype=int32)

or your 'failed' case - which isn't a fail

In [178]: np.lexsort((data.A[:,1],))
Out[178]: array([4, 3, 1, 2, 0], dtype=int32)

here data[:,1] is the primary key. data[:,0] is the tie breaker (not applicable in your example). I'm just working from the docs.

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

1 Comment

Went through all your hints, thanks so much for the help!
1

The approach in your link is working:

import numpy as np

data = np.matrix([[9, 8],
         [7, 6],
         [5, 7],
         [3, 2],
         [1, 0]])

print(data[np.argsort(data.A[:, 1])])

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

And now an example where it's better to see:

data = np.matrix([[1, 9],
         [2, 8],
         [3, 7],
         [4, 6],
         [0, 5]])
[[0 5]
 [4 6]
 [3 7]
 [2 8]
 [1 9]]

3 Comments

Sascha, I do appreciate your help and patience. I made a lot mistakes in my initial post. Biggest mistake was I confused by matrix and ndarray. Anyway, thanks again. Solved my problem
No worries. Have fun with python and numpy! You also should accept @hpaulj's answer because it is really detailed and solves your problem! Have a look at StackOverflows help to see why you won't need any "SOLVED" in title. You just mark one answer as accepted.
Just flagged. Well if possible I would like to flag you both

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.