##### What are these ordering code doing
 What are these ordering code doing coltson Unladen Swallow Posts: 4 Threads: 3 Joined: Oct 2020 Reputation: Nov-02-2020, 10:00 PM (This post was last modified: Nov-02-2020, 10:01 PM by coltson.) Hello, I am trying to understand what these ordering code does. They order a matrix, however I don't know what it is to order a matrix. My first guess is that it treats each row as an independent array. It is not that. Other option is that [i][j] should be bigger or smaller than [i+1][j]. Or finally that [i][j] has to be bigger/smaller[i][j+1]. However, in the following code, none of the above happens. `order = mothFit.argsort()` `mothPos = mothPos[order, :]`I printed the values for a small matrix, 7x3, and yet it does not say anything to me: Quote:('Matrix position:', 0, 0, 'it is: ', -29.387393693113182) ('Matrix position:', 0, 1, 'it is: ', -83.440206190738877) ('Matrix position:', 0, 2, 'it is: ', -10.309922902484161) ('Matrix position:', 1, 0, 'it is: ', -33.361882990330002) ('Matrix position:', 1, 1, 'it is: ', 72.39308962330756) ('Matrix position:', 1, 2, 'it is: ', -75.510119686670876) ('Matrix position:', 2, 0, 'it is: ', 40.051772638850878) ('Matrix position:', 2, 1, 'it is: ', -4.0235637181012862) ('Matrix position:', 2, 2, 'it is: ', -52.411468781539973) ('Matrix position:', 3, 0, 'it is: ', 89.862128975930261) ('Matrix position:', 3, 1, 'it is: ', -43.815001282979168) ('Matrix position:', 3, 2, 'it is: ', -93.485652785167318) ('Matrix position:', 4, 0, 'it is: ', 13.619610433388289) ('Matrix position:', 4, 1, 'it is: ', -27.184963891837128) ('Matrix position:', 4, 2, 'it is: ', -96.080991854199056) ('Matrix position:', 5, 0, 'it is: ', -81.052774094944553) ('Matrix position:', 5, 1, 'it is: ', -23.33498710449237) ('Matrix position:', 5, 2, 'it is: ', -19.97294189498686) ('Matrix position:', 6, 0, 'it is: ', 37.162577502915894) ('Matrix position:', 6, 1, 'it is: ', -4.6313634770747569) ('Matrix position:', 6, 2, 'it is: ', 46.449244269539577) Quote:('Matrix position:', 0, 0, 'it is: ', 37.162577502915894) ('Matrix position:', 0, 1, 'it is: ', -4.6313634770747569) ('Matrix position:', 0, 2, 'it is: ', 46.449244269539577) ('Matrix position:', 1, 0, 'it is: ', 40.051772638850878) ('Matrix position:', 1, 1, 'it is: ', -4.0235637181012862) ('Matrix position:', 1, 2, 'it is: ', -52.411468781539973) ('Matrix position:', 2, 0, 'it is: ', -81.052774094944553) ('Matrix position:', 2, 1, 'it is: ', -23.33498710449237) ('Matrix position:', 2, 2, 'it is: ', -19.97294189498686) ('Matrix position:', 3, 0, 'it is: ', -29.387393693113182) ('Matrix position:', 3, 1, 'it is: ', -83.440206190738877) ('Matrix position:', 3, 2, 'it is: ', -10.309922902484161) ('Matrix position:', 4, 0, 'it is: ', 13.619610433388289) ('Matrix position:', 4, 1, 'it is: ', -27.184963891837128) ('Matrix position:', 4, 2, 'it is: ', -96.080991854199056) ('Matrix position:', 5, 0, 'it is: ', -33.361882990330002) ('Matrix position:', 5, 1, 'it is: ', 72.39308962330756) ('Matrix position:', 5, 2, 'it is: ', -75.510119686670876) ('Matrix position:', 6, 0, 'it is: ', 89.862128975930261) ('Matrix position:', 6, 1, 'it is: ', -43.815001282979168) ('Matrix position:', 6, 2, 'it is: ', -93.485652785167318) So what the above code does? Reply Posts: 6,023 Threads: 16 Joined: Feb 2020 Reputation: Nov-03-2020, 05:07 AM `order = mothFit.argsort()` creates an array or matrix the same size as mothFit which contains indices that will result in a sorted list. Since there is no "axis" argument specified, mothFit is sorted along the "last" axis. Axis 0 sorts the values in colomns (along the rows axis), Axis 1 sorts values in rows (along the columns axis). The "last" axis is Axis 1. This is a complete, though short example. ```import numpy as np x = np.array([[1,6,3],[2,5,4]]) i = x.argsort() y = np.take_along_axis(x, i, axis=1) print('Sort each row\nIndices\n', i) print('Sorted Values\n', y)`````````Output:Sort each row Indices [[0 2 1] # In first row, the middle number is moved to the end [0 2 1]] # Same for the second row Sorted Values [[1 3 6] # Values in a row are sorted in increasing value [2 4 5]]``````You can specify the sorting axis = 0 and sort the values in a column. ```i = x.argsort(axis=0) y = np.take_along_axis(x, i, axis=0) print('\nSort each column\nIndices\n', i) print('Sorted Values\n', y)`````````Output:Sort each column Indices [[0 1 0] # Need to flip middle column [1 0 1]] Sorted Values [[1 5 3] # All columns sorted in increasing order [2 6 4]]``````If you set axis = None, the matrix is sorted as a flat list. ```i = x.argsort(axis=None) y = np.take_along_axis(x, i, axis=None) print('\nFlat sort\nIndices\n', i) print('Sorted Values\n', y)`````````Output:Flat sort Indices [0 3 2 5 4 1] Sorted Values [1 2 3 4 5 6]``````Sorting the values to to still be in matrix form requires the additional step of splitting the array into two parts. ```i = x.argsort(axis=None) y = np.take_along_axis(x, i, axis=None) z = np.array(np.array_split(y, 2)) print('\nFlat sort\nIndices\n', i) print('Sorted Values\n', z)`````````Output:Flat sort Indices [0 3 2 5 4 1] Sorted Values [[1 2 3] [4 5 6]]`````` Reply

 Possibly Related Threads… Thread Author Replies Views Last Post drf ordering by custom field and add ranking in to_representation tomfong521 0 1,785 Mar-24-2021, 09:56 AM Last Post: tomfong521 Help with Matplotlib and ordering the axis theliberalguy97 3 7,468 Dec-12-2020, 08:06 PM Last Post: deanhystad Ordering a list of dict giu88 2 2,652 Nov-05-2018, 02:12 PM Last Post: giu88

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020