WEBVTT
1
00:00:00.005 --> 00:00:04.000
- NumPy has a rich collection of universal functions,
2
00:00:04.000 --> 00:00:05.003
or ufuncs,
3
00:00:05.003 --> 00:00:09.002
that you can use to eliminate loops and optimize your code.
4
00:00:09.002 --> 00:00:13.006
Universal functions are basically Python objects that belong
5
00:00:13.006 --> 00:00:18.008
to NumPy ufunc class and encapsulate behavior of a function.
6
00:00:18.008 --> 00:00:23.000
You have already experienced some of that in NumPy part 1
7
00:00:23.000 --> 00:00:25.002
where we learned arithmetic functions
8
00:00:25.002 --> 00:00:27.004
and statistical functions.
9
00:00:27.004 --> 00:00:31.002
Many other examples of universal functions can be found
10
00:00:31.002 --> 00:00:34.009
in trigonometry, summary statistics,
11
00:00:34.009 --> 00:00:37.003
and comparison operations.
12
00:00:37.003 --> 00:00:39.002
Let's see them in action.
13
00:00:39.002 --> 00:00:42.008
We have already imported NumPy as 100.
14
00:00:42.008 --> 00:00:46.008
Let's now create an initial array of integers to work with
15
00:00:46.008 --> 00:00:48.008
and call it numbers.
16
00:00:48.008 --> 00:00:52.006
Let's go ahead and try one of the trigonometric functions
17
00:00:52.006 --> 00:00:57.004
called sin that calculates sine of all the array elements.
18
00:00:57.004 --> 00:01:04.002
We'll achieve that with np.sin(numbers).
19
00:01:04.002 --> 00:01:06.008
Or in case we want to calculate the logarithm
20
00:01:06.008 --> 00:01:08.006
of all the array elements,
21
00:01:08.006 --> 00:01:13.007
we would just use np.log(numbers).
22
00:01:13.007 --> 00:01:17.000
As you can see, we don't have to write a for loop.
23
00:01:17.000 --> 00:01:20.004
The universal function is operating on array
24
00:01:20.004 --> 00:01:23.001
in a element by element fashion.
25
00:01:23.001 --> 00:01:26.002
Another cool thing about universal functions
26
00:01:26.002 --> 00:01:29.004
is that we can create our own universal function
27
00:01:29.004 --> 00:01:32.001
using three simple steps.
28
00:01:32.001 --> 00:01:36.001
First, use a def keyword to define a function.
29
00:01:36.001 --> 00:01:38.006
You'll pass the parameter into a function
30
00:01:38.006 --> 00:01:41.007
and it will return data as a result.
31
00:01:41.007 --> 00:01:44.002
If you're already familiar with Python,
32
00:01:44.002 --> 00:01:46.004
or any other programming language,
33
00:01:46.004 --> 00:01:48.005
then this is straightforward.
34
00:01:48.005 --> 00:01:53.002
Second, add created function to NumPy ufunc library
35
00:01:53.002 --> 00:01:56.004
using frompyfunc method.
36
00:01:56.004 --> 00:02:00.004
Third, call this function over a NumPy array.
37
00:02:00.004 --> 00:02:03.004
Let's create a function that takes the value
38
00:02:03.004 --> 00:02:07.000
and returns the remainder of dividing with 10.
39
00:02:07.000 --> 00:02:13.009
Our array will contain integers from 1 to 100.
40
00:02:13.009 --> 00:02:17.008
Let's call it integers and print it out.
41
00:02:17.008 --> 00:02:24.005
Next, we'll define a function called modular
42
00:02:24.005 --> 00:02:27.009
and it will return remainder of the value with 10
43
00:02:27.009 --> 00:02:30.007
and add it into NumPy by typing
44
00:02:30.007 --> 00:02:37.006
mod_10=np.frompyfunc(modulo,1,1).
45
00:02:37.006 --> 00:02:40.002
Lastly, we'll use our function modulo
46
00:02:40.002 --> 00:02:42.005
over integers array by typing
47
00:02:42.005 --> 00:02:48.000
mod_integers=mod_10(integers).
48
00:02:48.000 --> 00:02:52.000
Now we just need to print our mod array.
49
00:02:52.000 --> 00:02:53.005
After we run our code,
50
00:02:53.005 --> 00:02:55.007
we see we have successfully created
51
00:02:55.007 --> 00:02:57.007
and used our universal function.