In this section you can learn about Numpy, Numpy Arrays, Indexing and Slicing in Numpy, Stacking, Multidimensional Array and many more.
Welcome to your first notebook of this notes. As mentioned in the lecture videos, you will use Python for the labs and programming assignments in this specializiation. In this course, most of your work will be inside a library called NumPy. NumPy (Numerical Python) is an open-source package that is widely used in science and engineering. You can check out the official NumPy documentaion here. In this notebook, you will use NumPy to create 2-D arrays and easily compute mathematical operations. Feel free to skip this notebook if you are already fluent with NumPy.
After this assignment you will be able to:
Use Jupyter Notebook and its features.
Use NumPy functions to create arrays and NumPy array operations.
Use indexing and slicing for 2-D arrays.
Find the shape of an array, reshape it and stack it horizontally and vertically.
Jupyter Notebooks are interactive coding journals that integrate live code, explanatory text, equations, visualizations and other multimedia resources, all in a single document. As a first exercise, run the test snippet below and the print statement cell for "Hello World".
NumPy is the main package for scientific computing in Python. It performs a wide variety of advanced mathematical operations with high efficiency. In this practice lab you will learn several key NumPy functions that will help you in future assignments, such as creating arrays, slicing, indexing, reshaping and stacking.
Before you get started, you have to import NumPy to load its functions. As you may notice, even though there is no expected output, when you run this cell, the Jupyter Notebook imports the package (often referred to as the library) and its functions. Try it for yourself and run the following cell.
Arrays are one of the core data structures of the NumPy library, essential for organizing your data. You can think of them as a grid of values, all of the same type. Arrays are analogous to the matrices you saw in the video lessons. If you have used Python lists before, you may remember that they are convenient, as you can store different data types. However, Python lists are limited in functions and take up more space and time to process than NumPy arrays.
NumPy provides an array object that is much faster and more compact than Python lists. Through its extensive API integration, the library offers many built-in functions that make computing much easier with only a few lines of code. This can be a huge advantage when performing math operations on large datasets.
The array object in NumPy is called ndarray meaning 'n-dimensional array'. To begin with, you will use one of the most common array types: the one-dimensional array ('1-D'). A 1-D array represents a standard list of values entirely in one dimension. Remember that in NumPy, all of the elements within the array are of the same type.
There are several ways to create an array in NumPy. You can create a 1-D array by simply using the function array() which takes in a list of values as an argument and returns a 1-D array.
# Create and print a NumPy array 'a' containing the elements 1, 2, 3.a = np.array([1, 2, 3])print(a)
Another way to implement an array is using np.arange(). This function will return an array of evenly spaced values within a given interval. To learn more about the arguments that this function takes, there is a powerful feature in Jupyter Notebook that allows you to access the documentation of any function by simply pressing shift+tab on your keyboard when clicking on the function. Give it a try for the built-in documentation of np.arange().
# Create an array with 3 integers, starting from the default integer 0.b = np.arange(3)print(b)
# Create an array that starts from the integer 1, ends at 20, incremented by 3.c = np.arange(1, 20, 3)print(c)
What if you wanted to create an array with five evenly spaced values in the interval from 0 to 100? As you may notice, you have 3 parameters that a function must take. One paremeter is the starting number, in this case 0, the final number 100 and the number of elements in the array, in this case, 5. NumPy has a function that allows you to do specifically this by using np.linspace().
Did you notice that the output of the function is presented in the float value form (e.g. "... 25. 50. ...")? The reason is that the default type for values in the NumPy function np.linspace is a floating point (np.float64). You can easily specify your data type using dtype. If you access the built-in documentation of the functions, you may notice that most functions take in an optional parameter dtype. In addition to float, NumPy has several other data types such as int, and char.
To change the type to integers, you need to set the dtype to int. You can do so, even in the previous functions. Feel free to try it out and modify the cells to output your desired data type.
char_arr = np.array(['Welcome to Math for ML!'])print(char_arr)print(char_arr.dtype) # Prints the data type of the array
Did you notice that the output of the data type of the char_arr array is <U23?
This means that the string ('Welcome to Math for ML!') is a 23-character (23) unicode string (U) on a little-endian architecture (<). You can learn more about data types here.
With NumPy you can also create arrays with more than one dimension. In the above examples, you dealt with 1-D arrays, where you can access their elements using a single index. A multidimensional array has more than one column. Think of a multidimensional array as an excel sheet where each row/column represents a dimension.
# Create a 2 dimensional array (2-D)two_dim_arr = np.array([[1,2,3], [4,5,6]])print(two_dim_arr)
An alternative way to create a multidimensional array is by reshaping the initial 1-D array. Using np.reshape() you can rearrange elements of the previous array into a new shape.
# 1-D array one_dim_arr = np.array([1, 2, 3, 4, 5, 6])# Multidimensional array using reshape()multi_dim_arr = np.reshape( one_dim_arr, # the array to be reshaped (2,3) # dimensions of the new array )# Print the new 2-D array with two rows and three columnsprint(multi_dim_arr)
In future assignments, you will need to know how to find the size, dimension and shape of an array. These are all atrributes of a ndarray and can be accessed as follows:
ndarray.ndim - Stores the number dimensions of the array.
ndarray.shape - Stores the shape of the array. Each number in the tuple denotes the lengths of each corresponding dimension.
ndarray.size - Stores the number of elements in the array.
# Dimension of the 2-D array multi_dim_arrmulti_dim_arr.ndim
# Shape of the 2-D array multi_dim_arr# Returns shape of 2 rows and 3 columnsmulti_dim_arr.shape
# Size of the array multi_dim_arr# Returns total number of elementsmulti_dim_arr.size
In this section, you will see that NumPy allows you to quickly perform elementwise addition, substraction, multiplication and division for both 1-D and multidimensional arrays. The operations are performed using the math symbol for each '+', '-' and '*'. Recall that addition of Python lists works completely differently as it would append the lists, thus making a longer list. Meanwhile, trying to subtract or multipy Python lists simply would cause an error.
Suppose you need to convert miles to kilometers. To do so, you can use the NumPy array functions that you've learned so far. You can do this by carrying out an operation between an array (miles) and a single number (the conversion rate which is a scalar). Since, 1 mile = 1.6 km, NumPy computes each multiplication within each cell.
This concept is called broadcasting, which allows you to perform operations specifically on arrays of different shapes.
Indexing is very useful as it allows you to select specific elements from an array. It also lets you select entire rows/columns or planes as you'll see in future assignments for multidimensional arrays.
Let us select specific elements from the arrays as given.
# Select the third element of the array. Remember the counting starts from 0.a = np.array([1, 2, 3, 4, 5])print(a[2])# Select the first element of the array.print(a[0])
For multidimensional arrays of shape n, to index a specific element, you must input n indices, one for each dimension. There are two common ways to do this, either by using two sets of brackets, or by using a single bracket and separating each index by a comma. Both methods are shown here.
# Indexing on a 2-D arraytwo_dim = np.array(([1, 2, 3], [4, 5, 6], [7, 8, 9]))# Select element number 8 from the 2-D array using indices i, j and two sets of bracketsprint(two_dim[2][1])# Select element number 8 from the 2-D array, this time using i and j indexes in a single # set of brackets, separated by a commaprint(two_dim[2,1])
Slicing gives you a sublist of elements that you specify from the array. The slice notation specifies a start and end value, and copies the list from start up to but not including the end (end-exclusive).
The syntax is:
array[start:end:step]
If no value is passed to start, it is assumed start = 0, if no value is passed to end, it is assumed that end = length of array - 1 and if no value is passed to step, it is assumed step = 1.
Note you can use slice notation with multi-dimensional indexing, as in a[0:2, :5]. This is the extent of indexing you'll need for this course but feel free to check out the official NumPy documentation for extensive documentation on more advanced NumPy array indexing techniques.
# Slice the array a to get the array [2,3,4]sliced_arr = a[1:4]print(sliced_arr)
# Slice the array a to get the array [1,2,3]sliced_arr = a[:3]print(sliced_arr)
# Slice the array a to get the array [3,4,5]sliced_arr = a[2:]print(sliced_arr)
# Slice the array a to get the array [1,3,5]sliced_arr = a[::2]print(sliced_arr)
# Note that a == a[:] == a[::]#print(a == a[:] == a[::])
# Slice the two_dim array to get the first two rowssliced_arr_1 = two_dim[0:2]sliced_arr_1
# Similarily, slice the two_dim array to get the last two rowssliced_two_dim_rows = two_dim[1:3]print(sliced_two_dim_rows)
# This example uses slice notation to get every row, and then pulls the second column.# Notice how this example combines slice notation with the use of multiple indexessliced_two_dim_cols = two_dim[:,1]print(sliced_two_dim_cols)
Finally, stacking is a feature of NumPy that leads to increased customization of arrays. It means to join two or more arrays, either horizontally or vertically, meaning that it is done along a new axis.
np.vstack() - stacks vertically
np.hstack() - stacks horizontally
np.hsplit() - splits an array into several smaller arrays
# Stack the arrays verticallyvert_stack = np.vstack((a1, a2))print(vert_stack)
# Stack the arrays horizontallyhorz_stack = np.hstack((a1, a2))print(horz_stack)
# Split the horizontally stacked array into 2 separate arrays of equal sizehorz_split_two = np.hsplit(horz_stack,2)print(horz_split_two)# Split the horizontally stacked array into 4 separate arrays of equal sizehorz_split_four = np.hsplit(horz_stack,4)print(horz_split_four)# Split the horizontally stacked array after the first columnhorz_split_first = np.hsplit(horz_stack,[1])print(horz_split_first)
# Split the vertically stacked array into 2 separate arrays of equal sizevert_split_two = np.vsplit(vert_stack,2)print(vert_split_two)# Split the horizontally stacked array into 4 separate arrays of equal sizevert_split_four = np.vsplit(vert_stack,4)print(vert_split_four)# Split the horizontally stacked array after the first and third rowvert_split_first_third = np.vsplit(vert_stack,[1,3])print(vert_split_first_third)