All right, you know how to slice lists.
[No audio]
0 to 1 will give you the first item
of the list, and 0 to 2, the first
and the second item of the list, and so on.
With NumPy arrays, you do basically
the same thing. Except with NumPy
arrays, sometimes you have 2 or more
dimensionals, or 3. So let me go
ahead and I'll create, I'll start with a
2 dimensional array. So I'll get that
from these variable. So this is the NumPy
array that we'll be working with, and
yeah, if you want to extract this number
here, this here, this one, and this, so
these four numbers, well, you'd first needs
to set the index of the rows that you
want to slice. So that'd be 0 to 2, which
gives you the first 2 rows. Actually,
we can try that, and you'll see that you
get the first 2 rows. Now, if you want
only this portion here, so these
two numbers and these two, you would
want to pass
[No audio]
comma there, and now the index
for the columns, which would be, you
know, 0 here, one, so two, three,
and yeah 2 to 4 maybe, you know, that
should do it, and that's it.
[No audio]
That means you have this indexing system. So it
start from 0 for rows, and then row, the
second row is 1, and 2, and so on,
for columns are the same thing, 0, 1, 2, 3,
and 4, and actually, if you like, you
can see the shape of your NumPy array.
So it's 3 by 5, which means 0
to 2, and 0 to 4, great, and of
course, you can use the convention of
the list indexing. So you can pass 0 and
everything after that or just like that.
So you get all the rows and only columns
from 2 to 3. Similarly, you can
get only one value if you like.
[No audio]
Not particularly this one, because 3 is
out of bounds for axis 0. So axis 0
is the horizontal axis, which says that
there is no row with index 3. So we
set is 0, 1, 2. So let's pass 2 there, and you
get 182, which is the very last item
of the NumPy array. And yeah, that's about
indexing. How about iterating through a
NumPy array? Well, there are two ways to
do that. The first way is to say, let's
say for i in your numpy array,
print i. And what that will do is it will
print out the rows of your NumPy
arrays, and so what these i access is in
your NumPy array is the rows. So in each
iteration, it gets the first row and it
prints it out in the first iteration,
then the second one, and then the third one,
and if you want to iterate through
columns, you'd want to access the
transposed version of your NumPy array, and
that's how you do it, and lastly, if
you want to iterate value by value,
you'd say for i in image and .flat
[No audio]
print i.
[No audio]
So this properly what it
does is it allows you to access the
values of your NumPy array one by one.
And that's about indexing, slicing, and
iterating. I'll see you in the next lecture.