Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Slicing11:50 with Craig Dennis
Slicing an ndarray is similar to slicing a list. In NumPy, however slicing creates a view instead of a copy.
My Notes for Boolean Array Indexing
## Boolean Array Indexing * You can create a boolean array by using comparison operators on an array. * You can use boolean arrays for fancy indexing. * Boolean arrays can be compared by using bitwise operators (`&`, `|`) * Do not use the `and` keyword. * Remember to mind the order of operations when combining * Even though boolean indexing returns a new array, you can update an existing array using a boolean index.
You know how when you don't use a skill for 0:00 a while you need to practice it a bit before you can get right back to doing it 0:02 flawlessly, like no matter how good you ever were at it. 0:06 It's where that saying use it or lose it comes from. 0:09 Well for me, in addition to juggling, 0:11 that skill I need to dust off is slicing an object. 0:13 You've probably used slices before on lists or tuples, but 0:17 if you haven't done it for a while, you could be a little [SOUND] klutzy. 0:20 This is where practice comes in. 0:24 And I want to encourage you to practice and test things out. 0:26 Let's practice a bit of slicing with lists first just to warm up. 0:29 And then let's practice with some multi-dimensional arrays. 0:33 It's like starting with these practice juggling balls instead of, say, chainsaws. 0:35 Jupiter notebooks are excellent for this sort of practice but actually real quick. 0:42 Before we get into our slice practicing, 0:46 let's make use of another great feature of notebooks, notes. 0:48 So here are mine on boolean array indexing. 0:53 So you can create a boolean array by using a comparison operator on an array. 0:56 And you can use boolean arrays for fancy indexing like we saw, 1:01 kinda like a where clause there from SQL. 1:04 And boolean arrays can be compared using bitwise operators, that's and, 1:07 and then the pipe sign there is or. 1:11 And remember, don't use the and keyword, and 1:13 also remember to use the order of operations. 1:15 Otherwise, you get that really weird value error that we saw. 1:18 And even though boolean indexing returns a brand new array, 1:21 like a copy, you can update an existing array by using a boolean index. 1:26 So let's go down to the bottom here, the very last row, and 1:32 if you want to clean some of these up you can. 1:35 I'm going to get rid of some these, just DDD in command mode there. 1:37 All right, so let's make a new list. 1:42 My go to list is this fruit. 1:45 We'll go ahead, we'll get apple, banana, cherry, and durian. 1:48 All right, now let's warm up those slicing skills. 1:58 Now, for some reason, I can never remember if slices are inclusive or exclusive. 2:02 So when I am not sure, one thing I like to do is just try. 2:07 You're not going to break anything, right? 2:10 All right, so let's get a slice of this list. 2:12 I want to get a portion of this list, like just the second and third value. 2:15 So let's see, I know that things are 0 based. 2:21 So I know that I want to get the second one, I'm going to start at 1. 2:25 And then I want a colon signifying up to. 2:30 And now, is it inclusive or exclusive? 2:33 I don't know, I'll try inclusive. 2:35 So, let's see 1 to 2. 2:38 No, it is exclusive, [SOUND] I missed that by much. 2:40 So if we come back and we say 1 to 3, we'll see that we get banana and cherry. 2:43 So it's like up to but not including. 2:50 That's what you can read with this is like 1 up to but not including 3, phew. 2:53 All right, and then again if you leave either side blank, so if we say fruit and 2:58 we go up to 3. 3:03 We'll get everything up to the third one. 3:04 Which, again, not the 3rd one, the 4th one, this is 0 based. 3:08 And then if you do it at the start, you can say, fruit from 3 onwards, 3:13 so we'll start at the 3rd and go to the end. 3:18 And there's durian, mm, the cheese of fruit. 3:21 So just a plain colon then, 3:24 if you just use a colon it will give you a copy of the array. 3:27 It's basically a copy right? 3:32 From start to end. 3:33 In fact this is typically used as a way of copying standard Python lists. 3:34 So if I store that in a variable, so we'll say copied = fruit, and 3:38 then the colon, so give me everything. 3:43 And then we go ahead and what if we modify that copy? 3:48 If we say copied, that last, that durian, we're going to set that to cheese. 3:52 If we set that to cheese, and then we take a look at, let's see, slicing. 3:59 A list returns a copy, and I'm gonna spell slicing correctly. 4:07 Slicing a list returns a copy. 4:15 So if we look here, if we look at fruit and the copied. 4:16 We're just gonna make a new tuple of those two so 4:19 we can look at the values next to each other. 4:22 We'll see that fruit was not changed when we changed the copy of that. 4:24 There are actually two different places in memory. 4:29 Okay, and there is a third part to this slice, the step. 4:34 Now, by default, this is 1, it moves 1 element at a time. 4:37 But I can add a second colon, representing a step. 4:41 Let's say that I wanted to get every other one. 4:47 So we'll say fruit, There we go, apple, cherry, and we skipped banana and durian. 4:48 So instead of stepping through each element one by one, we went by two, and 4:57 you can also use a negative to walk backwards, right? 5:00 So if we say fruit [::-1], we'll see that it runs backwards. 5:03 And there we go, I think I'm refreshed, 5:10 I think I'm ready to start juggling those chainsaws. 5:13 My de facto test list go to is this one of fruit. 5:17 When it comes to creating a numpy array to explore the most common way is to use 5:23 a function named arange. 5:27 Which is very similar to Python's range function, but 5:29 instead it returns an ndarray. 5:32 So if I say np.arange and I pass it in the ending value there. 5:34 So it goes up to and not including 20. 5:41 You'll see that we get all the values up to and not including 20. 5:46 So let's go ahead and create a new practice right here. 5:50 And we will make it np.arange, and we'll go up to our magic number, 42. 5:54 And you can actually change the shape property on an array. 6:02 So we can say practice.shape, and we're going to assign it, 6:07 let's go 7 rows, 6 columns. 6:12 So let's do that, and then let's take a look at what practice looks like. 6:15 Awesome, so let's go ahead and get this number 13 here, lucky 13. 6:20 So this is a two-dimensional array, or matrix. 6:26 And really, it's just an array of arrays. 6:30 So we first need to get this row here, so this is 0, 1, 2. 6:32 So we have practice. 6:38 Okay, let's make sure we got it. 6:44 Yep, and that's just an array. 6:46 So we need to get the 0, 1, we need to get the 1th there [LAUGH]. 6:47 There we go, and there's 13 and that is entirely too many hard brackets, 6:54 so let's express it with just a comma, so 2, 1. 6:59 Awesome, so as you can expect, the ndarray is Pythonic, so it too allows for slicing. 7:02 So if we wanted to start at the 3rd row here, and go to the 5th, 7:07 we could just do this. 7:11 We could say practice[2:5]. 7:12 And there we go, we've got just those rows, awesome. 7:17 And if we wanted to just get this column here we could just put comma 3. 7:22 Awesome, and we can also slice this column dimension. 7:32 So let's get the 4th column until the end. 7:37 So we'll say, 3 until the end, and there we go. 7:40 Now we have 15, 16, 17 and then if we wanted to step 7:43 every other column we could say 3::2. 7:47 There we just have those two. 7:52 Look at that, we stepped right over that column, right? 7:53 So we skipped right over this column, we limited it. 7:57 And then we sliced it and we got these three and then we get skipped over that. 7:59 So we got just this 15 to 17, 21, 23, and 29, [LAUGH] pretty great, right? 8:03 Now one thing that'll bite you 8:09 if you don't know about it is that slices in NumPy don't return a copy. 8:12 They return a view of our data. 8:17 Now this is different than we saw in the standard Python list. 8:19 So let's explores this real quick. 8:22 I'm gonna write not_copied, and we're just gonna get everything. 8:26 We're gonna take our practice array, and I'm gonna make a comment here for 8:30 us later as we look over this. 8:34 Any slicing of ndarray returns a view and not a copy. 8:36 Okay, so I'm gonna go ahead, and I'm gonna set, not copy it. 8:45 I'll set 0, 0, the first one there, 90201, 8:50 and we will return practice, not_copied. 8:56 Just so we can see them next to each other. 9:02 And we'll see that both of them changed, and 9:04 that is because this is a view and not a copy, right? 9:08 It's exactly the same. 9:15 It changed the original array practice, and 9:17 that's because not copied is actually what is known as a data view. 9:20 And as you can tell, 9:24 it's kind of hard to know just by looking at the representation of the array. 9:25 But they're views are not brand new arrays. 9:30 So one way that you can check to see if you have a view 9:32 is to check the base property. 9:36 So if we look at this we can say practice.base is None. 9:38 And that's true, but if we look at not_copied.base 9:46 is None, we'll see that that's false because it is copied. 9:51 And then also, that base is set, if you say not_copied.base 9:54 is practice, we will see that that's true. 10:01 So you can always see where it was copied from. 10:06 And there's also a property called flags on array and it's a dictionary and 10:09 one of the keys is called Own Data. 10:14 So you check that as well if you wanted to. 10:16 You can say practice.flags['OWNDATA']? 10:18 And that one should be true and not_copied does not own the data. 10:23 It is a view. 10:29 So data views are important to understand and be aware of 10:31 as you don't want to accidentally modify a structure that you didn't intend to. 10:34 Data views are part of the trick of how quick and 10:39 seamlessly you can arrange data in NumPy. 10:41 So you will see them used in the wild quite a bit. 10:44 Now the one major and not initially intuitive place where data views get 10:47 created is in slicing, like we just saw. 10:52 So I'd like to make sure that you recall that when you slice an array of any shape, 10:55 that you are creating a view and not a new array, 11:00 or copy, as happens with standard Python lists. 11:03 A view references the same values in memory 11:08 while a copy would be a brand new space. 11:10 Using a slice or view is handy as you can pass only part of the array around for 11:14 processing but not require the entire array. 11:20 By not creating a new array we are not only saving memory but 11:23 we're also allowing a reshaping of the array. 11:26 We're allowing portions or slices of the array to be modified. 11:30 Let's take a look at some more data view creating functions. 11:34 And actually, I'm gonna take some notes on slicing and 11:37 slicing multiple-dimensional arrays specifically. 11:40 I'm also gonna make sure to document that little data view gotcha. 11:43 Let's review that right after this quick break. 11:47
You need to sign up for Treehouse in order to download course files.Sign up