numpy - I miss sortrows from Matlab!
Feb 272019There are things where NumPy is great! Not having sortrows is not one of them.
This is my straightforward implementation for 2D numpy arrays. Negative indicies cannot be used for descending order, therefore I use auxiliary Descending-object.
import numpy as np
class Descending:
""" for np_sortrows: sort column in descending order """
def __init__(self, column_index):
self.column_index = column_index
def __int__(self): # when cast to integer
return self.column_index
def np_sortrows(M, columns=None):
""" sorting 2D matrix by rows
:param M: 2D numpy array to be sorted by rows
:param columns: None for all columns to be used,
iterable of indexes or Descending objects
:return: returns sorted M
"""
if len(M.shape) != 2:
raise ValueError('M must be 2d numpy.array')
if columns is None: # no columns specified, use all (reversed)
M_columns = tuple(M[:, c] for c in range(M.shape[1]-1, -1, -1))
else:
M_columns = []
for c in columns:
M_c = M[:, int(c)]
if isinstance(c, Descending):
M_columns.append(M_c[::-1])
else:
M_columns.append(M_c)
M_columns.reverse()
return M[np.lexsort(M_columns), :]
if __name__ == '__main__':
data = np.array([[3, 0, 0, .24],
[4, 1, 1, .41],
[2, 1, 3, .25],
[2, 1, 1, .63],
[1, 1, 3, .38]])
# to satisfy original question:
# third column is index 2, fourth column in reversed order
print(np_sortrows(data, [2, Descending(3)]))
Original question that proded me to make the post:
https://stackoverflow.com/questions/18920010/sortrows-with-multiple-sorting-keys-in-numpy/