1

I have a NxM matrix called coefficients that I want to sort:

import numpy
N = 10
M = 42
coefficients = numpy.random.uniform(size=(N, M))

I have an array called order with N elements that says the order that the rows of coefficients should be in:

order = numpy.random.choice(range(N), N, False)

I'm sorting coefficients by sorting order:

coefficients = numpy.array([mag for (orig, mag)
                            in sorted(zip(order, coefficients),
                                      key=lambda pair: pair[0])])

This works, but it's probably slower than it should be. If this was in 1D, I'd use fromiter, but I don't know how to tackle this since it's 2D. Is there an optimization I can make here?

1 Answer 1

4

To answer your question, just coefficients[order.argsort()] is enough :)

See also Numpy: sort by key function.

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

5 Comments

This goes in the opposite direction from the original code - the OP's code has original[0] go to result[order[0]] rather than having result[0] come from original[order[0]]. This might be an acceptable difference, depending on how order needs to behave. If not, we need coefficients[order.argsort()].
(I don't know why the other guys deleted their answers. There are two deleted answers that already provide this information.)
Omg, so obvious. Thanks a bunch you two!
@JoeKington, he might have thought the same :D
It's also worth noting that if the sort gets expensive, result = numpy.empty_like(coefficients); result[order] = coefficients can avoid the sort. At the scale given in the question, it's not a problem, but with millions of rows, it becomes a big difference.

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.