I need to sort a multidimensional array according to the values in the first sub-array, as fast as possible (the line is applied millions of times).
Below is my original line, and my attempt at improving its performance which is not working. As far as I can see, my numpy approach is only sorting properly the first sub-array, and none of the remaining ones.
What am I doing wrong and how can I improve the performance of the sorting?
import numpy as np
# Generate some random data.
# I receive the actual data as a list, hence the .tolist()
aa = np.random.rand(10, 2000).tolist()
# This is the original line I need to process faster.
b1 = zip(*sorted(zip(*aa), key=lambda x: x[0]))
# This is my attempt at improving the above line's performance
b2 = np.sort(np.asarray(aa).T, axis=0).T
# Check if all sub-arrays are equal
for a, b in zip(*[b1, b2]):
print(np.array_equal(a, b))
lambda x: x[0]withoperator.itemgetter(0).numpyapproach not working? What am I doing wrong?