OK now we've seen a few different ways to manipulate a raise but most of the ways we've worked with
have been arithmetic or aggregation.
So kind of performing calculations on Empire right now.
What if you need to change the shape of your name higher array.
Now why would you need to change the shape of an umpire right.
Well remember a lot of machine learning is lining up your inputs into a machine learning algorithm and
making sure that they come out with the right output.
One of those ways of lining up inputs is making sure your data or your list of numbers or your array
of numbers is in the right shape.
So let's have a look at how to do that in num pi.
So we'll go reshaping and transposing this is this section.
Nice little heading there.
So let's have a look.
A 2 array.
Beautiful.
Let's figure out what shape it is.
Two by three.
Yeah that makes sense.
Now remember before in a previous video we tried to multiply A2 by A3.
Let's visualize a three so we can see it again a three.
There we go.
Let's check out the shape a 3D up shape.
Okay now what's going to happen if we try to multiply these operations could not be broadcast together
with shapes 2 3 or 2 3 3.
Well the beautiful thing about an umpire rains is that they can be reshaped or transposed.
Let's have a look at reshape first.
So what does reshaped to reshape is a method you can call on any array.
So if we type in here dot reshape shift tab reshape dock string returns an array containing the same
data with a new shape beautiful.
There we go.
Get some more information there.
Let's see what it requires shape.
Mm hmm.
So one of the rules of broadcasting if we look up broadcasting one more time
one of the rules of broadcasting is here.
General broadcasting rules when operating on two arrays num pi compares their shapes.
Element wise it starts with the trailing dimensions and works its way forward.
Two dimensions are compatible when they are equal or one of them is one.
So what this means is that if the shapes of two arrays are equal the multiplication can happen but if
they're not equal one of them has to be one.
So that's what we're going to do here.
We're going to reshape the A2 array from its shape to three incompatible here.
And we're going to change one of these dimensions we're going to add a dimension actually to reshape
2 3 1.
Let's see what happens.
OK.
So it's shuffled our array around a bean.
Let's print the shape here so see our A2.
We've used the reshape method passed in a different shape.
So now we've added another dimension here and now its shape is 2 3 1.
We'll check a three shape okay two three three.
So if we refer back to the rules of broadcasting are the dimensions equal.
No.
Is one of them one.
Yes.
So let's try this.
A two will make a variable a to reshape Eagles a to reshape 2 3 1 Beautiful.
So we'll have a look at this a to reshape there's our reshaped a to a to reshape times a 3 There we
go.
Beautiful.
So that is the benefit of reshape.
Now this is only a small example here.
Before they were incompatible but because we reshaped it we reshaped A2 to be within the broadcasting
rules of num pie.
We could then use the multiplier function across these two arrays.
Now if this concept is kind of strange to begin with that's perfectly fine.
And it will be.
It will take a fair bit of practice to figure out how to use reshape properly but what the important
takeaway here is is that just because your name pie array comes in a certain shape like it begins like
this doesn't mean it has to stay in that shape so this reshaped version of A2 still contains the same
information as the original A2.
It's just in a different shape.
So this is one of the problems you'll come across in many different machine learning problems is making
sure that the data that you're trying to find patterns within is the same shape.
So the machine learning algorithm can run over it like this broadcasting operation as quickly as possible.
And if they aren't in the same shape you'll often get back errors like this which say the data you're
trying to find patterns in isn't the same shape.
So that's when you have to figure out how to use something like reshape to turn them into the same shape
so that they're compatible with each other.
Now there's one more thing which is transpose now what transpose is let's have a look at it first.
Let's put a little comment here so we know what's happening.
Transpose A2.
Actually we might leave it here.
A2 Doc T.
So we can see what's changed here.
Let's get A2 back up A2 there transpose what's changed.
Let's go dot shape 3 2.
Okay.
Now if we go here Hey to shape.
So what a transpose does it switches the accesses.
So now A2 shape was 2 3.
But if we call transpose and now t is 4 transpose it's going to swap.
The axis is around.
That's where we can see that now.
One two three point three is down here in the column and four five six point five.
It is on this column.
So what it's done is essentially just swap the rows and columns.
Now you can do this on a larger array as well.
Let's view A3 A3 not shape 2 3 3.
So if we call a three dot t it's going to swap these axes around so let's say a three dot teed up shape.
So now we've essentially flipped it.
So now this dimension so too is right at the end.
And 3 and 3 have swapped to the front.
The important difference here between transpose and reshape is that transpose just flips the accesses
around and reshape.
You can create your own custom shapes.
So for example we reshaped A2 to be compatible with our broadcasting rules.
So have a practice reshaping some of your arrays.
And I'll see you in the next video.