0:00

In this video, we will be covering Numpy in 1D, in particular, ndarrays.

Â Numpy is a library for scientific computing.

Â It has many useful functions.

Â There are many other advantages like speed and memory.

Â Numpy is also the basis for pandas,

Â so check out our pandas video.

Â In this video, we will be covering the basics and array creation,

Â indexing and slicing, basic operations, universal functions.

Â Let's go over how to create a numpy array.

Â A Python list is a container that allows you to store and access data.

Â Each element is associated with an index,

Â we can access each element using a square bracket as follows.

Â A numpy array or ndarray is similar to a list.

Â It's usually fixed in size,

Â and each element is of the same type.

Â In this case, integers.

Â We can cast a list to an numpy array by first importing numpy.

Â We then cast the list as follows.

Â We can access the data via an index.

Â As with the lists,

Â we can access each element with an integer and a square bracket.

Â The value of "a" is stored as follows.

Â If we check the type of the array we get, numpy.ndarray.

Â As numpy arrays contain data of the same type,

Â we can use the attribute dtype to obtain the data type of the array's elements.

Â In this case, a 64-bit integer.

Â Let's review some basic array attributes using the array "a."

Â The attribute size is the number of elements in the array,

Â as there are five elements the result is five.

Â The next two attributes will make more sense when we get

Â to higher dimensions, but let's review them.

Â The attribute ndim represents the number of

Â array dimensions or the rank of the array. In this case, one.

Â The attribute shape is a tuple of

Â integers indicating the size of the array in each dimension.

Â We can create a numpy array with real numbers.

Â When we check the type of the array, we get numpy.ndarray.

Â If we examine the attribute dtype,

Â we see "float64" as the elements are not integers.

Â There are many other attributes.

Â Check out numpy.org.

Â Let's review some indexing and slicing methods.

Â We can change the first element of the array to a 100 as follows.

Â The arrays first value is now a 100.

Â We can change the fifth element of the array as follows.

Â The fifth element is now zero.

Â Like lists and tuples,

Â we can slice a numpy array,

Â the elements of the array correspond to the following index.

Â We can select the elements from one to three,

Â and assign it to a new numpy array "d" as follows.

Â The elements in "d" correspond to the index.

Â Like lists, we do not count the element corresponding to the last index.

Â We can assign the corresponding indices to new values as follows.

Â The array "c" now has new values,

Â see the Labs or numpy.org for more examples of what you can do with numpy.

Â Numpy makes it easier to do many operations that are commonly performed in data science.

Â These same operations are usually computationally faster and

Â require less memory in numpy compared to regular python.

Â Let's review some of these operations on one dimensional arrays.

Â We will look at many of the operations in the context of

Â Euclidean vectors to make things more interesting.

Â Vector addition is a widely used operation in data science.

Â Consider the vector "u" with two elements,

Â the elements are distinguished by the different colors.

Â Similarly, consider the vector "v" with two components.

Â In vector addition, we create a new vector. In this case, "z."

Â The first component of the "z" is the addition of the first component

Â of vectors "u" and "v." Similarly,

Â the second component is the sum of the second components of "u" and "v." This new vector,

Â "z," is now a linear combination of the vector "u" and

Â "v." Representing vector addition with line segment or arrows is helpful.

Â The first vector is represented in red,

Â the vector will point in the direction of the two components.

Â The first component of the vector is one.

Â As a result, the arrow is offset one unit from the origin in the horizontal direction.

Â The second component is zero.

Â We represent this component in the vertical direction.

Â As this component is zero,

Â the vector does not point in the horizontal direction.

Â We represent the second vector in blue.

Â The first component is zero.

Â Therefore, the arrow does not point to the horizontal direction.

Â The second component is one,

Â as a result the vector points in the vertical direction one unit.

Â When we add the vector "u" and "v," we get the new vector "z."

Â We add the first component,

Â this corresponds to the horizontal direction.

Â We also add the second component.

Â It's helpful to use the tip to tail method when adding vectors,

Â placing the tail of a vector "v" on the tip of vector "u."

Â The new vector "z" is constructed by connecting the base of the first vector "u" with

Â the tail of the second "v." The following three lines of code will add the two lists,

Â and place the result in the list "z."

Â We can also perform vector addition with one line of numpy code.

Â It would require multiple lines to perform

Â vector subtraction on two list as shown on the right side of the screen.

Â In addition, the numpy code will run much faster.

Â This is important if you have lots of data.

Â We can also perform vector subtraction by

Â changing the addition sign to a subtraction sign.

Â It would require multiple lines to perform

Â vector subtraction on two lists as shown on the right side of the screen.

Â Vector multiplication with a scalar is another commonly performed operation.

Â Consider the vector "y."

Â Each component is specified by different color.

Â We simply multiply the vector by a scalar value. In this case, two.

Â Each component of the vector is multiplied by two.

Â In this case, each component is doubled.

Â We can use the line segment or arrows to visualize what's going on.

Â The original vector "y" is in purple after multiplying it by a scalar value of two.

Â The vector is stretched out by two units as shown in red.

Â The new vector's twice as long in each direction.

Â Vector multiplication with a scalar only requires one line of code using numpy.

Â It would require multiple lines to perform the same task as

Â shown with Python lists as shown on the right side of the screen.

Â In addition, the operation would also be much slower.

Â Hadamard product is another widely used operation and data science.

Â Consider the following two vectors "u" and "v",

Â the Hadamard product of "u" and "v" is a new vector "z."

Â The first component of "z" is the product of the first element of "u" and "v." Similarly,

Â the second component is the product of the second element of

Â "u" and "v." The resultant vector consists of

Â the entrywise product of "u" and "v." We can

Â also perform Hadamard product with one line of code in numpy.

Â It would require multiple lines to perform

Â Hadamard product on two list as shown on the right side of the screen.

Â The dot product is another widely used operation in data science.

Â Consider the vector "u" and "v."

Â The dot product is a single number given by the following term,

Â and represents how similar two vectors are.

Â We multipliy the first component from "v" and "u."

Â We then multiply the second component,

Â and add the result together.

Â The result is a number that represents how similar the two vectors are.

Â We can also perform dot product using the numpy function dot,

Â and assign it with the variable result as follows.

Â Consider the array "u."

Â The array contains the following elements.

Â If we add a scalar value to the array,

Â numpy will add that value to each element,

Â this property is known as broadcasting.

Â A universal function is a function that operates on ndarrays.

Â We can apply a universal function to a numpy array.

Â Consider the arrays "a."

Â We can calculate the mean or average value of

Â all the elements in "a" using the method mean.

Â This corresponds the average of all the elements.

Â In this case, the result is zero.

Â There are many other functions.

Â For example, consider the numpy arrays "b."

Â We can find the maximum value using the method five.

Â We see the largest value is five.

Â Therefore, the method max returns of five.

Â We can use numpy to create functions that map numpy arrays to new numpy arrays.

Â Let's implement some code on the left side of the screen

Â and use the right side of the screen to demonstrate what's going on.

Â We can access the value of pi in numpy as follows.

Â We can create the following numpy array in radians.

Â This array corresponds to the following vector.

Â We can apply the function sin to the array "x" and assign the values to the array "y."

Â This applies the sine function to each element in the array.

Â This corresponds to applying the sine function to each component of the vector.

Â The result is a new array "y," where each value

Â corresponds to a sine function being applied to each element in the array "x."

Â A useful function for plotting mathematical functions is line space.

Â Line space returns evenly spaced numbers over a specified interval.

Â We specify the starting point of the sequence.

Â The ending point of the sequence.

Â The parameter "num" indicates the number of samples to generate.

Â In this case, five.

Â The space between samples is one.

Â If we change the parameter "num" to nine,

Â we get nine evenly spaced numbers over the interval from negative two to two.

Â The result is the difference between subsequent samples is

Â 0.5 as opposed to one as before.

Â We can use the function line space to generate 100

Â evenly spaced samples from the interval zero to two pi.

Â We can use the numpy function sine to map the array "x" to a new array "y."

Â We can import the library pyplot as plt to help us plot function.

Â As we're using a Jupiter notebook,

Â we used the command matplotlib inline to display the plot.

Â The following command plots a graph.

Â The first input corresponds to the values for the horizontal or "x" axis.

Â The second input corresponds to the values for the vertical or "y" axis.

Â There is a lot more you can do with numpy.

Â Check out the Labs at numpy.org for more.

Â Thanks for watching this video.

Â