‫Alright.
‫Alright, alright.
‫So in the previous few videos we've had a look at different ways of creating numpy arrays.
‫We've seen how we can view them.
‫Now let's figure out the fun part.
‫Let's figure out how we can manipulate and compare our different arrays.
‫This is where the crux of machine learning happens, right?
‫It's manipulating performing calculations, finding patterns in arrays of different numbers.
‫So let's start it off.
‫We'll put in another heading because we're keeping our notebooks nice and neat, manipulating and comparing.
‫Have I spelt it right?
‫I usually get typos in these headings.
‫Manipulating and comparing arrays.
‫Beautiful.
‫We're going to put a little subheading here because like pandas with numpy, if you can think of a way
‫of manipulating numbers, you can probably do it.
‫So the first one we're going to look at is a riff matic.
‫Here we go.
‫Lucky encoding is a little bit better than my spelling ability, but arithmetic is a fancy word for
‫basic math equations.
‫So let's have a look and start off by checking out our A1 array.
‫We created this earlier.
‫A1 wonderful.
‫We'll create another one called ones.
‫Actually, we've already got ones.
‫Yep.
‫Beautiful.
‫But if you wanted to recreate it, we'll go empty ones three.
‫Just to make sure we know what it looks like.
‫One's wonderful.
‫Okay.
‫So we're going to have a look at is nice and simple plus so what do you think this will do if we go
‫one which is our right here plus ones?
‫Well, let's not talk about it, Daniel.
‫Let's run the code.
‫Beautiful.
‫I one plus ones.
‫So we've got one plus one equals two, two plus one equals three, three plus one equals four.
‫Excellent.
‫So all this is done has just added together the different elements in the same position of both arrays.
‫So the zero position, one plus one equals to the first position, two plus one equals three.
‫And the second position is three plus one equals four.
‫All right.
‫Well, let's keep going.
‫Let's see a few more options.
‫Hey, one minus ones.
‫Can you guess what'll happen here?
‫012 so all it's done is it subtracted ones from a one.
‫Let's push forward.
‫Well, let's try multiply I one times ones.
‫Yeah, well, that makes sense because our ones array is just all ones and it's just times a one by
‫one.
‫One times one is one, one times two is two and so on.
‫Let's bring in another array.
‫A two.
‫Here we go.
‫Now, this will be interesting if we multiply a one by a two.
‫What's going to happen?
‫Huh?
‫Let's see.
‫Let's work through this.
‫So we've got one.
‫This is.
‫Let's get up a one again so we can see everything in one place.
‫A one.
‫One times one is one, two times two is four.
‫Three times 3.3 is 9.9.
‫That makes sense.
‫But what about this second row here?
‫Hmm.
‫Let's see if we can figure this out by looking at it.
‫One, four, four, two times five is ten.
‫Yep.
‫Three times 6.5 is 19.5.
‫Let's use Python to help us out for that.
‫6.5.
‫It's 19.5.
‫Ah, we can get rid of this cell.
‫We don't want that.
‫Actually, never use a toolbar escape X to get rid of that.
‫So what this has done is it's taken a one these three numbers here multiplied it by this row, put it
‫in here, and then done the same thing again with a one and multiplied it by this row.
‫Huh.
‫That's interesting.
‫Let's have a look at our other array first A3.
‫Let's combine these two A2 and A3 A2 times A3.
‫Operands could not be broadcast together with shapes.
‫Two, three.
‫Two, three, three.
‫Huh?
‫Operations could not be broadcast together.
‫That's interesting.
‫I remember right at the start, at the concept video where we talked about when numpy is fast because
‫it vector arises code through a technique called broadcasting.
‫Well, this is what's happening here.
‫But to be that fast, broadcasting has a few rules.
‫So let's look that up.
‫Num py broadcasting.
‫There we go.
‫We're going to go to the documentation broadcasting num.
‫Click on that.
‫The term broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations,
‫which is exactly what we're doing.
‫We're trying to use some arithmetic on arrays with different shapes.
‫So there's a three and there's a two.
‫They've got different shapes and that's what the error is coming up.
‫So let's have a look here.
‫Subject to certain constraints, the smaller array is broadcast across the larger array, so they have
‫compatible shapes.
‫Well, that explains what's happened up here with a one times a two.
‫The smaller array is broadcast across the larger array, so they have compatible shapes.
‫That makes sense because we've got here this is a smaller array.
‫It's been broadcast across this row and then across this row so that the output has a compatible shape
‫of the larger array.
‫Okay, we'll keep going.
‫There's some examples here.
‫So these are two arrays multiplied.
‫They've got the same shape.
‫One, two, three to 2 to 8.
‫Times B is one times two is two.
‫Two times two is four, three times two is six.
‫Aha.
‫That makes sense.
‫And then broadcasting rule relaxes this constraint when the arrays shape meet certain constraints.
‫Okay, so this is good.
‫This is the kind of workflow you want to start getting into, right?
‫Is if you come across an error like this and you will all the time one of these mismatched shapes,
‫errors like different shapes that are incompatible with each other, a value error.
‫These are going to come up all the time in machine learning.
‫And part of being a data scientist or a machine learning engineer is reading documentation like we're
‫doing now and trying to figure out how to fix our problem.
‫So General Broadcasting rules.
‫Okay, 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.
‫Now to save a little bit of time instead of going through the entire documentation, I'll leave that
‫for you.
‫I want you to have a think about try and figure out a way.
‫How would we get these two to be of compatibility?
‫How would we fulfill this broadcasting need?
‫So what you may want to look into is how can you reshape a to to be compatible with a three so that
‫might be looking at search put in another comment here how to reshape numpy array.
‫So check out the broadcasting documentation along with searching something like this and then figure
‫out how to make these two shapes aligned.
‫If you can't, that's entirely fine, right?
‫This takes a bit of practice, but we're going to see how to do it in a future video.
‫Right now, we're going to continue with our arithmetic just to finish things off.
‫So where were we?
‫We've done multiplication.
‫Let's try a one divided by ones.
‫So this little slash is four divided by and you can probably predict what's going to happen here.
‫Beautiful.
‫It works fine because our arrays are similar shapes.
‫Okay, let's try a two.
‫Let's try some floor division eight two divided by a11, two, four.
‫But we'll see what non floor division works here.
‫There we go.
‫So floor division, what this means if you do two slashes, you might have seen this in Python.
‫If not, that's fine for division removes the decimals so you can see here it rounds it down.
‫Brands.
‫Dan So 1.01.0, 1.0, 1.1.
‫But that's been rounded down to 12.166667 has been rounded down to two.
‫Let's keep going.
‫What have we missed out on?
‫Let's go square A to square two.
‫This is a power.
‫So to the power of so everything in here is going to be to the power of two.
‫So if we look at a two on its own, it's going to be one times one.
‫Two times two is four, one times one is one, 3.3 times 3.3 is 10.89.
‫We'll confirm that with Python.
‫Wonderful.
‫10.89.
‫That's just filled it in there and rounded it off.
‫Actually NumPy has a square you can use with this.
‫So all of these functions that you see here that we've gone through arithmetic, flawed vision, etc.,
‫NumPy has the equivalent here.
‫Now, which one should you use?
‫Well, it's going to depend on what your workflow is best at.
‫So if you want to use the math symbol operations, you can use that.
‫Or if you want to use square or you can even go NP add going to go shift tab to figure out what it does.
‫Let's figure out here docstring adds arguments element wise.
‫So let's try that a one ones.
‫Same as just doing a one plus one.
‫Wonderful.
‫I've got a couple more.
‫I won modulo.
‫This is going to show us the remainders.
‫So a1 divided by two.
‫Let's see where that goes.
‫A one divided by two.
‫Let's go.
‫NumPy modulo.
‫Maude.
‫There we go.
‫Return element wise remainder of division.
‫Wonderful.
‫So this is again, I'm trying to exemplify the kind of workflow you go towards if you're not sure what
‫something does.
‫You can always look up the documentation and have a look.
‫That's what we did with broadcasting.
‫We've just done that with modulo.
‫If we divided every element in a one modulo two, it's going to show us the remainder that's left over.
‫So let's do a two, modulo two.
‫So this is just showing us the remainder of what's left after we've divided everything by two.
‫And if you wanted the exponential, if you're not sure what this is, don't worry.
‫For now, it's just to highlight the kind of arithmetic that you can do in numpy is going to take the
‫exponential of everything in an array and if you want the log, you might see this come up in future
‫projects of an array you can use NP log.
‫So that was a bit of a whirlwind tour of some of the arithmetic functions that you can do in NumPy.
‫Remember, if you can imagine it, you can probably do it.
‫This is only just scratching the surface.
‫So before next video, we have a practice around creating some of your own arrays and then using some
‫of these arithmetic functions on them.
‫Remember, if in doubt, run the code if you're not sure what something does.
‫Have a look at the numpy documentation.
‫See if you can figure it out.
‫I'll see you in the next video.
‫We're going to cover a little topic called aggregation, but we'll save that for the next video.
‫See you soon.