Search code examples
pythonnumpynpnp.argsort

np.argsort not sorting correctly when value over a certain threshold


For some reason when I want to sort this matrix by the 3rd column with argsort, it does not work when element (3,3) in my matrix is greater than ten.. If its 9 or less it seems to sort correctly though. Does anyone know whats wrong here?

X = np.array([[5, 2, 3], [2.222, 5.5, 6], [3.3, 8, 10], [1.05, 0, 0]])
y = np.array([['T'], ['F'], ['T'], ['T']])
data = np.column_stack([X,y])
print(data)
sorted_data = data[data[:, 2].argsort()]
print(sorted_data)

screenshot of resulting print out


Solution

  • As already pointed out in the comments, argsort() is working correctly, but once you stack X with y, then the full array dtype is no longer float but unicode string (see this post).

    Modified code here:

    import numpy as np
    
    X = np.array([[5, 2, 3], [2.222, 5.5, 6], [3.3, 8, 10], [1.05, 0, 0]])
    y = np.array([['T'], ['F'], ['T'], ['T']])
    data = np.column_stack([X,y])
    print(data)
    sorted_data = data[data[:, 2].astype(float).argsort()]
    print(sorted_data)