Hello everyone and welcome to the pie in the kissing and selection lecture in this lecture.
We're going to discuss how to select elements or groups elements from a pie.
All right let's go ahead and jump to the Juber notebook to get started.
All right.
You're here at the Jupiter note book.
I've already said important umpires and P..
You know what I'm going to start off with is by creating a sample array I'm going to say the variable
A R R is equal to end p.
Got a range.
Just using tab to autocomplete there and I'm going go from zero to 10 and when I say zero to 10 I mean
the actual output in order to get that output I'll pass and at 11 up here.
So now I have an array of 11 elements that go from zero to 10.
Simplest way to pick out one or some of the elements of an array looks a lot like indexing from a python
list.
Basically you're going to use brackets and slice notation in order to do this.
You go ahead and do is type in the variable name of the array itself.
Passen square brackets and then to get a single value at an index which you can do just passing a number
.
Meaning if I want the value at index 8 I can pass an 8 and I will return back the value in x 8 in this
case.
It's actually 8 itself since I just used arranged to create this array if I want to get the values in
a range just like a python list.
I can use slice notation meaning I can say the starting index and the stop index.
So if I go ahead and say one colon five it will start index 1 which is the number 1 and go all the way
up to index 5 which is the number four because indexing starts at zero.
So to show another example of this I can say zero colon 5 and that returns all the values from 0 all
the way it's index 5.
I can also remove the stop point or the start point to go ahead and indicate that I want everything
else in the array.
Everything from the beginning of the right.
So for instance if I want everything up to index 6 instead of specifying the starting parameter as 0
I can just put a colon and then put 6 Ellora's turn everything up to the start of the array to index
6.
So that's the same thing as saying zero colon 6 but I save a little bit of typing by just putting in
the coal itself.
Similarly what I can go ahead and do is say array in if I want to start at a particular index and grab
everything up to the end of the array I can use the same station I did in a Python list by using slicing
notation for coal.
Something to note when you're using this notation of a number and then a colon is that you're not actually
grabbing at index 5 and beyond.
You're grabbing everything be on index 5 because remember indexing as far as notation in Python starts
at zero.
So 5 here is actually index 6 meaning this notation of five Colon says scrap everything beyond index
5.
So that's going to be five six seven eight nine ten because remember index 5 is actually here at four
.
Since indexing starts at zero you have grabbed five elements.
Meaning if I say five colon it grabs everything beyond the fifth index which occurs at four and you
can go ahead and review Python list slice notation if you want more information about this colon.
But essentially this works exactly the same as it does for a normal Python list.
No arrays differ from a normal Python list because of their ability to broadcast.
Me go ahead and show an example of this.
I'm going to take my array and grab elements.
Zero and five.
So it's going to be zero one two three four and then I consider it equal to the number 100.
And what that it's going to do.
It's going to broadcast that value to those first five digits to all be 100.
I'm going to go ahead and reset the array and you'll see why in just a second if I show the array I'm
back to 0 1 2 3 all the way up to 10.
I don't want to show you an important no on slices of it or rate.
If I go ahead and create a variable called slice under score of underscore are for Array.
And I said equal to a slice of the array for instance 06 It's good to see that slice of the array here
we have the arrays there are one two three four five.
Basically that first chunk of the half of the array.
If I broadcast on the Saray weenie I say slice of array I use a colon.
That means I'm grabbing everything in that slice.
I try to broadcast this to be another number such as 99.
That means when I check slice of array I see a bunch of 99.
Here's where you have to pay careful attention.
If I called back the array it's actually changed 99 not just on the slice but on the original array
.
I had called two.
So you should know how that change also occurs in the original array meaning the data is not copied
.
It's just a view.
So the original array.
And the reason it does that is to avoid memory issues with very large arrays meaning no pi is not going
to automatically set copies of arrays.
If you actually want a copy and not a reference to the original array which you can do is actually specifically
specify copy.
So I can do something like this.
I can go ahead and say r underscore copy and use the copy method off of the array to specifically indicate
that I want a copy.
So if I take a look at my array it's 9 9 9 9 9 x cetera and then 6 7 8 9 10.
And if I take a look at my array copy it's also the same thing.
But if I do a change to my array copy maybe by broadcasting every value to be 100.
And I check out array copy every value is 100.
But that original array is still unaffected by that broadcast in the command to 100.
Go ahead and review the notes on this.
This is a little confusing to you but the basic premise here is that if you actually grab a slice of
the array and set it as a variable without explicitly saying that you want a copy of the array you should
keep in mind that you're just viewing a link to the original array and that changes you do will actually
affect that original array.
OK.
Let's go and talk about indexing a to the array.
Otherwise known as a matrix.
I'm going to go ahead and clear all the cells here for a second.
All right I just cleared all the cells here and we're going to do is go ahead and create a to d array
.
I'm going to say N.P. array and I'm going to pass in a nested list.
So I'm going to say 5 10 15 20 25 30 and then finally 35 40 45.
So I want to go out and show that to the array and here I have three rows with three columns it's to
them missional matrix.
Great.
All right.
There's two general formats for grabbing elements from a to the array or Matrix.
There's the double bracket for it.
And then there's the single bracket formit with the comma.
Let me go to show you the double bracket format and then I'll show you an example of the single bracket
format which is the format I would recommend to use but you should know both just in case you encounter
them in the wild.
So again there's my two theory.
Well let's say I wanted to grab the digit 5 so that's in the very first or index 0 row and in the very
first column index 0 for double bracket notation I can pasan first the row I want and then the column
I want.
So passing in 00 here will return the digit 5.
Since that's the elements in the first row in the first column.
What I can do is delete and just do a single set of brackets and index an entire Roback.
So this will index the entire row of index 0 meaning 5 10 15 and if I want to let's say the element
25 Well that's index one for the row in its index one for the column.
So I can say 1 1 and this all return 25.
What I want you to do is try to see if now you could pause the video and try to grab elements from the
array individually using this double bracket notation.
All right.
Let's say we want to grab 40 as just one last example.
That's a row index too.
And it's a column in next one.
So that's 40.
That's the double bracket notation.
Well you should do.
However I would recommend is using the comma single bracket notation.
And basically what that does is it replaces the double set of brackets with just a comma and you pass
in the row comma column number.
So for instance let's say I wanted the digit 30 I would pass in index 1 because it's index one to row
and then it's in the third column.
So 0 1 2 it's in the index to column and that gives me the 30.
And that's the same as just saying what are underscored to me and doing it with double brackets I would
recommend that you always use this single bracket of a common notation because usually it's less prone
to error.
Now let's imagine that you didn't want single elements but actual chunks of the.
For instance you want some matrices from this matrix you can use colon for slice notation Nurit to grab
certain sections of the entire to the array and sort of just single elements.
I'll go ahead and show you how to do that.
We're going to delete this second cell and show you a couple of examples.
Let's say we wanted to grab from the top right corner.
Meaning I want to say 10 15 and 25 30 so this top right corner here's what I want to grab when I can
go ahead and do is use slice notation in order to do this.
So I can say grab everything up to call a row to and then grab from column 1 onwards and that returns
10 15 25 30.
Let me go in and break this down one more time.
As far as the rows What we're saying is grab everything but not including rows 0 and 1 as the colon
.
But then don't include two.
I'm saying slice it too and I'll go ahead and show that it's just a single command so I say Colon's
to that returns these two rows row 0 and row 1 then by adding on top of that I'm saying as far as the
columns go grab everything from Column 1 all the way to the end which basically means just drop all
over this column 0 and that returns these two little subsections 10 15 and then 25 30.
All right.
What I would recommend you do now is to in your mind grab subsections for instance 20 25 30 and 35 40
45 and see if you can use slice notation along with this single brackets comma indexing notation with
pi in order to grab subsections of the Saray.
And I encourage you to also see if you can make a bigger and grab larger subsections OK hopefully have
enough practice with that if you want more examples.
You can always visit the notebook for this lecture which has a few more examples of 2-D array slicing
throughout the course we won't do too much of this sort of fancy index slicing So it is confusing to
you.
You don't have to worry about it too much since he won't really actually do it that often throughout
the course.
Well I usually just grab simple either single elements or entire rows or columns.
They don't usually grab subsections of subsections like we did here.
All right.
Let's go ahead and move on to the more important method of selection which you're going to use much
more often in this course which is conditional selection.
I'm going to go ahead and create a new array just a single mention array say and Pido arrange one comma
11 and check out this array.
OK.
So for this array I have one two three four five six seven eight nine 10.
What I can do is I can take this array combine it with comparison operators to actually get a full boolean
array out of this.
And what I mean by that is if I compare this to a single digit for instance a are bigger than 5 I will
get an array of boolean values.
Notice it says D type B O L.
So these are true or false values.
And it gives me false wherever that comparison is false and true or that comparison was true.
And then I can go ahead and save this as maybe you'll underscore array.
So if I call bool underscore right now I get back this boolean array.
Now you can use that to actually do conditional selection.
Meaning I can pass that in two brackets
and I will only get the results where this boolean array happened to be true.
Let's go to break that down one more time getting the array and using a comparison operator on it will
actually return a boolean array meaning an array of all boolean values.
Then I can use that boolean array to actually index or conditionally select elements from that original
array where this happened to be true.
So only returns instances where that boolean array was true and more commonly sort of splitting this
up into two steps.
You would actually do this all in one step.
You would say array and in brackets instead of the array you would pass in the conditional statement
such as a r r greater than 5 and that will return the same answer.
And this is the sort of syntax notation we're going to be using all the time as far as conditional selection
.
So for example let's say we wanted to know all the elements of the array that are less than 3.
What I would do is I would say array brackets and then say array less than three and that return all
the elements where that is true and that's the sort of thing we're going to be using a lot in this course
especially a move on to pandas.
This is the sort of notation you should get used to.
I know it's a little weird at first to see the variable name inside of itself but hopefully splitting
it up into these two steps makes it clear what's actually going on.
You're just passing in a boolean array of values and then you're just returning where ever it happens
to be true at the index of the original array.
All right that's it for this lecture.
What I would recommend doing as far as an exercise for yourself is to create a new to the array by saying
N.P. dots arr..
Well go ahead and say 50 and then reshape this to be a 5 by 10.
No it isn't just calling a method off of another method.
So this is all one array and then you have this nice five by 10 array.
So 0 to 40 and then 0 to 9.
I would recommend for you just practice for yourself.
Try to in your mind grab chunks and then see if you can use bracket notation to grab the chunks that
you thought of.
So for instance maybe you want to grab your 13 14 and 23 24.
Let's go out and show how you would do that.
First I want to break it up in two rows and columns.
So 13:14 that starts at row 0 1 and then 2.
Meaning I want to grab rows 1 through 2.
I'm going to go ahead and say one and remember with this sort of slice notation and go one more than
you actually need.
So I'm saying grab for one all the way and not including index three.
So that should be these two rows.
Let's go in and confirm that over here by just saying are your score to the grabbing those rows.
One call and three and this is basically working just like normal Python list notation.
So here we have index one in next to up to but not including three.
And then for the columns and say I want 13 14 and 23 24 that occurs at index column three groups and
then I want four not including five saga but at five there that returns what we were looking for 13
14 23 24.
I would go ahead encourage you to practice this by setting up the survey in your own Jupiter note book
and then just grabbing chunks of this matrix as you see fit.
All right.
There's a lot more indexing help in the notebook.
Don't worry a whole lot if you don't feel 100 percent comfortable this you'll get more practices in
the final exercise of the PI.
But really this is just to let you know what some pie is capable of.
We won't do this sort of slicing notation that often in the course using this called notation.
We'll use the conditional selection using person operators.
A lot more.
All right thanks everyone and I'll see you at the next lecture