How to use Numpy Arrays in Python

Learn to create and manipulate 2D NumPy arrays in Python. The presenter covers the .shape attribute, NumPy's requirement that arrays be homogeneous, and two indexing methods: nested brackets and comma syntax. You'll also see how element-wise arithmetic works on 2D arrays.
Chapters
Lets recreate the numpy arrays from the previous video. If you ask for the type of these arrays, Python tells you that you are numpy. MDarray. Numpy tells you it's a type that was defined in the NumPy package. MDArray stands for N dimensional array. The array's NP height and NPweight are one dimensional arrays, but it's perfectly possible to create two dimensional, three dimensional, heck, even seven dimensional arrays. Let's stick to two in this video though, you can create a 2D NumPy array from a regular Python list of lists. Let's Try to create one NumPy array for all height and weight data of your family. Like this. If you print out np2d now, you'll see that it is a rectangular data structure. Each sublist in the list corresponds to a row in the two dimensional NumPy array. From MP2D shape, you can see that we indeed have two rows and five columns. Shape is a so called attribute of the NP2D array that can give you more information about what the data structure looks like. Also, for 2D arrays, the NumPy rule applies. An array can only contain a single type. If you change one float to be string, all the array elements will be coerced to strings as well. To end up with a homogeneous array. You can think of the 2D NumPy array as an improved list of lists. You can perform calculations on the arrays like I showed before, and you can do more advanced ways of subsetting. Suppose you want the first row and then the third element in that row. To select a row, you need the index 0 inside square brackets to then select the third element. You can extend the same call with another pair of brackets, this time with the next two. Like this. Basically you are selecting the row and then from that row do another selection. There is also an alternative way of subsetting. Using single square brackets and a comma. This column returns the exact same value as before. The value before the comma specifies a row. The value after the comma specifies a column. The intersection of the rows and columns you specified are returned. Once you get used to it, this syntax is more intuitive and opens up more possibilities. Suppose you want to select the height and weight of the second and third family member. You want both rows, so you put in a column before the comma. You only want the second and third column, so you put in the indices 1 to 3 after the comma. Remember that the third index is not included here. The intersection gives us a 2D array with two rows and two columns. Similarly, you can select the weight of all family members like this. You only want the second row, so put one before the comma. You want all columns, so you use a colon after the comma. The intersection gives us the entire second row. Finally, 2D NumPy arrays enable you to do element wise calculations the same way you did it with 1D numpy arrays. That's something you can experiment with in the exercises, along with creating and subsetting 2D NumPy arrays. Exciting.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







