‫Okay.
‫Now we've seen a few different ways to manipulate arrays, but most of the ways we've worked with have
‫been arithmetic or aggregation.
‫So kind of performing calculations on an umpire raise.
‫Now what if you needed to change the shape of your numpy array?
‫Now, why would you need to change the shape of an umpire, Ray?
‫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.
‫And 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 NumPy.
‫So we'll go reshaping and transposing.
‫This is this section.
‫Nice little heading there.
‫So let's have a look.
‫A 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 a two by a three.
‫Let's visualize a three so we can see it again.
‫A three.
‫There we go.
‫Let's check out the shape.
‫A three dot shape.
‫Okay.
‫What's going to happen if we try to multiply these?
‫Ah, operands could not be broadcast together with shapes.
‫Two, three or two, three, three.
‫Well.
‫The beautiful thing about numpy arrays is that they can be reshaped or transposed.
‫Let's have a look at Reshape first.
‫So what does Reshape do?
‫Reshape is a method you can call on any array.
‫So if we type in here.
‫Reshape shift tab.
‫Reshape docstring.
‫Returns an array containing the same data with a new shape.
‫Beautiful.
‫There we go.
‫Get some more information there.
‫But let's see what it requires.
‫Shape.
‫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, numpy 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 A to 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.
‫So reshape two, three, one.
‫Let's see what happens.
‫Ah.
‫Okay, so it's shuffled our way around a bit.
‫Let's print the shape here.
‫So you see our way to we've used the reshape method past in a different shape.
‫So now we've added another dimension here, and now its shape is two, three, one.
‫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 a variable A to reshape equals A to reshape.
‫Two, three, one.
‫Beautiful.
‫So we'll have a look at this.
‫A to reshape.
‫There's our reshaped A2, A2 Reshape Times A3.
‫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 a to to be within the broadcasting
‫rules of numpy.
‫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 you're an umpire, Ray 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 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 a to actually we might leave it here A to T so we can see what's changed here.
‫Let's get a two back up A to Z.
‫Transpose what's changed.
‫Let's go to shape.
‫Three.
‫Two.
‫Okay.
‫Now, if we go here.
‫Eight to shape.
‫So what a transpose does.
‫It switches the axes.
‫So now a two shape was two three.
‫But if we call transpose and now T is four transpose, it's going to swap the axes around.
‫That's where we can see that.
‫Now, 1 to 3.3 is down here in the column and four five, 6.5 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 a3a3 point shape, two, three, three.
‫So if we call a3t.
‫It's going to swap these axes around.
‫So if let's see, a three point shape.
‫So now we've essentially flipped it.
‫So now this dimension, so too is right at the end, and three and three have swapped to the front.
‫The important difference here between transpose and reshape is that transpose just flips the axes around
‫and reshape.
‫You can create your own custom shapes.
‫So for example, we reshaped A to 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.