0:02 in this video we will be covering numpy
0:08 in 1d in particular nd arrays numpy is a
0:10 library for scientific computing it has
0:13 many useful functions there are many
0:15 other advantages like speed and memory
0:18 numpy is also the basis for pandas so
0:22 check out our pandas video in this video
0:24 we will be covering the basics and array
0:28 creation indexing and slicing basic
0:32 operations universal functions let's go
0:34 over how to create an umpire ray a
0:37 Python list is a container that allows
0:39 you to store and access data each
0:43 element is associated with an index we
0:45 can access each element using a square
0:49 bracket as follows a numpy array or ND
0:52 array is similar to a list it's usually
0:55 fixed in size and each element is of the
0:58 same type in this case integers we can
1:00 cast a list to a numpy array by first
1:04 importing numpy we then cast the list as
1:07 follows we can access the data via an
1:10 index as with the lists we can access
1:12 each element with an integer and a
1:15 square bracket the value of a is stored
1:18 as follows if we check the type of the
1:23 array we get num PI dot nd array as
1:26 numpy arrays contain data of the same
1:29 type we can use the attribute de type to
1:31 obtain the datatype of the arrays
1:35 elements in this case a 64-bit integer
1:38 let's review some basic array attributes
1:41 using the array a the attribute size is
1:43 the number of elements in the array as
1:46 there are five elements the result is 5
1:49 the next two attributes will make more
1:51 sense when we get to higher dimensions
1:54 but let's review them the attribute n
1:57 dim represents the number of array
1:59 dimensions or the rank of the array in
2:03 this case 1 the attribute shape is a
2:05 tuple of integers indicating the size of
2:08 the array in each dimension we can
2:10 create a numpy array with real numbers
2:13 when we check the type of the array we get
2:13 get
2:17 und array if we examine the attribute D
2:21 type we see float64 as the elements are
2:23 not integers there are many other attributes
2:24 attributes
2:28 check out numpy org let's review some
2:31 indexing and slicing methods we can
2:33 change the first element of the array to
2:36 a hundred as follows the arrays first
2:39 value is now a hundred we can change the
2:40 fifth element of the array as follows
2:45 the fifth element is now zero like lists
2:47 and tuples we can slice a numpy array
2:49 the elements of the array correspond to
2:52 the following index we can select the
2:55 elements from 1 to 3 and assign it to a
2:58 new numpy array d as follows the
3:00 elements in D correspond to the index
3:03 like lists we do not count the element
3:06 corresponding to the last index we can
3:09 assign the corresponding indices to new
3:12 values as follows the array C now has
3:16 new values see the labs or numpad org
3:17 for more examples of what you can do
3:21 with numpy numpy makes it easier to do
3:23 many operations that are commonly
3:25 performed in data science these same
3:27 operations are usually computationally
3:30 faster and require less memory in numpy
3:33 compared to regular Python let's review
3:34 some of these operations on
3:37 one-dimensional arrays we will look at
3:39 many of the operations in the context of
3:42 Euclidean vectors to make things more
3:45 interesting vector addition is a widely
3:47 used operation in data science consider
3:49 the vector U with two elements the
3:52 elements are distinguished by the
3:55 different colors similarly consider the
3:58 vector V with two components in vector
4:00 addition we create a new vector in this
4:03 case zette the first component of Zed is
4:05 the addition of the first component of
4:09 vectors U and V similarly the second
4:11 component is the sum of the second
4:14 components of U and V this new vector
4:16 Zed is now a linear combination of the
4:20 vector U and V representing vector
4:22 addition with line segments or arrows is
4:24 helpful the first vector is represented
4:26 in red the vector will point in the direction
4:27 direction
4:29 of the two components the first
4:31 component of the vector is one as a
4:34 result the arrow is offset one unit from
4:36 the origin in the horizontal direction
4:39 the second component is zero we
4:41 represent this component in the vertical
4:44 direction as this component is 0 the
4:46 vector does not point in the horizontal
4:49 direction we represent the second vector
4:52 in blue the first component is zero
4:54 therefore the error does not point to
4:57 the horizontal direction the second
5:00 component is 1 as a result the vector
5:02 points in the vertical direction 1 unit
5:05 when we add the vector U and V we get
5:08 the new vector Z we add the first
5:10 component this corresponds to the
5:13 horizontal direction we also add the
5:16 second component it's helpful to use the
5:18 tip to tail method when adding vectors
5:21 placing the tail of a vector V on the
5:24 tip of vector u the new vector Z is
5:26 constructed by connecting the base of
5:28 the first vector U with the tail of the
5:31 second V the following three lines of
5:33 code will add the two lists and place
5:36 the result in the list Zed we can also
5:39 perform vector addition with one line of
5:42 numpy code it would require multiple
5:44 lines to perform vector subtraction on
5:46 two lists as shown on the right side of
5:49 the screen in addition the numpy code
5:51 will run much faster this is important
5:54 if you have lots of data we can also
5:56 perform vector subtraction by changing
5:58 the addition sign to a subtraction sign
6:00 it would require multiple lines to
6:02 perform vector subtraction on two lists
6:05 as shown on the right side of the screen
6:08 vector multiplication with a scalar is
6:10 another commonly performed operation
6:13 consider the vector Y each component is
6:15 specified by different color we simply
6:18 multiply the vector by a scalar value in
6:20 this case 2 each component of the vector
6:23 is multiplied by 2 in this case each
6:26 component is doubled we can use the line
6:29 segments or arrows to visualize what's
6:31 going on the original vector Y is in
6:34 purple after multiplying it by a scalar
6:37 value of to the vector is stretched out
6:39 by two units as shown in red
6:41 the new vector is twice as long in each
6:44 direction vector multiplication with a
6:46 scalar only requires one line of code
6:50 using them pi it would require multiple
6:52 lines to perform the same task as shown
6:54 with Python lists as shown on the right
6:57 side of the screen in addition the
7:00 operation would also be much slower
7:03 Hadamard product is another widely used
7:06 operation in data science consider the
7:09 following two vectors U and V the
7:11 Hadamard product of u and v is a new
7:14 vector z the first component of z is the
7:16 product of the first element of U and V
7:19 similarly the second component is the
7:21 product of the second element of U and V
7:24 the resultant vector consists of the
7:27 entry wise product of U and V we can
7:30 also perform Hadamard product with one
7:32 line of code in numpy it would require
7:34 multiple lines to perform how to mark
7:36 product on two lists as shown on the
7:39 right side of the screen the dot product
7:42 is another widely used operation in data
7:45 science consider the vector U and V the
7:48 dot product is a single number given by
7:50 the following term and represents how
7:53 similar two vectors are we multiply the
7:56 first component from V and U we then
7:57 multiply the second component and add
8:00 the result together the result is a
8:02 number that represents how similar the
8:05 two vectors are we can also perform dot
8:08 product using the numpy function dot and
8:10 assign it with the variable result as
8:13 follows consider the array u the array
8:16 contains the following elements if we
8:19 add a scalar value to the array numpy
8:21 will add that value to each element this
8:24 property is known as broadcasting a
8:26 universal function is a function that
8:30 operates on nd arrays we can apply a
8:33 universal function to a numpy array
8:36 consider the arrays a we can calculate
8:38 the mean or average value of all the
8:42 elements in a using the method mean this
8:44 corresponds to the average of all the
8:47 elements in this case the result is zero
8:49 there are many other functions for
8:52 example consider the numpy arrays
8:55 we can find the maximum value using the
8:58 method five we see the largest value is
9:01 five therefore the method max returns a
9:04 five we can use numpy to create
9:06 functions that map numpy raised to new
9:09 numpy arrays let's implement some code
9:11 on the left side of the screen and use
9:13 the right side of the screen to
9:15 demonstrate what's going on we can
9:17 access the value of pi in numpy as
9:20 follows we can create the following
9:23 numpy array in radians this array
9:26 corresponds to the following vector we
9:28 can apply the function sine to the array
9:31 X and assign the values to the array Y
9:34 this applies the sine function to each
9:37 element in the array this corresponds to
9:38 applying the sine function to each
9:42 component of the vector the result is a
9:45 new array Y where each value corresponds
9:47 to a sine function being applied to each
9:51 element in the array X a useful function
9:52 for plotting mathematical functions is
9:56 line space line space returns evenly
9:58 spaced numbers over specified interval
10:01 we specify the starting point of the
10:03 sequence the ending point of the
10:06 sequence the parameter num indicates the
10:09 number of samples to generate in this
10:12 case 5 the space between samples is 1 if
10:16 we change the parameter num to 9 we get
10:19 9 evenly spaced numbers over the
10:22 interval from negative 2 to 2 the result
10:24 is the difference between subsequent
10:29 samples is 0.5 as opposed to 1 as before
10:32 we can use the function line space to
10:35 generate 100 evenly spaced samples from
10:38 the interval 0 to 2 pi we can use the
10:41 numpy function sign to map the array X
10:45 to a new array Y we can import the
10:48 library PI plot as P LT to help us plot
10:51 the function as we are using a Jupiter
10:54 notebook we use the command mat plot Lib
10:57 in line to display the plot the
10:59 following command plots a graph the
11:01 first input corresponds to the values
11:05 for the horizontal or x-axis the second input
11:06 input
11:08 responds to the values for the vertical
11:11 or y-axis there's a lot more you can do
11:14 with numpy check out the labs numpy org