# 11-785 Spring 2023 Recitation 0B: Fundamentals of NumPy (Part 2/8)

We’re now going to be talking about accessing and modifying data in Numpi. So we’re first going to talk about indexing, which is essentially accessing values from Numpi arrays. So indexing in Numpi is almost the same as it is in Python lists. So we have an array here with some sample elements from a uniform distribution. This array comprises of four batches with 30 elements within each batch, and these 30 elements can further be divided into five rows and six columns. So since indexing in Numpi arrays starts from zero, if we want to index the third row, the fourth column of the second batch, we pass the following values in. The one here corresponds to the second batch, a zero corresponds to the first one, and the two in the three map to the third row and the fourth column, respectively. We’re now going to talk about slicing, which is essentially accessing subsections of Numpi arrays based on indices. If we want to slice along a batch and return all the elements belonging to a particular batch number, this is what we would do. So this is essentially the same as doing N as zero. Now, if we want to segregate elements belonging to the first three rows, the first four columns, and belonging to the first batch, these are the indices that we would pass in. We can also slice along multiple batches. So this index pattern returns elements in the fourth row, the fifth column, across all batches. Now, slicing can also be done at intervals, and the syntax we’re slicing at intervals is as follows, where we provide the start, the stop, and the step size, and these are all separated by columns. Now, if we want to print all the elements belonging to alternate batches, such that the step size is two, this is what we would pass in. Now, if we want elements belonging to alternate batches from rows two to five, and a column starting from one with a step size of two, these are the indices that we would pass in. And we can also modify Numpi arrays. So to copy a Numpi array, we use the function NP.copy, and specify the array which we want to copy as an input parameter. To modify single values, we can pass the index at which we want to modify the value, and this is where the value changes. Now, this is a simple check operation to see whether the value at N copy is the same as the value at N. So since we’ve changed it to 0.5 here, the value of this is going to be false, which indicates that the value has been updated in the N-copy array correctly. So we now move on to modifying multiple values in Numpi arrays. So here we’re going to be looking at a small section of the data present in N-copy. So we know that these two sections are the same, and so our output is going to be all true. And before modifying the values, we just quickly make this check. And once we modify the values within the subsection to be all 5, we see that once we run this check, we are going to get an output of false, which indicates that our values have been updated correctly in N-copy.

52
01:01

16
02:18

45
02:59

64
00:57

1
01:22:34

58
00:53