Sort Numpy Arrays by Row and Column with numpy.argsort
23K views
May 13, 2022
Use numpy.argsort to sort numpy arrays by columns and rows. This tutorial will give you step-by-step instructions to sort numpy arrays with python. Website: https://opensourceoptions.com Sign up for email notifications (https://opensourceoptions.com/subscribe) and get $5 off any course at https://opensourceoptions.com/course-list
View Video Transcript
0:00
Hi folks, welcome to open source options. Today I want to show you how you can sort
0:06
numpeer arrays by rows and by columns. Now the code for all this is up on my website
0:12
open source options.com and I'll include this link in the description so you can go
0:18
there and see the code we're going to walk through in this video today. All right so I
0:23
have Visual Studio code open and we're going to jump right into this and get started. First of
0:30
we want to import Numpi. I'm going to assume you have NNP installed already. Most
0:38
Python distributions will have it. If not, you can figure out how to install that and I'll try
0:43
to get a video up on my channel that shows you how to do that. I'm just going to zoom in a little
0:47
here so you can see my code a little better. All right. So we've got import NMP as NP
0:55
The next thing I'm going to do is make an array called A and I'm going to make it full of
0:59
random integers so I'm going to do numpi.random.randt. And I wanted to go up to the value of 100
1:10
And I want the size to equal five rows and four columns
1:18
All right. And so now let's go ahead and print a and we'll do that
1:29
Okay. Looks like we're good there. I'm going to just hit Control S to save that
1:35
Now the next thing I want to do is have a way to run this code. Import Numpi as NP, not as NNP, and that should fix the error there
1:47
Once I save it. Okay. So now I'm going to run this script from the console, from the Anaconda prompt
1:54
And if you're not sure how to do that, I'll show you that right now. I'm going to go down here
1:58
I'm going to go to Anaconda, my Anaconda 3 prompt. And I'm just going to check that I have Python and Numpy install
2:04
So I'm going to type Python. It brings up an interactive prompt. I'm going to type Import Numpy and no error
2:11
So I'm going to quit there. And we're good to go. So now I can CD into where my code is
2:24
Okay. And I'm there. and now I just need to use the name of this script with Python to run us
2:31
I'm going to type Python, sort, and use tab to autocomplete, sort rows columns
2:37
I'm going to hit enter, and you can see it prints out this random array here
2:43
All right. Now because this is random, this is going to change every time I run the code
2:49
And so we'll just print this out as a point of reference to come back and look at our starting array
2:53
All right, so we first have this random array set up or printing it out
2:57
Now the next thing we want to do is sort this array And I going to start out by sorting it by a column Okay so to access a column with Numpy we going to use our square braces to index it So this is the colon gives me all the
3:14
rows. The rows are the first position and zero gives me the first column. Okay, and so I'm just
3:20
going to print this out to show you what it looks like. It will be a one-dimensional array
3:25
Let's go over. Um, and it will have five values in it
3:30
We have five rows. We're getting every value for a column, so we'll have a one-dimensional array with five rows
3:36
And let's go ahead, go back to the Anaconda prompt, and I'm going to come up and run this
3:43
Oh, I didn't save the script, that's the problem. I should get an auto-save turned on there, but I don't have it yet, so we'll just deal with it
3:51
Okay, and so you can see here's my first array, and here I have 59, 55, 69, 69, 85, 69, which is this first row right there
4:00
Now let's say I want to sort this row. Okay, I want to get those, or the sort this column
4:05
I want to sort those based on their value. I can go ahead and do that using argsort with Numpy
4:14
So first, let's just do print first column, just so we know what we're doing here
4:25
Then I'm going to go print first column, arg sort result and then what we're going to do is after that we're going to come down and we're going to do
4:37
first column sorted okay so we want to take a look at the arg sort result first and so the way we do this
4:44
so if we want to sort those we're going to go a we're going to get the first column with our
4:50
indexing here we're going to do dot argsort and close the function braces okay and let's just take a look
4:58
what this return and I'll tell you and then I'll go over it when it comes up. What this is going
5:03
to do is it's going to return the indices that would sort the array. Now I know that sounds a little
5:10
confusing but what it means is let's say that the smallest value is in the second or the third
5:20
let's say it's in the third location in that column. Then we would get the value two which
5:26
indicates the third element be the first value in this return of arc sort
5:31
And I'll show you what it means here. So let's just print this out, actually. So we'll print this
5:40
That, and let's go back over here. I need to save that. Come up here, and let's hit enter
5:47
All right, so we have our first column here. Okay, 352, 81, 87, 78
5:54
You can see our first column got printed out the same way. right there and here's our arg sort 01243 so you notice that the first element element
6:03
is the smallest and these three are actually in order but those last ones are switched and so that tells us those are the indices we want to get in order to sort this column
6:16
Now, now that we know that, we can sort a column pretty easily
6:22
And so if we do A, sorry, hold on just a sec here
6:28
Okay, so if we just want to get the first column sorted, we could save it as a variable, which would work
6:39
Or what we can do is we can do A, and we can, let's see here, then we can do, we're indexing it, so we want to get the row into C, which is going to be these ones here, so we can do A, 0
7:02
org sort and actually we're going to do this so we sort the entire array
7:09
so we want to get the entire array sorted based on those first values
7:14
and so we'll get that here I'm going to print this so we can show it
7:19
so this is going to get us all those columns returned in this manner
7:31
And I'll show you what that looks like. Let's go ahead and run the script and you'll see
7:36
I'm going to go up, I'm going to hit enter. Okay, and this is the whole array sort, not the first column
7:42
but you can see here, 86, 11, 49, 7513. You can see that comes across right here
7:49
We get our indices that would sort those. So the first one, 11 is the smallest
7:56
which is in the second position, so a value of one, 13 is the next, so the position of 5, value of 4
8:02
You can see when we do that, that the corresponding values all across there come back sorted
8:09
We have 11, 13, 49, 75, and the row values are all the same
8:16
Pretty cool, huh? That works out really nicely. Okay, now let's talk about how we can just sort that first column by itself
8:23
and it's pretty easy. We're going to do the exact same thing
8:27
We're going to do A, A, A, 0. We're going to do dot arg sort
8:37
And we're just going to add a column reference, which is going to be column zero
8:42
Let's print that out. Whoops. Like this. And I'm going to print out another thing here so we can see what it says
8:54
That's going to be first column. sorted this is going to be entire array sorted and let's just throw this down here and let's go ahead
9:06
and save that and let run this and see what we get so we run that code there oh and by the way the way i running the same line of code in the command prompt anaconda is I hitting the up arrow and it goes to the most previous command you entered
9:23
Okay, so let's take a look here. So here's our first one
9:27
We have 62, 78, 53, 66, 16. So 16's the lowest. We come down here, these are all in numerical order
9:37
We come down to first column sorted, and those are in numerical order. Cool
9:41
we're almost done here I want to show you just how we can easily change this so that we
9:46
sort on a different column and the way to do that is to change the index we're sorting on here
9:52
So let's say I want to do this. We don't want to sort these based on column number two
10:02
So it's going to be a third column. Come down here, I'm going to change these two. And I'm not going to print out this. This is kind of
10:11
an intermediate step so we'll comment those out and we'll print uh we'll just print the column
10:19
so make this too and let's go ahead and run this I come down here to my
10:29
anaconda prompt and I'm going to run that and you can see here okay so
10:41
So column two is this one here, just a third column. And you can see that those are now in order here as opposed to what they were here
10:51
Now you'll notice that down here where it says first column sorted, those are not in order
10:56
and I'll show you why. That's because I didn't change this to two. I kept that at zero
11:00
So it took the indices from the sorted column two and applied them to the first column, which is incorrect
11:06
Now the way we can kind of solve this problem is, we can add a new variable up here and let's say we want to have a specific sort column
11:17
So we'll call this sort column, sort call and we can make that equals two
11:23
And then we're just going to copy this variable and we're going to come put it down in here so that we can, oops, put it right there
11:34
Anywhere I have this to, we're going to put it there, we're going to put it there, and we're going to put it there, and we're going to put it there
11:41
and that way if I want to change this I can change this to one or three or whatever and it will still work and I'll just show it to you with the first column and we should get a good value here so we can column one which is this column right here and you can see initially they're not in order in these two then here we sorted that entire array to be in order and down here our first column is sorted all right folks that is how you can use numpy arc sort to sort a
12:11
raised by columns and rows. I hope you found this useful. If you did, it helps me out to give the
12:16
thumbs up. As always, thanks for watching, and have a wonderful day
#Computer Education
#Distance Learning
#Educational Software