WEBVTT
1
00:00:00.005 --> 00:00:02.007
- [Instructor] Once we have learned a set of doors
2
00:00:02.007 --> 00:00:06.002
for creating and manipulating NumPy arrays,
3
00:00:06.002 --> 00:00:10.000
let's move on to learn arithmetic operations.
4
00:00:10.000 --> 00:00:12.006
NumPy has a powerful capacity
5
00:00:12.006 --> 00:00:15.008
to apply operations on the color array
6
00:00:15.008 --> 00:00:18.008
instead of each element individually.
7
00:00:18.008 --> 00:00:21.007
This is called vectorization.
8
00:00:21.007 --> 00:00:23.005
When used efficiently,
9
00:00:23.005 --> 00:00:26.004
vectorized expressions will eliminate the need
10
00:00:26.004 --> 00:00:29.002
for many explicit for-loops.
11
00:00:29.002 --> 00:00:31.000
The benefits are:
12
00:00:31.000 --> 00:00:34.008
Higher performing code, less verbose code
13
00:00:34.008 --> 00:00:37.001
and better maintainability.
14
00:00:37.001 --> 00:00:39.006
There are plenty of arithmetic operations
15
00:00:39.006 --> 00:00:41.003
that are supported.
16
00:00:41.003 --> 00:00:43.005
We'll cover some of them;
17
00:00:43.005 --> 00:00:50.000
addition, subtraction, multiplication, division,
18
00:00:50.000 --> 00:00:54.008
exponentiation, and specific functions to perform them
19
00:00:54.008 --> 00:00:58.000
as well as some other useful functions.
20
00:00:58.000 --> 00:01:01.000
Let's start exploring arithmetic operations
21
00:01:01.000 --> 00:01:04.003
by doing some simple calculations with scalars
22
00:01:04.003 --> 00:01:06.003
and between arrays.
23
00:01:06.003 --> 00:01:09.003
We have imported NumPy as np.
24
00:01:09.003 --> 00:01:13.002
Now let's create a two one dimensional arrays
25
00:01:13.002 --> 00:01:15.004
using an arrange function.
26
00:01:15.004 --> 00:01:17.008
We'll call them just a and b.
27
00:01:17.008 --> 00:01:22.001
A will contain integers from one to 10,
28
00:01:22.001 --> 00:01:27.008
and b will contain integers from 21 to 30.
29
00:01:27.008 --> 00:01:32.008
Let's add them using plus operator, just type a + b
30
00:01:32.008 --> 00:01:35.004
and we have two arrays a and b,
31
00:01:35.004 --> 00:01:37.009
which are added element by element.
32
00:01:37.009 --> 00:01:39.006
It's that simple.
33
00:01:39.006 --> 00:01:40.004
Great.
34
00:01:40.004 --> 00:01:42.005
Now let's see subtraction.
35
00:01:42.005 --> 00:01:47.002
We just need to type b - a, isn't that easy?
36
00:01:47.002 --> 00:01:51.001
Next, let's multiply by using asterisks operator
37
00:01:51.001 --> 00:01:54.005
by typing a asterisks b.
38
00:01:54.005 --> 00:01:55.004
Nice.
39
00:01:55.004 --> 00:01:59.009
And now let's see division by typing b/a.
40
00:01:59.009 --> 00:02:04.003
For exponentiation we'll use double asterisks operator
41
00:02:04.003 --> 00:02:08.002
by typing a double asterisks b.
42
00:02:08.002 --> 00:02:12.003
We can also do operations between array and a scalar.
43
00:02:12.003 --> 00:02:15.009
In this case, scalar value is applied to each element
44
00:02:15.009 --> 00:02:17.004
in the array.
45
00:02:17.004 --> 00:02:23.005
Let's use our array a and multiply it with two
46
00:02:23.005 --> 00:02:24.007
and that's it.
47
00:02:24.007 --> 00:02:28.008
All elements are now twice the previous value.
48
00:02:28.008 --> 00:02:30.007
NumPy by also has functions
49
00:02:30.007 --> 00:02:33.006
to perform arithmetic operations.
50
00:02:33.006 --> 00:02:36.000
You can easily remember the names
51
00:02:36.000 --> 00:02:38.007
because they are the same as in math,
52
00:02:38.007 --> 00:02:42.000
add, subtract, multiply, and divide.
53
00:02:42.000 --> 00:02:45.006
Let's try them out on arrays a and b.
54
00:02:45.006 --> 00:02:51.001
So type np.add a,b,
55
00:02:51.001 --> 00:02:57.009
and next np.substract b.a.
56
00:02:57.009 --> 00:03:06.000
To multiply a and b, will type np.multiply a,b
57
00:03:06.000 --> 00:03:11.004
and to divide np.divide b,a.
58
00:03:11.004 --> 00:03:13.006
Lastly, I want to introduce you
59
00:03:13.006 --> 00:03:16.008
to a few more useful arithmetic functions,
60
00:03:16.008 --> 00:03:19.009
mode, power, and square root.
61
00:03:19.009 --> 00:03:21.006
We will use the mode function
62
00:03:21.006 --> 00:03:25.005
to output the remainder of the division of two arrays.
63
00:03:25.005 --> 00:03:29.009
Let's see it in action by typing mode b,a,
64
00:03:29.009 --> 00:03:30.008
great.
65
00:03:30.008 --> 00:03:32.009
So we got our remainders.
66
00:03:32.009 --> 00:03:36.002
Power function is used for exponentiation.
67
00:03:36.002 --> 00:03:41.001
We have to type np.power a,b.
68
00:03:41.001 --> 00:03:44.007
Square root function is used to calculate the square root
69
00:03:44.007 --> 00:03:47.002
of all elements of the array.
70
00:03:47.002 --> 00:03:54.005
Let's use it on array a, by typing np.square root a,
71
00:03:54.005 --> 00:03:55.006
great.
72
00:03:55.006 --> 00:03:57.006
So we successfully mastered
73
00:03:57.006 --> 00:04:00.003
arithmetic operations and functions.