Locked lesson.
About this lesson
Numpy has universal functions built into it that we can play with and use. In this video, we'll discuss square roots, absolute values, exponents, and more.
Exercise files
Download this lesson’s related exercise files.
Numpy Universal Functions.docx57.1 KB Numpy Universal Functions - Solution.docx
55.5 KB
Quick reference
Numpy Universal Functions
Numpy comes with several universal functions.
When to use
Use these whenever you want to do a specific thing to your array.
Instructions
A list of Universal Functions can be found at:
https://docs.scipy.org/doc/numpy/reference/ufuncs.html
Given a Numpy Array named my_arr, here are some common functions:
np.sqrt(my_arr)
np.absolute(my_arr)
np.exp(my_arr)
my_arr ** my_arr
np.max(my_arr)
np.min(my_arr)
np.sign(my_arr)
np.sin(my_arr)
np.cos(my_arr)
np.log(my_arr)
Hints & tips
Login to download- 00:04 Okay, in this video I want to look at some universal functions that are built into
- 00:09 NumPy that allow us to do different operations on our arrays.
- 00:13 So we got the same array from our last video, we called it my_arr.
- 00:17 And we just set this equal to this np.arange.
- 00:20 So we generated some dummy data for us from 0 to 10.
- 00:24 Well, 0 to 11, but not counting 11.
- 00:26 So here we have that.
- 00:28 Now, like I said, NumPy comes with some universal functions built in,
- 00:32 that we can play with and use.
- 00:34 So let's just start looking at these.
- 00:37 If we wanted the square root of each item, we could
- 00:42 go np.sqrt stands for square root, and then just pass in our array.
- 00:47 And then boom, we get the square root of each of these items.
- 00:51 So the square root of 9 is 3.
- 00:54 You see right there, 3.
- 00:56 So very cool.
- 00:58 If we want the absolute value of each one we call absolute.
- 01:04 And these are all positive numbers so we get just the same ones.
- 01:09 If we went negative 10 here, and then run this again we get some negative numbers.
- 01:17 Well, if we run the absolute value of that we would see the absolute value
- 01:21 of negative 10 is 10, etc.
- 01:23 So that's kind of cool.
- 01:25 Go ahead and change this back.
- 01:26 We can call the exponent on each one.
- 01:30 Right, remember in the last video we did that by going my_arr times times,
- 01:38 for instance, my_arr or whatever.
- 01:44 We can also use a function to do that.
- 01:45 So that's cool.
- 01:48 And there's just a few of these I want to talk about and
- 01:49 then I'll show you where you can find a list of these.
- 01:51 We can find the max, what is the highest number, the max number?
- 01:55 Well, that's 10, right?
- 01:57 Interesting.
- 01:58 We could go min and that becomes 0.
- 02:01 Often you want to find what the highest number and the lowest number is,
- 02:04 the max and the min, so that's an easy way to do that.
- 02:08 We can find the sign, are these positive or negative, right?
- 02:12 So, 0 is neither positive nor negative, the rest of these are all positive,
- 02:16 so we get a 1 for that, all right?
- 02:18 If we came up here and went -10 again, and then run this again.
- 02:25 And then called this, we get negative, negative, negative, negative, for
- 02:30 all these negative numbers, so that's interesting.
- 02:33 Let me change this back.
- 02:36 We can do trig.
- 02:37 Remember trigonometry?
- 02:39 So you can call the sine or the cosine, right?
- 02:45 We could call logarithm, and
- 02:47 then we get an infinity because the log of 0 is infinity.
- 02:51 But for the rest of them, we get the logarithm from each one.
- 02:53 So very, very cool and very easy to use just by passing in
- 02:58 your array into whatever function, universal function you want to use.
- 03:03 And I mentioned there's a bunch of these you can draw on.
- 03:05 So let's open another web browser, and this is the official documentation for
- 03:11 NumPy and it's just docs.scipy.org/doc/numpy/reference/ufuncs,
- 03:18 universal functions.
- 03:21 And this is the page for the universal functions.
- 03:23 Then if you scroll down towards the bottom, you can read all about this if you
- 03:27 want but the thing we really care about is we get down here and
- 03:30 we start seeing a list of all of these functions.
- 03:32 Now we did math in the last video, math operations.
- 03:35 You can also use each one of these universal functions to do your math too.
- 03:38 I figure, I feel like using the math we did in the last video is easier than
- 03:43 calling the add function if you want to add, but
- 03:46 you can always do that if you want.
- 03:48 So here are all of these.
- 03:52 So there's a bunch of math operations, right?
- 03:55 There's the square root we just did, you can find the square, reciprocal,
- 03:59 all kinds of cool things.
- 04:00 There's all the trig functions you would expect, sine, cosine, tangent, etc, etc.
- 04:06 Bit-twiddling, we probably won't look into any of this stuff.
- 04:10 Comparison functions and some other things, floating functions.
- 04:14 So very cool.
- 04:14 So check this resource out if you want to refer back to this in the future.
- 04:19 But pretty easy to use these universal functions with NumPy.
- 04:22 So that's pretty much all we're going to talk about with NumPy.
- 04:25 We're going to use NumPy throughout the rest of this course a lot.
- 04:29 And from time to time, I might show you some new thing with NumPy in
- 04:33 order to help us use it a certain way going forward.
- 04:35 But for the most part,
- 04:36 that's really all I want you to wrap your heads around at this moment.
- 04:39 Numpy arrays, they're just basically Python lists only they hold more data.
- 04:45 You can do math operations to them.
- 04:47 You can use functions on them.
- 04:49 And really, that's all we need to get started.
- 04:51 So in the next section, we're going to jump into Pandas.
- 04:53 I'll tell you all about it and show you how to install it and get it set up.
- 04:55 And that'll be in the next video.
Lesson notes are only available for subscribers.