All right now that you know how to do slicing and iterating through Numpy arrays
let's do some more specific operations and would be stacking Nympy arrays so concatenating
Numpy arrays to each other and also splitting a Numpy array to smaller arrays.
I have this Numpy array still here.
So let's start with stocking to Numpy arrays.
To do that you may want to create a new variable where you will save your big array and that would be equal to Numpy
and the method to stock two Numpy arrays
Actually there are two methods one with is horizontal stack so hstack. That expects for you to pass
two or more number arrays that you want to stack horizontally.
So let me pass.
Im_g
and then the same array.
It's not a problem. If you have two different arrays you can 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 hstack takes 1 positional argument but 2 were given.
Which means that the hstack
inside the brackets it gets only 1 argument but we are passing 2.
So how do we go about concatenating 2 Numpy arrays?
We can't just pass a 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 a hstack method. You execute that.
And here is a stacked array. Yes, 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
and you get a longer array which is not very good looking like that.
So you can't print that out and you know you see the difference.
So with the first array, the second and the third one.
And if you want to stack vertically you'd want to do this there.
And yeah, as you expected
this will concatenate the arrays in the vertical position, in the vertical axis.
Be aware that
if you try to concatenate arrays that have different dimensions you'll have,
you'll get an error.
So Ims has three dimensions
if you can remember that from up here. Now you learnd how to concatenate. And how both 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 a Numpy array.
And how many arrays you want to produce out of that?
So horizontally let's say 3.
Yeah, this says array spilt does not result in an equal division.
The reason for that is you know we're trying
This array has one two three four five columns.
So you're trying to divide five with three and Numpy cannot decide how to do that.
So should it give to you a Numpy array with this 2 columns first and then another Numpy array with two columns
and then the last Numpy array with one column.
So Numpy cannot do that.
Well you should do is maybe 5 there
and lst.
That should give you five different Numpy arrays which in this case happens to be you know one array for
each column and you can do vertical splitting. This time we can try three because we have nine rows there.
And yeah, we get three small arrays.
Note that this lst, so the split
what it produces is Python list of Numpy arrays.
So it's a playing Python list and out of that you can access each of the Numpy arrays if you like.
You know that. It outputs the first arrey.
And yeah that's about concatenating and splitting Numpy arrays. Hope I've given you a good overview of Numpy.
I try to do some practical examples like opening images but this is still yet in the theoretical part.
Numpy is quite theoretical and you only understand it when you get to use it like these.
Real world examples like working with Pandas as we did previously in the course and also working with
images.
So we've run into some hard core image processing
in the next lectures, in the next session and you'll see how
Numpy comes in handy in there.
And yeah, I hope you enjoyed this.
I'll see you later.