3

I have an N x 100 numpy matrix containing any kind of numbers that I want to sort.

In order for it to be more visual, I will now fill it out with dummy values:

import numpy as np

X = np.array( [[float(number) for number in range(100)] for _ in range(10)] )

# X 
[[   0.    1.    2. ...,   97.   98.  99.]
 [   0.    1.    2. ...,   97.   98.  99.]
 [   0.    1.    2. ...,   97.   98.  99.]
 ..., 
 [   0.    1.    2. ...,   97.   98.  99.]
 [   0.    1.    2. ...,   97.   98.  99.]
 [   0.    1.    2. ...,   97.   98.  99.]]

I want to sort the columns for all N rows using the following 100-element list as the key:

# s
["butterfly", "zebra", "cactus", ... "animal", "xylitol", "yoyo"]

So that the output looks like this:

# X_sorted
[[   97.    0.    2. ...,   98.   99.  1.]
 [   97.    0.    2. ...,   98.   99.  1.]
 [   97.    0.    2. ...,   98.   99.  1.]
 ..., 
 [   97.    0.    2. ...,   98.   99.  1.]
 [   97.    0.    2. ...,   98.   99.  1.]
 [   97.    0.    2. ...,   98.   99.  1.]]

So basically, I want to retrieve the alphabetical sorting output of s, and apply it to the columns of X.

How can I achieve this?

I am familiar with the sort command using key, but I do not know how to apply this to the matrix columns in this scenario.

4
  • Where exactly are you using numpy here? This is important. The objects shown here are all lists. Commented Feb 22, 2018 at 14:19
  • X is a numpy matrix, and s is a list. I am quite new to Python, so please let me know how to change the displayed formatting in my question in order to avoid confusion, in case they are displayed in a wrong manner. Commented Feb 22, 2018 at 14:22
  • Try printing a numpy array and make your output match. There's usually np.array( or array( at the beginning of an array to indicate that it is an array (and of course a matching ) at the end). Commented Feb 22, 2018 at 14:25
  • 1
    I edited your post, you'll see how you could have formatted this when it'll be accepted. Commented Feb 22, 2018 at 14:27

2 Answers 2

3

If your objects were numpy arrays (as in X = np.array(X); s = np.array(s), then you could use np.argsort, which returns an array of the indices that would make the input sorted.

X_sorted = X[:, np.argsort(s)]
Sign up to request clarification or add additional context in comments.

1 Comment

Pretty sure it suffices for s to be array-like, np.argsort will coerce.
-1

Basically, your job is to sort the string list in descending order, obtain the indices of sorted array in the original array and apply that to each row of the original numpy array. Here is the code for that

# This gives indices of array s in descending alphabetical order
s_sorted_indices_desc = np.argsort(np.array(s))[::-1]

# This applies sorting according to the indices obtained above, to each row
X_sorted = np.apply_along_axis(lambda row: row[s_sorted_indices_desc], 1, X)

5 Comments

Where do you see descending order in the question?
The second operation should just be a single indexing operation. The apply is complete overkill.
@MadPhysicist Implicit assumption seeing that butterfly has an index of 97 while zebra has an index of 0
Animal has index 97. Butterfly has index 0
Try running with OP's input. You'll see what I mean.