Manipulation10:35 with Craig Dennis
Let's look at some common array manipulation functions.
We were able to change the shape of our array, 0:00 by setting the shape property directly. 0:02 Now, this is handy when you want to permanently change the arrangement of your 0:04 data, but that's not always gonna be the case. 0:07 You'll find, as you work more and more with data sets, that you're going to want 0:11 to temporarily bend and mold the view of your data to work with. 0:15 But leave the original one intact. 0:18 This is where those data views, 0:20 like the ones that we got from slicing come in handy. 0:22 There are a ton of ways to manipulate your data. 0:25 Let's take a look at some of the more popular techniques, and 0:28 I'll show you how and where to learn more. 0:31 Ooh, first though, let's compare our notes on slicing. 0:34 So slicing works a lot like normal list slicing, it's very Pythonic. 0:38 And you can use commas to separate each dimension slice, right? 0:42 So you can slice the dimension, and it always returns a data view, remember that. 0:46 Not a copy, that's very different then it, but it's important to know. 0:51 And you can always access the base object using ndarray.base, awesome. 0:55 So remember we changed the shape earlier, we changed the shape, 1:01 where did we did that? 1:04 We just changed it right here. 1:05 Specifically once and we changed it permanently, 1:06 we changed the original practice array that way. 1:09 So you can actually create a view instead by using the method reshape. 1:11 Let's remind ourselves what a practice array looks like. 1:15 It's a 7 by 6, right, so all the way up to 42. 1:19 So let's assume that we wanted to have three rows and 1:23 that would be 14 columns, right? 1:27 So we will do that, we'll say practice_view 1:29 = practice.reshape(3, 14). 1:36 And just to make sure, we go get practice, and then we get practice_view. 1:42 We're just gonna pop these out here to take a look. 1:47 I figure this is a handy way to do it, all in one line. 1:49 And then we'll say practice_view .base is practice, okay? 1:51 So we're gonna make a view off of that practice array. 1:56 So here we go, 2:01 we now have a three dimensional array instead of a seven by six. 2:02 And you'll see that at the end here we've got True, so it is the base. 2:08 So the view isn't taking up anymore memory, 2:12 it's just pointing to the original data. 2:14 Which allows you to save space and 2:16 it gets things in the proper shape as you need without wasting space. 2:19 Reshape also has a great feature. 2:23 It'll infer what you want if you pass it a negative one to any dimension. 2:25 So for instance, let's say practice.reshape and 2:31 we're gonna give the first one, I don't know. 2:36 All I know is I want two columns, right? 2:38 So you'll see that that inferred 21, if we go here and say .shape. 2:41 See what happened there is we got 21 by 2, awesome right? 2:47 Now a very commonly reshaping that you'll want to do is all of the elements in 2:51 a single dimension. 2:56 Like you have something shaped, but you just want a single dimension. 2:57 Which you could re shape with just a -1, but there is a better way. 3:01 So we can say practice.ravel. 3:05 And you'll see what happens here is we get a single dimension and 3:08 it kind of unravels all of those dimensions. 3:13 Now this is a view, and not a copy. 3:17 If you do want a copy, you can use the method flatten. 3:20 Flatten creates a copy. 3:24 How do you remember that though? 3:26 Well the answer is, you don't. 3:28 Like most things in this programming world, 3:31 you just need to remember that you need to lean on documentation. 3:33 NumPy's official documentation is pretty good. 3:37 So let's take a quick peek. 3:40 [LAUGH] I have this page bookmarked, and 3:41 if it were an actual book, the page would be worn. 3:44 It would have tons of highlights and circled entries and lots and 3:46 lots of coffee stains. 3:50 NumPy's reference guide is very well indexed on the Oracle of your choice. 3:52 So I'm going to use Google and we'll say array manipulation and we'll do NumPy. 3:56 And this first hit here is what we want, so let's do that. 4:06 So here's the page, usually whatever you may be trying to do, 4:11 manipulation wise, can be found here. 4:14 So let's scroll down a little bit, changing the array shape. 4:17 Looky here, here's reshape, here's ravel, and here's flatten. 4:20 So let's click in to ravel here. 4:25 And it returns a contiguous flattened array, awesome. 4:29 Contiguous meaning one element after the other, no space in between. 4:34 And then flattened, meaning the dimensions are removed. 4:39 And a one dimensional array, containing the elements of the input, is returned. 4:42 A copy is made only if needed. 4:47 So there we go, it's a data view. 4:49 We could just look up this documentation and know that it's a data view. 4:51 These docs do end up working a bit like a Wikipedia hole. 4:55 Where one topic leads to another one and 4:58 before you know it you've read half the internet. 5:00 So just a word of warning. 5:02 This see also section is a bit of a learning rabbit hole. 5:05 So I wanna point out that I really like the notes and 5:09 examples that these pages provide. 5:12 Seeing the examples show the function in action, and 5:14 oftentimes there's extra information tucked away. 5:17 Like for instance, here, it says this is equivalent to reshape(-1, 5:19 which we just talked about right? 5:23 Remember how reshape takes the -1, if we wanted to, ravel, we could do that. 5:24 So here you go, here's np.raval, two arrays, there it is. 5:29 And it's saying it's equivalent, there's reshape -1 and there's ravel and 5:35 it's doing the same thing. 5:37 There's some extra talk in the doc here and in several functions about order. 5:41 This deals with the way that we talk about indices and 5:45 how they're stored in the memory. 5:48 Now, this is out of the scope of this course. 5:50 But I do want you to know that all of the functions that we are using use 5:52 C as the ordering default. 5:56 And, actually in most cases, you'll see that that makes sense. 5:59 If you'd like to learn more about index order, check the teacher's notes. 6:01 Okay, let's go into our hole here and let's take a look at flatten. 6:05 So let's assume that we found this page first. 6:12 And here in the description, we see very clearly that this returns a copy 6:14 of the array, collapsed into one dimension. 6:18 And if this wasn't what we wanted we could also come down here and 6:22 we could see that ravel would probably work. 6:25 There's another quick way to get at this information. 6:28 If you don't feel like leaving the comfort of your notebook. 6:32 So let's go back to the notebook. 6:34 And I totally get not wanting to leave the notebook. 6:38 There is no telling what's gonna distract you if you open up that browser. 6:40 Am I right? 6:43 So the NumPy team did something super clever and 6:44 built a search engine for dock strings. 6:47 So imagine this is a few days in the future, and you're in your notebook, and 6:50 you wanna make sure that you get that one function. 6:53 You know that one, right? 6:55 Hmm, what was the name of that one again, the one that makes the array flat? 6:57 But it's a view, and it's not a copy, what was that called? 7:00 Hmm, well I'm gonna search for it, I'm gonna look for it. 7:02 So I'm gonna np.lookfor, and I'm gonna look for flat, 7:07 cuz I remembered it had something to do with flattening. 7:11 So here's some stuff, here it is. 7:15 A contiguous flattened array, that was it, ravel. 7:18 And let's just make sure that that's right. 7:21 So I can come up here and I can say, np.ravel?, and do a question mark. 7:23 And is that what I was talking about? 7:28 Yes, contiguous flattened array. 7:31 So check this out, it's the actual doc string. 7:32 So it's got the parameters, there's the returns, there's your see also's. 7:35 There's the notes that we talked about and here's the examples. 7:38 Pretty rad, right? 7:41 This lookfor function super handy for 7:43 not getting interrupted and/or going down a documentation internet hole. 7:45 So anyways, let's get back to that bookmarked page and 7:50 explore what else is in here. 7:53 So I'm gonna comeback up here and I'm gonna go back. 7:55 And back again, get back to our list, awesome. 8:02 So, transpose-like operations. 8:05 Transposing lets you flip the order of the array around, 8:08 it switches the rows with the columns. 8:11 So if you had four row by three columns, transposing would 8:13 give you three rows by four columns, the same data, it's just flipped around. 8:16 So you'll see this referred to with the property of capital T here. 8:19 So let's go ahead, 8:23 let's get in there, cuz this is a little strange and you'll see them. 8:24 Here's a great example. 8:27 So we've made a two dimensional array. 8:28 We've got one, two, three, four rows, and if you do x.T, and 8:30 again, that is a property on there. 8:34 Now, it's flipped, right? 8:36 So we have one and three, and two and four. 8:38 So we have one and three as the rows, and two and four as the columns. 8:40 It just transposes it. 8:43 This is handy for quite a few equations that require you to invert a matrix. 8:45 I just wanted you to understand what the strangely named, property T is doing. 8:51 You'll see this thrown around quite a bit and examples and blog posts. 8:56 So let's go back and cut scootch down this document a little bit here. 8:59 Changing the number of dimensions, 9:04 changing the kind of array, joining arrays. 9:05 This is important here. 9:08 Much like we can split and join on strings, this is a similar idea. 9:10 It breaks apart and puts together multidimensional arrays. 9:13 Now the thing that I wanna point out here is that this is h prefix, that stands for 9:17 horizontal. 9:22 V is for vertical, and d is for depth in a three-dimensional space. 9:23 So again, these methods are all handy for 9:29 when you want to stitch together the proper matrix for a particular equation. 9:32 There are a plethora of options for 9:37 manipulating your arrays to match just about whatever shape you might need. 9:38 There are tons of well documented recipes for adding rows and columns. 9:42 I'm glad that you've got access to that array of manipulation 9:47 page in the documentation. 9:49 I'm sure it will serve you well, it has me. 9:50 Why don't we both go and add some notes to our notebook? 9:53 Make sure to include a link to that page. 9:56 Also, don't forget to take note of that super handy method 9:58 that we talked about to search the documentation, lookfor. 10:01 I'm going to review that manipulation page and 10:04 just kinda jot down anything I want to remember. 10:07 Now I know these manipulation methods have been a little abstract. 10:09 And it's kind of challenging to imagine why you might need to use them. 10:12 This is one of those cases where it's handy to know what you're capable of doing 10:17 when it comes time to use your skills. 10:20 And I don't know about you, but I always disliked that part of math class. 10:23 I was always like, but why? 10:26 Let's see if we can't answer that question. 10:28 Let's take a break and swing back and 10:30 start exploring some of the more powerful math features of this library. 10:32
You need to sign up for Treehouse in order to download course files.Sign up