Locked lesson.
About this lesson
Let's discuss mathematical operations we can use in Numpy, including scalars.
Exercise files
Download this lesson’s related exercise files.
Numpy Operations.docx57.2 KB Numpy Operations - Solution.docx
55.5 KB
Quick reference
Numpy Operations
There are two main Numpy Math Operations; Array with Array Operations, and Scalar Operations.
When to use
Use these when you want to do math to the elements of your Numpy Array.
Instructions
To add, subtract, multiply or divide an array to itself: (given an array called my_arr)
my_arr + my_arr
my_arr - my_arr
my_arr * my_arr
my_arr / my_arr
To add, subtract, multiply or divide an array to any number: (given an array called my_arr)
my_arr + 100
my_arr - 100
my_arr * 100
my_arr / 100
You can use any Python math operators with either method.
Hints & tips
- Array With Array Operations: my_arr + my_arr
- Array With Scalar Operations: my_arr + 100
- 00:05 Okay, in this video I want to talk about NumPy operations,
- 00:07 specifically mathematical operations.
- 00:10 So we've got an array,
- 00:11 we want to do math to the individual elements inside that array.
- 00:15 How do we do that?
- 00:15 Well, there's basically a couple of ways we could do it.
- 00:17 We could use the array itself to do some math.
- 00:21 And we can use something called scalars.
- 00:22 Scalars are just basically numbers and we'll look at those in just a second.
- 00:26 So let's create an array, and I'm going to just call it my array.
- 00:30 Now in the last couple of videos, we just created some dummy np arrays, but
- 00:35 we can actually create them dynamically just by calling the arange function and
- 00:40 then passing in some ranges.
- 00:42 So if we want an array with from 0 to 10, we would just type in a range 0 11.
- 00:49 Now if we run this, nothing happens, but then if we come down here and
- 00:53 just do it like this, we can see here's our array.
- 00:56 So we just dynamically created some dummy data that we can play with.
- 01:00 So the first set of operations we're going to look at,
- 01:02 we call them array with array operations.
- 01:04 And we can just basically do any kind of math we want.
- 01:07 So we can go my_array plus, for instance, my_array.
- 01:11 And if we run this we'll see every element inside of our array gets added to itself,
- 01:16 because we're adding each array to itself.
- 01:19 So 0 plus 0 is 0, 1 plus 1 is 2, 2 plus 2 is 4,
- 01:23 3 plus 3 is 6, and all the way up to 10 plus 10 is 20.
- 01:28 We can do the same thing with subtraction, right?
- 01:31 So 0 minus 0 is 0, 1 minus 1 is 0 all the way up 0 all the way across, right?
- 01:39 We can do multiplication.
- 01:42 So 0 times 0 is 0, 2 times 2 is 4, 8 times 8 is 64,
- 01:47 all the way up to 10 times 10 is 100.
- 01:51 Now we can also do division.
- 01:52 Now this is kind of interesting.
- 01:53 Check this out.
- 01:54 When we do we get this warning.
- 01:56 And that's because what we're doing here is trying to take zero divided by zero.
- 02:00 We're getting this Nan.
- 02:01 That's a no warning.
- 02:02 So if you pull up a calculator and take 0 divided by 0 it's undefined right.
- 02:08 We remember that from math back when we're small children,
- 02:11 learning this stuff in school.
- 02:13 Also if you go any number say 5 divided by 0, you get, cannot divide by 0,
- 02:18 right, because that becomes infinity.
- 02:20 And normally if you're doing Python, and we can write down here and see.
- 02:24 We can take 0 divided by 0, and we'll just get an ugly error, right?
- 02:28 Division by zero not allowed, right?
- 02:30 Well, the nice thing about numpy arrays is, it allows us to do it.
- 02:35 It just gives us a warning, and then it returns a null object, right, NaN.
- 02:40 And we'll also see, if we tried to do 1 divided by 0 later.
- 02:45 With Python we get this huge error, with NumPy arrays,
- 02:47 we won't get an error at all, we'll just get a little warning like this.
- 02:51 And it will say infinity because any number divided by zero is infinity.
- 02:55 So that's interesting, you can also do exponents.
- 02:57 So double stars, and we get,
- 03:01 2 to the 2nd power of 5 to the 5th power, etc.
- 03:06 And you can see all of these big numbers put in there.
- 03:10 So those are arrays with array operations.
- 03:14 So we're just basically doing math using the array itself.
- 03:17 And the other way to do it is by using scalars like I mentioned at the beginning
- 03:20 of this video.
- 03:21 And scalars is just a fancy name for numbers, right?
- 03:24 So we can go my_array plus 10.
- 03:30 And that will just add 10 to every element in our array.
- 03:34 So zero plus 10 is 10, 1 plus 10 is 11,
- 03:38 5 plus 10 is 15, all the way up to 10 plus 10 is 20.
- 03:42 And again, same math operations we can do add, subtract, multiply, divide.
- 03:47 So we can subtract, we can multiply, right?
- 03:53 10 times 10 is 100.
- 03:55 We can divide.
- 03:58 Right now we get decimals because we're dividing.
- 04:01 Now, here, let's try this.
- 04:03 Remember I said when we divide by zero, that's infinity.
- 04:07 And a normal calculator won't let us do that.
- 04:09 But here we just get a warning and we get infinity, infinity, infinity,
- 04:13 infinity except the first one which is that NaN because that's again,
- 04:16 0 divided by 0, which is NAN number.
- 04:19 Very interesting and really, that's all there is to this NumPy operation.
- 04:24 So, this is data analysis, we're always going to want to do different types of
- 04:29 math on the elements inside of our NumPy arrays, and very,
- 04:33 very easy with NumPy using mathematical operations.
- 04:36 So that's all for this video.
- 04:37 In the next video, we'll look at universal functions.
Lesson notes are only available for subscribers.