Array Functions
- 03:46
How to use Numpy for mathematical operations on arrays, including custom functions like square root and aggregation functions such as sum, minimum, and maximum, highlighting their efficiency in handling large datasets.
NumPy mathematical functions: https://numpy.org/doc/stable/reference/
Downloads
No associated resources to download.
Glossary
NumPy Numpy arrays PythonTranscript
On top of the standard mathematical operators like addition, subtraction, multiplication, and division. NumPy also comes with some custom mathematical functions like you see here, round square root, absolute value and power, which raises a number to an exponent. Now, a lot of these functions you have elsewhere in Python, but if you try to apply them to a NumPy array, you're gonna run into a problem. So you need to use these custom NumPy mathematical functions. You can find a full list of NumPy mathematical functions in a link just outside this video. For example, let's say that we wanted to take array z and calculate the square root of each of the objects in that array. I can use the NumPy square root function, pass it array Z, and I'm gonna define that as root Z. So I'll create a new array, and when I execute that cell, let's print it.
When I execute that cell, you can see I have the square root of each of the objects in array Z. Now, if I didn't want all of these decimal places, I could use the NumPy round function, pass it root Z, and then as my second argument, choose the number of decimal places, and that will give me the rounded version of those square roots.
NumPy also has some useful aggregation functions that you can use when you're looking at arrays with a lot of data that you wanna summarize. For example, sum, minimum, maximum, median, mean and standard deviation. Here I'm gonna print array Z so that you can see the whole array. And then we're gonna print the sum of the objects using the NumPy sum function, and then the minimum using the NumPy min function and the max using the NumPy max function.
So here we have the array printed. Again, the sum of the objects is 36, minimum is 6, maximum is 12, which we can verify by looking at it. NumPy aggregation functions are designed to accept an optional argument called axis. If you only enter the array as an argument without the optional axis argument, the aggregation function will apply to the entire array like you just saw. However, by adding the argument axis equals 0, you can aggregate across columns. And by adding the optional argument axis equals 1, you can aggregate across rows. Let's try adding the axis argument to this code we just wrote. I'm gonna go back to the sum function, and I'm gonna give it the argument axis equals 0, and that's gonna give us the sum across the columns. I'm gonna copy that argument and then I'm gonna paste it in my other two functions.
So now I'm going to get the sum, the min and the max of array Z across columns.
So you can see that now I have the sum in column one 16, which you can verify here. And the sum of column two is 20.
The men of the first column is 6, and the second column is 8, and the max of the first column is 10, and the second column is 12. If we wanted to, we could change the access argument to one, and that's gonna aggregate across rows instead of columns. So when I execute that cell, my sum across the first row is gonna be 14. The second row is 22, which you can see makes sense here. And my min is 6 and 10. The max is 8 and 12. So these kinds of aggregation functions can be very useful as you're looking at a large data set and trying to get a better understanding of your data using summary statistics.