[No audio]
All right, now that you know how to do
slicing and iterating through NumPy
arrays, let's do some more specific
operations, and that'd be stocking
NumPy arrays, so concatenating NumPy
arrays to each other, and also splitting
a NumPy array to smaller arrays. And yeah, I have
this NumPy array still here to what's,
let's start with stocking to NumPy
arrays. To do that, you may want to
create a new variable where you'll
save your big array and let it be equal to
numpy and the methods to stock two NumPy
arrays, actually, there are two methods.
One is horizontal stack, so hstack,
and that expects from you to pass two or
more NumPy arrays that you want to stack
horizontally. So let me pass
[No audio]
im_g, and
then the same array, it is not a
problem. If you had two different
arrays, you could do that. But
I'm just passing the same array
there. Now if you execute this, you'll
get an error, and I want you to see
this error so that you understand it. So
it says each stack takes one positional
argument, but 2 were given, which
means that a stack inside the brackets,
it gets only 1 argument, but we were
passing 2. So how do we go about,
I mean, concatenating, 2 NumPy arrays. We can
just pass one NumPy array there. So the
solution here is to have a tuple of
NumPy arrays, just like that. So a tuple
inside the input or the hstack method,
you know, execute that.
[No audio]
And here is a stacked
array. Yeah, so this is the first array
and then the next array, which in this case
happens to be the same one. But if you
want, you can add more,
[No audio]
and you get a longer array, which is not very good
looking like that. So you can print that out,
and yeah, now you see the difference.
So the first array, second,
and the third one.
[No audio]
And if you want to stack
vertically, you would want to do v there.
[No audio]
And yeah, as you expected this, we'll
concatenate the arrays, array in the
vertical position in the vertical axis.
Beware that if you try to concatenate
arrays that have different dimensions,
[No audio]
you will have, you'll get an error. So im_c
has 3-dimensions if you can
remember that from up here. Now you'll
learn how to concatenate and how about
splitting an array into smaller arrays.
Well, to do that, you could create a
variable and then numpy.horizontalsplit
for splitting horizontally, and then this
expects NumPy array, and how many arrays
you want to produce all of that? So
horizontally, let's say 3.
[No audio]
Now this says array split does not
result in an equal division, and the reason
to that is, you know, we're trying this array has
1, 2, 3, 4, 5 columns. So it's trying to divide
5 with 3 and NumPy cannot decide
how to do that. So should it give to you
a NumPy array with these two column first,
and then another NumPy with 2 columns,
and then the last NumPy array with 1
column. So NumPy cannot do that. What
you should do is maybe 5 there and
lst
[No audio]
and that should give you 5
different NumPy arrays, which in this
case happens to be, you know, 1 array
for each column, and you can do verticalsplitting.
This time we can try
3 because we have 9 rows there.
[No audio]
And yeah, we get 3 small arrays.
[No audio]
Note that this lst, so the split, what it
produces is a Python list of NumPy arrays.
so it's a plain python list
and after that you can access each of the
NumPy arrays if you like, you know
that. So it outputs the first array, and
yeah, that's about concatenating and
splitting a NumPy arrays. Hope I give
you a good overview of NumPy. I tried to
do some practical examples like opening
images. But this is still yet the
theoretical part. So NumPy is quite
theoretical, and you only understand it
when you get to use like this real world
examples like working with pandas, as we
did previously in the course and also
working with images. So we're going to
do some hardcore image processing here
in the next lectures, in the next
section, and you'll see how NumPy comes
in handy in there, and yeah, I hope you enjoyed
this and I'll see you later.