Coding

Complete Python NumPy Tutorial (Creating Arrays, Indexing, Math, Statistics, Reshaping)

  • 00:00:00 how's it going everyone and welcome back
  • 00:00:01 to another video today we're going
  • 00:00:03 through the numpy library which is
  • 00:00:05 considered to be kind of the fundamental
  • 00:00:08 package for all scientific computing and
  • 00:00:10 python so it's a super super important
  • 00:00:13 library it's kind of the base for a lot
  • 00:00:16 of the other like major data science
  • 00:00:18 libraries and Python select pandas that
  • 00:00:21 I've done a video on before it builds
  • 00:00:23 pretty much like entirely off in the
  • 00:00:25 numpy library so it's super important
  • 00:00:28 and kind of because it's important is
  • 00:00:30 because it's this like base the way
  • 00:00:33 we're going to do this video is I'm
  • 00:00:34 going to start out with kind of a
  • 00:00:35 background information on how a numpy
  • 00:00:38 works and I think really having that
  • 00:00:40 intuition is helpful for when you
  • 00:00:42 actually start writing code with it so
  • 00:00:43 we'll do the background information and
  • 00:00:44 then after that we'll jump into all
  • 00:00:46 sorts of useful methods and ways you can
  • 00:00:51 utilize this library as far as actual
  • 00:00:53 code goes as always make sure to smash
  • 00:00:56 that subscribe button
  • 00:00:58 throw this video a thumbs up follow my
  • 00:01:01 tweeters tweeter gram insta gram Twitter
  • 00:01:05 github – hit the bell for the
  • 00:01:09 notifications throw this another thumbs
  • 00:01:12 up yeah to begin numpy is a
  • 00:01:16 multi-dimensional array library so what
  • 00:01:18 that means is you can use it on pi to
  • 00:01:19 store all sorts of data and
  • 00:01:21 one-dimensional arrays two-dimensional
  • 00:01:24 arrays three dimensional arrays four
  • 00:01:26 dimensional arrays etc and so the common
  • 00:01:29 question you kind of ask or I'm commonly
  • 00:01:31 asked when you know you first bring up
  • 00:01:34 numpy is why do you use numpy over lists
  • 00:01:38 so the main difference comes from the
  • 00:01:42 speed so lists they are very slow
  • 00:01:44 meanwhile numpy is very fast and so why
  • 00:01:50 are lists slow and numpy fast well one
  • 00:01:53 reason is because numpy uses fixed types
  • 00:01:57 so what that means is imagine we have
  • 00:01:59 this three by four matrix three rows
  • 00:02:03 four columns and it's all integer values
  • 00:02:07 and we're gonna kind of look at how the
  • 00:02:09 integer values differ between numpy and
  • 00:02:12 lists so let's just zoom in on that five
  • 00:02:15 that's there that matrix so our
  • 00:02:18 computers they don't see five they see
  • 00:02:20 binary that represents five and so this
  • 00:02:27 is the number five in binary and it's 8
  • 00:02:30 bits which makes up a byte so our
  • 00:02:32 computers read information in bytes so
  • 00:02:36 when we use numpy this this one bit five
  • 00:02:42 is actually by default going to be
  • 00:02:46 casted to this int 32 type which
  • 00:02:49 consists of 4 bytes and so it represents
  • 00:02:52 5 in a total memory space of 4 bytes say
  • 00:02:57 in 32 and you also we can even specify
  • 00:02:59 so by default it's in 32 but you could
  • 00:03:01 even specify that you didn't need all 4
  • 00:03:04 bytes I represent this value so you
  • 00:03:06 could specify within numpy that you want
  • 00:03:09 to maybe in in 16 which is 16 bits or 2
  • 00:03:12 bytes or even if you have really small
  • 00:03:14 values into 8 which is just a single
  • 00:03:16 byte on the other hand with lists
  • 00:03:22 there's a lot more information you need
  • 00:03:24 to store as an integer so it in lists
  • 00:03:27 lists use a built-in int type for Python
  • 00:03:30 and so that built in int type consists
  • 00:03:33 of 4 different things it consists of the
  • 00:03:35 object value which you know has its own
  • 00:03:39 bits associated with it object type the
  • 00:03:43 reference count how many times that that
  • 00:03:45 integer has been specifically like
  • 00:03:47 pointed at and the size of that integer
  • 00:03:50 value and so if we break this up into
  • 00:03:54 the actual binary that it represents we
  • 00:03:57 can take the object value and that's
  • 00:04:00 represented as a long which is like 8
  • 00:04:02 bytes the object type same deal
  • 00:04:04 reference count same deal and then the
  • 00:04:06 size I believe is a little bit smaller I
  • 00:04:09 think is only 4 bytes but as you can see
  • 00:04:11 that's a single integer within lists
  • 00:04:13 using the built in into
  • 00:04:15 it requires a lot more space than numpy
  • 00:04:18 so basically the takeaway from this is
  • 00:04:22 that because numpy uses less bytes of
  • 00:04:27 memory the computer can read less bytes
  • 00:04:30 of memory quicker obviously so it's
  • 00:04:32 faster in that regard another reason
  • 00:04:35 that I didn't specifically say is that
  • 00:04:38 when we're iterating through each item
  • 00:04:40 in a numpy array we don't have to do
  • 00:04:44 type checking each time so in Python
  • 00:04:46 built-in lists you could have a list of
  • 00:04:47 like an integer then a float then a
  • 00:04:49 string then a boolean and you'd have to
  • 00:04:52 check each element you're looking at
  • 00:04:54 what type it is
  • 00:04:55 but numpy we don't have to do that so
  • 00:04:57 another reason it's faster is that
  • 00:04:58 there's no type checking when iterating
  • 00:05:00 through objects moving on another reason
  • 00:05:03 that numpy is faster than lists is
  • 00:05:05 because numpy utilizes contiguous memory
  • 00:05:08 so what that means is imagine that this
  • 00:05:11 kind of array like structure is our
  • 00:05:13 computer's memory so we could store
  • 00:05:15 information in any one of these memory
  • 00:05:18 blocks so when a list the way that that
  • 00:05:22 would look in a lists memory is that our
  • 00:05:25 list would be kind of scattered around
  • 00:05:27 so maybe we have a list that takes up
  • 00:05:30 eight memory blocks the thing is that
  • 00:05:33 these memory blocks aren't necessarily
  • 00:05:36 next to each other so you have some
  • 00:05:37 information here you have some
  • 00:05:38 information here you know a good amount
  • 00:05:40 of information in here then you skip a
  • 00:05:43 block here here and skip two blocks you
  • 00:05:45 have some information here so it's all
  • 00:05:46 kind of scattered around so kind of if
  • 00:05:50 you have an eighth item array what that
  • 00:05:52 looks like is that that array is
  • 00:05:55 actually just or that list is just
  • 00:05:59 containing pointers to the actual
  • 00:06:01 information that's scattered around our
  • 00:06:04 computer's memory and so it's just the
  • 00:06:09 all the information is not right next to
  • 00:06:10 each other kind of if you have to bounce
  • 00:06:12 around your computer's memory
  • 00:06:14 and it's not super super fast to like
  • 00:06:17 rapidly go through and kind of
  • 00:06:20 potentially perform functions on all
  • 00:06:23 items at the time or subsets of the
  • 00:06:25 items numpy array however uses
  • 00:06:29 contiguous memory so all eight blocks in
  • 00:06:32 this case would be right next to each
  • 00:06:34 other and this has all sorts of
  • 00:06:35 advantages and also just to mention real
  • 00:06:38 quick you'd also kind of have to have to
  • 00:06:41 store somehow where the start of that
  • 00:06:43 memory is and then like the total size
  • 00:06:45 and the type of memory block but it's a
  • 00:06:49 lot easier than this kind of pointer
  • 00:06:51 structure that's up here and so the
  • 00:06:54 benefits of numpy using this contiguous
  • 00:06:56 memory are a couple of different things
  • 00:06:58 so the first benefit is that our CPUs or
  • 00:07:03 our computers have these Cindi vector
  • 00:07:07 processing units and so when this memory
  • 00:07:10 is all like right next to each other we
  • 00:07:12 can utilize this unit and basically what
  • 00:07:15 cindy stands for is single instruction
  • 00:07:17 multiple data so we could like if we
  • 00:07:21 have to do it in addition of like a lot
  • 00:07:22 of values instead of just doing one
  • 00:07:25 addition at a time we can use this cindy
  • 00:07:27 vector unit and basically perform
  • 00:07:30 computations on all of these values at
  • 00:07:32 one time so it's quicker in that regard
  • 00:07:35 another reason it's quicker is that we
  • 00:07:38 more effectively utilize our cache so
  • 00:07:41 are kind of our quicker memory in our
  • 00:07:42 computer basically if we load in all
  • 00:07:45 these values we can keep them close to
  • 00:07:48 where we need to access them and I
  • 00:07:52 perform all sorts of operations well as
  • 00:07:54 in the list case you maybe load in like
  • 00:07:56 half of this but then this other half
  • 00:07:59 because it's scattered around in
  • 00:08:00 different places you'd have to like go
  • 00:08:03 back and like reload that in to your
  • 00:08:06 cache like you know just be overall
  • 00:08:09 slower because you'd have to do more
  • 00:08:11 like longer memory lookups within your
  • 00:08:14 computer ok so we kind of went over some
  • 00:08:17 of the performance benefits but how are
  • 00:08:19 lists different from numpy well this we
  • 00:08:22 can do insertion deletion append
  • 00:08:24 concatenation etc and we can also do
  • 00:08:27 those same exact things in numpy I guess
  • 00:08:29 the big difference though is that within
  • 00:08:31 numpy we can do all that and we can do
  • 00:08:35 lots of lots more and we'll see the lots
  • 00:08:37 lots more throughout the video but as a
  • 00:08:40 simple example imagine we have these two
  • 00:08:44 arrays one thing that we can do that's
  • 00:08:46 really nice and numpy is that if we try
  • 00:08:48 to multiply these one item at a time we
  • 00:08:53 could do that in lists you couldn't
  • 00:08:55 multiply 1 + 1 3 into 5 + 3 etc but when
  • 00:09:02 we do the exact same computation within
  • 00:09:04 numpy it allows us to do these you know
  • 00:09:06 single value like item wise computations
  • 00:09:11 which is pretty neat and pretty useful
  • 00:09:13 so that's one example and you'll see a
  • 00:09:15 lot more throughout the video so
  • 00:09:17 applications have known pi there's all
  • 00:09:18 sorts of applications I think the first
  • 00:09:20 one the kind of the first one that comes
  • 00:09:22 to my mind is that is a kind of a MATLAB
  • 00:09:26 or placement you can do all sorts of
  • 00:09:28 mathematics with numpy I think I should
  • 00:09:30 say that I think the sci-fi library has
  • 00:09:34 even more mathematics like functions and
  • 00:09:37 whatnot so numpy it isn't cutting it for
  • 00:09:39 you try to look through the sci-fi
  • 00:09:42 documentation you might be vilified and
  • 00:09:44 even more but yeah it's pretty powerful
  • 00:09:46 the math that numpy can do it's useful
  • 00:09:50 in plotting is the back end of many
  • 00:09:53 different applications so pandas which
  • 00:09:57 I've done a video on before it is just
  • 00:09:59 like the core component of pandas
  • 00:10:02 library it really allows canvas to work
  • 00:10:04 if you've seen my Kinect for how to
  • 00:10:07 program the Hat video I use numpy to
  • 00:10:09 store the board and then in future
  • 00:10:13 videos that I'm going to do you can you
  • 00:10:15 can actually store images through numpy
  • 00:10:18 it's like PNG images you can use numpy
  • 00:10:22 to store all the information and like do
  • 00:10:24 all sorts of cool stuff at all post
  • 00:10:26 future videos on let's see also another
  • 00:10:30 I think useful reason to know numpy is
  • 00:10:34 that it's
  • 00:10:35 like pretty important for machine
  • 00:10:37 learning applications both directly and
  • 00:10:40 then also kind of indirectly because one
  • 00:10:43 of the key libraries or key kind of
  • 00:10:46 concepts you learn with machine learning
  • 00:10:48 is the idea of like tensors and tensors
  • 00:10:52 are pretty connected to kind of like the
  • 00:10:56 tensor libraries are pretty similar to
  • 00:10:57 like the Danone pi library it's just a
  • 00:10:59 way to store all sorts of values
  • 00:11:01 so knowing numpy will help you kind of
  • 00:11:04 be able to do some stuff with machine
  • 00:11:07 learning all right to get started the
  • 00:11:09 code the first thing you want to do is
  • 00:11:11 import the numpy library and just so
  • 00:11:14 we're on the same page I'm just a
  • 00:11:15 Jupiter notebook to use to code this up
  • 00:11:17 but you can use whatever editor you
  • 00:11:19 prefer also all this code that I'll be
  • 00:11:21 going through will be on my github and
  • 00:11:23 the link to that will be in the
  • 00:11:25 description
  • 00:11:25 okay so import numpy as NP if that works
  • 00:11:29 for you great if it didn't work you'll
  • 00:11:31 have to do a pip install so you can go
  • 00:11:34 ahead into your terminal and type in pip
  • 00:11:37 install numpy and so it's already
  • 00:11:47 installed for me so
  • 00:11:49 and if pip doesn't work for you try pip
  • 00:11:51 3 install numpy that should work so the
  • 00:11:54 first thing that's important to know is
  • 00:11:56 how to initialize an array so we'll just
  • 00:11:58 say that a equals NP array and then
  • 00:12:03 within this we just basically pass in a
  • 00:12:05 list so 1 2 3 this would be a
  • 00:12:08 one-dimensional array containing the
  • 00:12:10 values 1 2 3 as you see and you can go
  • 00:12:14 ahead you're not using different
  • 00:12:16 notebooks in print
  • 00:12:17 hey okay cool so we could also
  • 00:12:20 initialize a little bit more complex
  • 00:12:21 arrays so we could do like a 2d array of
  • 00:12:24 floats and I could do that the following
  • 00:12:26 way we have a list within a list
  • 00:12:33 so here's some floating values and then
  • 00:12:37 we're going to make this two-dimensional
  • 00:12:39 so here's some more float values and
  • 00:12:45 let's go ahead and print be cool so now
  • 00:12:52 that we know how to initialize arrays
  • 00:12:54 and you can keep doing this like I can
  • 00:12:57 nest lists within a list within a list
  • 00:12:59 to create a three dimensional array etc
  • 00:13:02 some other useful things to know about
  • 00:13:04 this is how do how do you get the
  • 00:13:08 dimension of your numpy arrays so if I
  • 00:13:13 did a dot number dimensions so this
  • 00:13:18 tells me that it's one dimensional for a
  • 00:13:21 and if I did B dot and it would be to
  • 00:13:25 shape is another important function so
  • 00:13:28 get shape if we do the first one a shape
  • 00:13:32 this was always the day oh it's a vector
  • 00:13:34 so it's only going to tell me the one
  • 00:13:35 dimension because it only has one
  • 00:13:37 dimension so it's size three if I do B
  • 00:13:40 dot shape it's gonna tell me the rows in
  • 00:13:42 the column so this is two rows and three
  • 00:13:44 columns so this should print out two by
  • 00:13:46 three as it does okay other things we
  • 00:13:50 want to know how much memory are numpy
  • 00:13:52 array is take up so we can get to type
  • 00:13:55 and you also get the size so if we want
  • 00:13:59 to get the type we do just a dot type
  • 00:14:04 sorry a dot data type in 32 by default
  • 00:14:09 so even though these are small values by
  • 00:14:12 default it specifies that it should take
  • 00:14:15 up 4 bytes or being in 32 if we wanted
  • 00:14:18 to specify what type we want it to store
  • 00:14:21 us so maybe we knew that we didn't have
  • 00:14:23 many like big values so we could do like
  • 00:14:27 an in 16 and so that would take up less
  • 00:14:30 size and you can see the difference in
  • 00:14:32 size and I say so right now it's in 16
  • 00:14:36 and if I want to see the size there's a
  • 00:14:38 couple different I guess important
  • 00:14:40 functions with this we could do a dot
  • 00:14:43 item size so this should tell me to
  • 00:14:45 bytes as it does if we left this as an
  • 00:14:50 inch 32 it will tell me four bytes down
  • 00:14:55 here as it does you can also do I think
  • 00:15:02 the total size I guess a dot size is the
  • 00:15:15 total number of elements so the total
  • 00:15:17 size would be a dot size times a dot
  • 00:15:20 item size another way to do that is I
  • 00:15:24 think just number of bytes as you see
  • 00:15:27 that's the same thing and you can also
  • 00:15:31 do this with be just like feed item size
  • 00:15:34 these are floats and I believe that this
  • 00:15:37 is an 8 byte type so if I do B dot item
  • 00:15:39 size as you see it's 8 so floats are
  • 00:15:42 gonna be bigger than floats are bigger
  • 00:15:47 than integers usually unless you define
  • 00:15:50 this as like in int 64 and so yeah you
  • 00:15:53 really I usually don't even worry about
  • 00:15:55 the datatype too much I don't specify it
  • 00:15:57 but if you really want to be efficient
  • 00:15:59 try to specify this so that it fits all
  • 00:16:02 your data but if yeah I guess it fits
  • 00:16:06 all your data as tightly as possible
  • 00:16:08 alright so now that we've gone through
  • 00:16:10 some of the basics let's actually show
  • 00:16:11 how we access flush change specific
  • 00:16:13 elements rows columns etc so imagine we
  • 00:16:16 have the array there's gonna be a
  • 00:16:22 two-dimensional array so I'm gonna make
  • 00:16:25 this kind of long and you'll see why in
  • 00:16:27 a second
  • 00:16:32 okay so this is a two by seven ray if I
  • 00:16:39 print that out okay and I could prove
  • 00:16:44 that it's a two by seven by doing it up
  • 00:16:46 shape that's just a reminder so what if
  • 00:16:49 we wanted to get this specific element
  • 00:16:52 well to do that we can use this notation
  • 00:16:56 of row comma column so this is the row
  • 00:17:01 index this is the column index so I
  • 00:17:03 could just do something just like a
  • 00:17:06 let's say I wanted to get this 13 right
  • 00:17:10 here well that would be in the second
  • 00:17:12 row but because we started Python
  • 00:17:14 indexing it zero be the first row and
  • 00:17:16 then the zero one two three four five
  • 00:17:20 fifth column so yeah that gives us the
  • 00:17:26 13 as you see down here the one thing
  • 00:17:28 that's kind of cool is you can also use
  • 00:17:30 the negative notation similar to lists
  • 00:17:34 so I could also say the negative second
  • 00:17:36 element would be 13 as well because this
  • 00:17:39 would be negative one and then negative
  • 00:17:40 two so there's a couple different ways
  • 00:17:43 to do this but we'll stick with the
  • 00:17:44 first one okay let's say we wanted to
  • 00:17:46 get a specific I can't spell row that's
  • 00:17:55 pretty straightforward as well so in
  • 00:17:57 this case if we wanted the first row we
  • 00:18:00 would do 0 and then because we want all
  • 00:18:02 columns we use the basic slight syntax
  • 00:18:04 similar to lists I can just do a single
  • 00:18:07 column and that will get me everything
  • 00:18:09 in the road that's nice what if we want
  • 00:18:11 to eat that specific column well if you
  • 00:18:14 know how to do rows you probably know
  • 00:18:16 how to do columns a let's say we wanted
  • 00:18:19 this row or this column right here 3 and
  • 00:18:22 10 that would be all the rows and then 0
  • 00:18:25 1 2 column that gives me the 3 10 and
  • 00:18:30 from here we can do even some more like
  • 00:18:33 tricky stuff so
  • 00:18:35 [Music]
  • 00:18:38 we're just say getting a little more
  • 00:18:41 fantasy and we have the start index this
  • 00:18:47 is just a reminder start index and index
  • 00:18:49 and then finally the step size so if I
  • 00:18:55 wanted to let's say get between the
  • 00:18:57 numbers 2 & 6 every other element so 2 4
  • 00:19:02 & 6 will just specify that I would do
  • 00:19:06 well they want the first row and then I
  • 00:19:10 want to start at the first element the
  • 00:19:13 two and I actually screwed that up it
  • 00:19:15 should be 1 so I start at the 2 then I
  • 00:19:18 want to end here at the 6 which is the
  • 00:19:23 its exclusive so that would be I want to
  • 00:19:27 actually go to the 6th element and then
  • 00:19:29 I want to step by 2 because I wanted to
  • 00:19:31 for 6 so I do 1 6 2 and that gives me 2
  • 00:19:35 4 6 and I can also use the negative here
  • 00:19:38 and do like negative 2
  • 00:19:41 yeah what happened there Oh see that was
  • 00:19:43 going to give me backwards I didn't want
  • 00:19:45 to change it there I wanted to change
  • 00:19:47 the 6 to be negative 2 okay it's
  • 00:19:51 exclusive so when this to actually be
  • 00:19:53 negative one a little bit more of a
  • 00:19:55 fancy way to do that
  • 00:19:56 okay so that's how you access elements
  • 00:19:59 and then if we wanted to change
  • 00:20:01 something it's pretty straightforward to
  • 00:20:03 it say I wanted to change that 13 that I
  • 00:20:05 originally accessed well I can just do
  • 00:20:08 like 20 if I print out a now that
  • 00:20:15 original element that was 13 is now 20
  • 00:20:17 and you can do the same thing for series
  • 00:20:22 of numbers so like for an entire column
  • 00:20:24 let's say we wanted to replace this 310
  • 00:20:26 column I would do something like a colon
  • 00:20:33 2 equals let's say I wanted it to be all
  • 00:20:37 fives I could start like this and as you
  • 00:20:42 see it's all 5 5 5 and then if I wanted
  • 00:20:46 it to be two different numbers you just
  • 00:20:47 kind of specify the same shade
  • 00:20:49 as what you've subsequence so I'd be
  • 00:20:52 like one two so now you see that we have
  • 00:20:55 a one two in that position really
  • 00:20:59 quickly that just show a 3d example if I
  • 00:21:04 had a 3d so we'll say B equals numpy
  • 00:21:14 array of all this and if I print B so if
  • 00:21:24 we want to get a specific element here
  • 00:21:25 the recommendation I have is work
  • 00:21:28 outside in so work outside in so let's
  • 00:21:31 say I wanted this for right here
  • 00:21:33 well the farthest outside would be which
  • 00:21:37 one of these do I want and I want the
  • 00:21:39 first set so I want this area right here
  • 00:21:41 so if I wanted that I would do B 0 and
  • 00:21:45 then now that I'm in here I want the
  • 00:21:48 second row so I want the 3/4 so that
  • 00:21:52 would be 1 and now that I'm within this
  • 00:21:55 I want the first or the second you have
  • 00:21:59 a second element but the first index
  • 00:22:01 like that so that gives me the 4 and you
  • 00:22:04 can do similar type stuff with like the
  • 00:22:07 colons in here so each one of these
  • 00:22:08 dimensions that you're indexing you can
  • 00:22:11 be all fancy with how you access
  • 00:22:14 elements so I can do something like this
  • 00:22:16 and you know get 3 4 7 8 you can kind of
  • 00:22:21 play around with this and see how
  • 00:22:24 changing different things changes what
  • 00:22:26 you get and if you wanted to replace in
  • 00:22:29 this case basically just have to create
  • 00:22:33 a subsequence that's the same dimension
  • 00:22:36 so if I did it'd be 1 this it gives me 3
  • 00:22:42 4 7 8 let's say I wanted to change that
  • 00:22:44 to 9 9 8 8 as long as it's the same
  • 00:22:51 dimension it's gonna work
  • 00:22:54 so 9 9 8 8 if I try to like do something
  • 00:22:57 like 9
  • 00:22:57 nine eight eight it's gonna have an area
  • 00:23:00 alright so that's the basics of indexing
  • 00:23:02 I think it by the it at the end of the
  • 00:23:04 video I'll do a little like challenge
  • 00:23:08 problem on like some advanced indexing
  • 00:23:10 so look at the end of the video for that
  • 00:23:12 alright next let's go through how to
  • 00:23:14 initialize all sorts of different types
  • 00:23:16 of arrays so to start off let's
  • 00:23:18 initialize in all zeros matrix and to do
  • 00:23:24 that there's a nice built-in function
  • 00:23:27 called NP zeros and we can first I guess
  • 00:23:32 actually all we really need to do is
  • 00:23:33 specify a shape so I did like MP zeros
  • 00:23:36 five it's gonna just give me a vector of
  • 00:23:38 five like five but I also can pass in a
  • 00:23:42 more complex shape so if I wanted it to
  • 00:23:44 be like a two by two or two by three
  • 00:23:47 let's say as you see there I could do
  • 00:23:49 three dimensional 2 by 3 H by three
  • 00:23:52 could even do four dimensional if I
  • 00:23:54 wanted to 2 by 3 by 3 by 2 yeah it gets
  • 00:23:58 pretty crazy but yeah you can do all
  • 00:24:00 sorts of zeros with that next let's do
  • 00:24:07 in all ones matrix pretty similar to the
  • 00:24:10 last one and P dot ones of let's say
  • 00:24:15 four by two by two and there you go and
  • 00:24:21 you're getting also specify the data
  • 00:24:23 type here so if you wanted like all ones
  • 00:24:26 but in 32 you can go ahead and do that
  • 00:24:30 so all ones all zeros however you might
  • 00:24:35 want to initialize some matrix that's
  • 00:24:37 not ones or zeros and the other number
  • 00:24:41 so for that you can do NP full and this
  • 00:24:45 one takes in two parameters so the first
  • 00:24:48 is the shape so two by two and then the
  • 00:24:52 next is the value so if I wanted at all
  • 00:24:54 99's then it's a two by two with 99
  • 00:24:59 another useful and you can you know that
  • 00:25:03 has a data type too so
  • 00:25:05 that to be float32 there you go
  • 00:25:12 and I'll put a link in the description
  • 00:25:15 to a list full of these like array
  • 00:25:17 creation routines useful to know is
  • 00:25:21 there's also this full like there's this
  • 00:25:30 full like method and basically that just
  • 00:25:32 allows us to take a shape that's already
  • 00:25:35 built so let's imagine we wanted to
  • 00:25:38 reuse that this array that we I guess
  • 00:25:42 had in the last section
  • 00:25:45 a I think that's still loaded and then
  • 00:25:48 we just make sure well I can pass in and
  • 00:25:51 make a array that's the same shy size of
  • 00:25:57 fours let's say by doing full like
  • 00:26:03 they're actually I think I don't even
  • 00:26:04 have to pass in eat up shape I just have
  • 00:26:09 to pass in hey there you go if I didn't
  • 00:26:12 use full light I would have to do full
  • 00:26:14 of a dot shape I don't know if that's
  • 00:26:17 that useful for you but I guess it's
  • 00:26:19 potentially good to know ok next one
  • 00:26:26 let's say we wanted to initialize a
  • 00:26:32 array or a matrix of random numbers so
  • 00:26:37 random decimal numbers to start so do
  • 00:26:41 that we do n peed a random door and and
  • 00:26:46 we specify the shape so let's say 4 by 2
  • 00:26:51 [Music]
  • 00:26:56 actually confused tuple state
  • 00:27:01 oh okay yeah this one's a little bit
  • 00:27:07 different so instead of passing in a
  • 00:27:09 tuple you can pass in directly the
  • 00:27:12 integers you wanna the integers of the
  • 00:27:15 shape so it's a kind of weird thing to
  • 00:27:17 remember so if I did the four by two
  • 00:27:19 this way I would actually pass it in
  • 00:27:21 like that and when you get errors like
  • 00:27:23 this often times you can just do a quick
  • 00:27:25 Google search and realize that that's
  • 00:27:27 what you need to do so I can even keep
  • 00:27:29 going so I could do a four by two by
  • 00:27:31 three random numbers between zero and
  • 00:27:34 one
  • 00:27:35 I could also pass in something like a
  • 00:27:38 dot shape I don't know if this would
  • 00:27:40 work
  • 00:27:41 let's try yeah so if you wanted to pass
  • 00:27:45 in like a shape you can do a random
  • 00:27:47 sample data shape and that now you see
  • 00:27:53 gives us the same shape as our a from up
  • 00:27:57 here so yeah Rand and then there's
  • 00:28:01 random sample which is another method
  • 00:28:04 we'll keep it as a brand of four by two
  • 00:28:07 okay what if you didn't want just
  • 00:28:10 decimal numbers but you wanted random
  • 00:28:12 like integer values well to do that we
  • 00:28:18 can do random and I see I'm getting NPI
  • 00:28:25 random brand int and in this one we're
  • 00:28:29 gonna pass in the start value or if you
  • 00:28:31 don't specify a start value it's gonna
  • 00:28:33 just start at zero and so if you don't
  • 00:28:38 specify a shape then it's just gonna do
  • 00:28:41 one number so let's say we wanted a
  • 00:28:43 three by three yeah what did I do wrong
  • 00:28:47 and this is not shape it's actually size
  • 00:28:50 and yeah all the documentation has these
  • 00:28:54 like you know you're not expected to
  • 00:28:56 memorize all of these things what I
  • 00:28:58 think it is helpful to see is that you
  • 00:28:59 see that you can do these types of
  • 00:29:01 things so like when you're thinking
  • 00:29:03 about a problem you can like kind of
  • 00:29:05 point back like oh I remember that
  • 00:29:07 that's possible
  • 00:29:08 maybe do a Google search
  • 00:29:10 how to get it but yeah Brianna Brandon
  • 00:29:13 0-7 with size 3 by 3 is here you could
  • 00:29:18 also specify like a different parameter
  • 00:29:19 so that's I went forward to 7 and I
  • 00:29:23 think and if I keep running this to kind
  • 00:29:25 of cool you can see it changing and so
  • 00:29:27 it looks like that 7 is exclusive so if
  • 00:29:29 I wanted it include 7 I would stop a
  • 00:29:31 little bit later get also photo in like
  • 00:29:34 negative numbers here cool all right
  • 00:29:39 what else other than random integers
  • 00:29:42 maybe you wanted to do like the identity
  • 00:29:43 matrix you do identity of 3 this one
  • 00:29:48 only needs one parameter because the
  • 00:29:50 identity matrix look by its nature is
  • 00:29:53 going to be a square major matrix what
  • 00:30:03 else is useful maybe it's useful to
  • 00:30:08 repeat a array a few times so to do that
  • 00:30:11 you could do say we have the array 1 2 3
  • 00:30:18 let's say I wanted to repeat that three
  • 00:30:22 times passing the array you want to
  • 00:30:29 repeat and then let's print r1 see what
  • 00:30:32 happens okay and then if I specify the
  • 00:30:37 axis equals zero
  • 00:30:39 I don't know did do anything what I can
  • 00:30:42 do is make this a two-dimensional array
  • 00:30:44 I think because it was a vector it
  • 00:30:46 didn't do what I wanted to what I wanted
  • 00:30:48 to do is one two three or one two three
  • 00:30:51 one two three one two three so if I
  • 00:30:55 wanted to do that now I made this a two
  • 00:30:59 dimensional array and it will repeat the
  • 00:31:01 inner part on the 0th axis so I'll be
  • 00:31:05 basically making it rose there you go so
  • 00:31:08 if I make this equal the one that's
  • 00:31:12 gonna be what we saw before
  • 00:31:16 cool
  • 00:31:33 okay so next here's a picture of an
  • 00:31:37 array I want you to try to initialize
  • 00:31:38 using everything that we kind of just
  • 00:31:40 went through so all these different
  • 00:31:42 methods so look at this picture and then
  • 00:31:46 try to put it together without just
  • 00:31:48 manually typing out to all the numbers
  • 00:31:50 because you can imagine like this isn't
  • 00:31:52 too too big but if you got into a matrix
  • 00:31:55 that was massive you'd want to know how
  • 00:31:57 to build it up using these kind of like
  • 00:31:59 fundamental concepts okay so here's the
  • 00:32:03 solution to that so I can do output
  • 00:32:06 equals I'm going to start with making
  • 00:32:09 everything ones so ones and so we have
  • 00:32:13 5×5 of ones print output so this is what
  • 00:32:21 I have now
  • 00:32:21 okay and now basically what we're going
  • 00:32:23 to do is fill in this middle part with
  • 00:32:26 zeros so Z I wanted to say equals NP dot
  • 00:32:30 zeroes and that's going to be a three by
  • 00:32:33 three and if I print Z now we have this
  • 00:32:39 now what I can do is fill in the middle
  • 00:32:44 element so that's one one with a nine
  • 00:32:47 and now if I print Z we get this and
  • 00:32:51 then finally we need to replace the
  • 00:32:53 middle part of the ones matrix so output
  • 00:32:56 the middle part so that's gonna be the
  • 00:32:58 first row to the third row so I want the
  • 00:33:04 first row to the third row and that I
  • 00:33:08 want the same thing with columns because
  • 00:33:10 it's the middle first column to the
  • 00:33:12 third column and actually this is
  • 00:33:15 exclusive value so it needs to go to
  • 00:33:16 four and that's gonna equal Z and now
  • 00:33:24 what happens when I print output is yay
  • 00:33:29 we got what we're looking for and
  • 00:33:31 actually one thing that I think it's
  • 00:33:32 nice is instead of using four I can also
  • 00:33:35 do negative one so basically the from
  • 00:33:37 the first element to the last
  • 00:33:40 yeah and as you see it didn't change
  • 00:33:43 this last initialization I want to go
  • 00:33:45 through I guess is a little bit
  • 00:33:46 different it's a over on the concept of
  • 00:33:49 copying but something you've got to be
  • 00:33:50 really careful about so I'm just going
  • 00:33:52 to quickly mention it I want to act my
  • 00:34:00 age invoice there we go okay so imagine
  • 00:34:03 we have two arrays or we have one array
  • 00:34:08 let's call it a and so you know a is
  • 00:34:13 just a normal array as you can see and
  • 00:34:16 let's say we want to make be a direct
  • 00:34:18 copy of a so now I'm going to just do B
  • 00:34:21 equals a and then print out B and as you
  • 00:34:25 can see it's still 1 2 3 and so I'm like
  • 00:34:29 okay I have this copy like things are
  • 00:34:31 cool it's fine I want to change the
  • 00:34:34 first element in B so I'm gonna do B 0
  • 00:34:36 equals 100 here's the issue I print out
  • 00:34:42 B looks good the issue lies in if I
  • 00:34:46 print out a look what happens I just
  • 00:34:49 printed out a and a now has a 100
  • 00:34:54 instead of the 1 2 3 that I initially
  • 00:34:56 said it as and that's because when we
  • 00:34:59 did B equals a we just said that the the
  • 00:35:03 variable name B points the same thing as
  • 00:35:06 a does we didn't tell like numpy to make
  • 00:35:10 a copy of what is the contents of a so
  • 00:35:14 that's the that's why the because we're
  • 00:35:16 just pointing at the same exact thing
  • 00:35:18 that a is pointing when we change the
  • 00:35:20 value it also changes the value of a so
  • 00:35:23 if we want to prevent that we can use
  • 00:35:24 this dot copy function oh sorry I
  • 00:35:26 shouldn't do it yet B equals a dot copy
  • 00:35:29 and then when we run the sale as you can
  • 00:35:33 see 1 2 3 still there because now we're
  • 00:35:37 just copying the contents of what's in a
  • 00:35:39 and if I print B it has the 100 200 100
  • 00:35:44 2 3 okay so one of the big uses of numpy
  • 00:35:47 is all the math capabilities it offers
  • 00:35:51 just to kind of show some of that one
  • 00:35:54 thing that it can do is element-wise
  • 00:35:57 want to make this four values
  • 00:35:59 element-wise addition subtractions
  • 00:36:02 element-wise I guess arithmetic so here
  • 00:36:05 we have a printout a and if I wanted to
  • 00:36:11 do something like a plus two adds to
  • 00:36:15 each element you can do a minus to
  • 00:36:18 subtract two from each element a times
  • 00:36:21 two as you can see a divided by two
  • 00:36:28 divides everything by two one thing to
  • 00:36:32 note with and you can also do stuff like
  • 00:36:34 a plus equals two so now if I printed
  • 00:36:37 out a in this column it's going to be
  • 00:36:42 two plus everything it's kind of cool
  • 00:36:47 you can do like the same type of math
  • 00:36:48 that you can do in Python you can also
  • 00:36:52 create another array and P dot a and
  • 00:36:55 that's like let's say one zero one zero
  • 00:37:03 I can do something like a plus B and
  • 00:37:07 that should be two two four four
  • 00:37:09 oh and because I added their degree run
  • 00:37:13 this okay
  • 00:37:14 two two four four like we expect so all
  • 00:37:17 sorts of useful things you could even do
  • 00:37:18 like a to the second power
  • 00:37:23 one four nine sixteen and that might
  • 00:37:25 have made it a bigger data type I'm not
  • 00:37:27 sure cool we can do stuff like take the
  • 00:37:34 sine of all the values so let's say we
  • 00:37:37 had a we do MP dot sign passin a gives
  • 00:37:42 us all the sinusoid of all those values
  • 00:37:45 which you know and you have like cosine
  • 00:37:49 of all those values all sorts of useful
  • 00:37:51 things that you can form on an entire
  • 00:37:53 array or entire matrix all at once and
  • 00:37:56 if you want all the different things
  • 00:37:58 that you can do I'll paste in a link
  • 00:38:02 here this will all be part of the
  • 00:38:05 as I mentioned before I have this on my
  • 00:38:06 github so if you look in the description
  • 00:38:09 you can find this exact notebook say
  • 00:38:12 yeah look up their routines right here
  • 00:38:15 for math all sorts of cool stuff alright
  • 00:38:20 moving on we're going to still be in
  • 00:38:23 math but let's jump into linear algebra
  • 00:38:27 type stuff so here we are doing a linear
  • 00:38:29 algebra ok so this was kind of like
  • 00:38:32 basic all sorts of functions you do on
  • 00:38:35 elements linear algebra so this is like
  • 00:38:38 really I feel like when I'm using MATLAB
  • 00:38:40 it would be doing these linear algebra
  • 00:38:42 type stuff so let's say we have two
  • 00:38:45 matrices and the big difference with
  • 00:38:47 linear algebra is like we're not doing
  • 00:38:49 element wise so like in this case this B
  • 00:38:53 we're doing element wise computation so
  • 00:38:57 like 8 times be in you know linear
  • 00:39:03 algebra you're trying to multiply
  • 00:39:05 matrices and that's a different process
  • 00:39:07 so let's say we have two matrices will
  • 00:39:11 have a and I'm going to use this syntax
  • 00:39:13 we learned about earlier I want to say
  • 00:39:15 this is a 2 by 3 matrix of all twos
  • 00:39:21 actually let's make this 2 by 3 matrix
  • 00:39:25 of ones so we have a as you can see and
  • 00:39:30 then we'll have B which is equal to NV
  • 00:39:36 dot full it's going to be a 3 by 2 and
  • 00:39:40 it's going to be a value 2 so if I print
  • 00:39:43 out B now we have this and if you
  • 00:39:47 remember linear algebra you have to have
  • 00:39:49 the columns of the first matrix be the
  • 00:39:52 equal to the rows of the second one so
  • 00:39:56 as you can see this has three columns
  • 00:39:57 and this has three rows so we're good
  • 00:39:59 there so we would multiply this row by
  • 00:40:02 this column and you know you do the the
  • 00:40:05 process of my Trischka multiplication I
  • 00:40:07 don't walk through the whole thing but
  • 00:40:08 we should end up with a 2 by 2 matrix
  • 00:40:12 the end if we want to do matrix
  • 00:40:16 multiplication and it doesn't just
  • 00:40:18 automatically affirm if you try to do
  • 00:40:19 any times B it's not going to work
  • 00:40:21 because these are different sizes so we
  • 00:40:23 can do is MP has a matrix multiply
  • 00:40:26 function and if I pass an A then passing
  • 00:40:29 B we get six six six six should I say
  • 00:40:35 enough sixes I don't know but yeah did
  • 00:40:38 multiply those two matrices you know and
  • 00:40:42 if I try to switch up this dimension in
  • 00:40:44 the middle it's not going to work
  • 00:40:46 because it's now incompatible
  • 00:40:50 that's matrix multiplication
  • 00:40:52 you could also want to do maybe some
  • 00:40:54 other stuff with matrices so let's
  • 00:40:56 imagine I wanted to create the herb to
  • 00:40:59 find the let's say determinant of a
  • 00:41:03 matrix so we could as a sanity check you
  • 00:41:07 know makes C equal the identity matrix
  • 00:41:10 and if you are familiar with linear
  • 00:41:12 algebra you know that the identity
  • 00:41:14 matrix has a determinant of one so if I
  • 00:41:19 do a linear algebra determinant of C we
  • 00:41:22 should get one one point now as we get
  • 00:41:25 so find determinant you know and there's
  • 00:41:32 all sorts of other good things like
  • 00:41:34 eigenvalues you know the inverse of a
  • 00:41:38 matrix so what what do you multiply by a
  • 00:41:41 matrix to get the identity matrix and so
  • 00:41:45 yeah all sorts of good stuff on that
  • 00:41:47 like I guess I'll do mmm and if you want
  • 00:41:53 to have all this information on the
  • 00:41:55 other types of linear algebra stuff here
  • 00:41:58 is some useful information definitely go
  • 00:42:01 to this link and as I've said a couple
  • 00:42:04 times in this video this notebook is on
  • 00:42:07 my github page so you can find all this
  • 00:42:10 there but yeah there's so many different
  • 00:42:12 things that you can do with matrices in
  • 00:42:15 lineage by using the
  • 00:42:17 by library okay continuing on words
  • 00:42:21 let's look at some statistics with numpy
  • 00:42:27 so kind of the easiest things we might
  • 00:42:30 think about when we think about sorry
  • 00:42:35 statistics there's like men mean max etc
  • 00:42:39 so let's say we have this array so let's
  • 00:42:45 say we wanted to take the min of it you
  • 00:42:47 can just do NP dot min of stats that's
  • 00:42:51 gonna give us the one that you see there
  • 00:42:53 you do NP max of stats six you could
  • 00:42:59 also do it on like a row basis so if I
  • 00:43:02 said axis equals one that's going to
  • 00:43:05 give me the min of the first row and the
  • 00:43:08 men of the second row or maybe this is a
  • 00:43:12 better way to see it if I said axes
  • 00:43:14 equals 0 well it's gonna give me all the
  • 00:43:16 values that are up top here because
  • 00:43:17 those are all the the mins so yeah you
  • 00:43:21 can do all sorts of cool stuff with min
  • 00:43:23 to the max with this same thing with max
  • 00:43:28 let's say axis equals zero x equals 1 3
  • 00:43:34 & 6 is the biggest value 3 is the
  • 00:43:37 biggest value in the 6 is the biggest
  • 00:43:38 value you can also do in feed out some
  • 00:43:40 of stats if I do it just as is it's
  • 00:43:44 gonna sum up all of the elements in the
  • 00:43:46 matrix and then same thing I can do row
  • 00:43:49 or column so actually equals 0 is going
  • 00:43:54 to add up all these terms going
  • 00:43:56 downwards next let's talk a little bit
  • 00:43:58 about reorganizing arrays so kind of the
  • 00:44:01 I would say the key method within
  • 00:44:03 reorganizing arrays so if I have the
  • 00:44:05 array I want to call it before and let's
  • 00:44:10 say that that is equal to this value
  • 00:44:14 right here so we have it before I'll
  • 00:44:17 print 4 out looks like that so let's say
  • 00:44:22 we wanted to instead of this shape that
  • 00:44:25 it currently
  • 00:44:26 a two-by-four let's say we wanted to
  • 00:44:31 make it a I don't know a eight by one or
  • 00:44:36 something or maybe a four by two or a
  • 00:44:40 yeah all sorts of different things we
  • 00:44:42 could do I'll start with a by one so we
  • 00:44:45 have before and if we wanted to make it
  • 00:44:48 something else we can do after equals
  • 00:44:51 before dot reshape and then we pass in
  • 00:44:54 the new size we want it to have so if we
  • 00:44:57 wanted it to be an eight by one and pass
  • 00:44:59 it in like that and we can print out
  • 00:45:01 after as you can see it's an eight by
  • 00:45:04 one now I could also say maybe I wanted
  • 00:45:07 it to be a four by two so now you got
  • 00:45:10 that you could even pass it in as a two
  • 00:45:14 by two by two as long as it has the same
  • 00:45:17 amount of values like it's fair game so
  • 00:45:19 as you see two by two by two still works
  • 00:45:22 with the reshape what doesn't work is
  • 00:45:25 like if I you wanted it to be two by
  • 00:45:27 three the values don't fit in so when
  • 00:45:30 you get errors but using that of shape
  • 00:45:31 it's usually because there's a mismatch
  • 00:45:33 between the shape you're trying to
  • 00:45:35 resize it to versus the original shape
  • 00:45:39 moving onwards this look at vertical
  • 00:45:41 stacks so vertically stacking vectors or
  • 00:45:48 matrices and you know dimensions are
  • 00:45:51 important in vertical stack as well so
  • 00:45:54 vertical stacking matrices let's say we
  • 00:45:56 had these two arrays if I wanted to
  • 00:46:02 stack you know one two three four on top
  • 00:46:05 of five six seven eight I can do NP dot
  • 00:46:08 V stack and I can pass in V 1 V 2 and as
  • 00:46:14 you see now they're part of the same
  • 00:46:16 matrix and one two three four is on top
  • 00:46:20 of five six seven eight
  • 00:46:21 what I can even do is keep passing these
  • 00:46:23 in so let's say I wanted like three
  • 00:46:25 copies of this five six seven eight and
  • 00:46:28 only one copy of
  • 00:46:29 or I could enter tweet we them that's a
  • 00:46:32 vertical stack horizontal stacks are
  • 00:46:36 pretty similar and also note here like I
  • 00:46:39 can't do that the size of some dish mass
  • 00:46:41 Mitch mis-match so yep horizontal stack
  • 00:46:46 very similar let's see we had well use a
  • 00:46:53 sub notation we've learned before we had
  • 00:46:55 these two matrices so if I printed out
  • 00:46:59 each one you gotta like that and then
  • 00:47:02 each two is this well I want each to be
  • 00:47:05 on the back of each one I can just do an
  • 00:47:07 MP dot each stack horizontal stack and
  • 00:47:10 that it will be H 1 and H 2 and that did
  • 00:47:16 not work because it did not surround
  • 00:47:19 this in parenthesis either parenthesis
  • 00:47:22 or brackets I think they both work yeah
  • 00:47:23 there you go so now we've horizontally
  • 00:47:27 stacked to the zeros on top of the earth
  • 00:47:29 to the right of the ones alright let's
  • 00:47:31 get into some miscellaneous things so
  • 00:47:33 first off imagine you have you know some
  • 00:47:36 sort of text file with all sorts of data
  • 00:47:38 and for whatever reason you choose you
  • 00:47:41 don't want to use pandas but you want to
  • 00:47:43 load all that data from that file into a
  • 00:47:47 numpy array well we can do that without
  • 00:47:50 too much trouble so I have this text
  • 00:47:55 file that I created as you can see here
  • 00:47:57 this is on my github page you can
  • 00:47:59 download it there this is just really
  • 00:48:01 simple data but it shows kind of what
  • 00:48:03 you can do with it all delimited by
  • 00:48:06 commas called data txt but what I can do
  • 00:48:11 is I can do MP and I can use this
  • 00:48:14 function called gen from text and I pass
  • 00:48:18 in the name of the file which is dated
  • 00:48:21 txt and then I pass in a delimiter which
  • 00:48:24 is the separator and that's a comma and
  • 00:48:27 if I do that I see that I get that data
  • 00:48:30 that I just showed you
  • 00:48:34 you get that
  • 00:48:37 crease the zoom here I get that as an
  • 00:48:43 array so that's pretty nice so I'll just
  • 00:48:47 call this file data equals and file data
  • 00:48:54 yeah one thing you notice though is a
  • 00:48:57 automatically cast it to a float type
  • 00:49:03 and what if I wanted it to be an integer
  • 00:49:05 well I can do another function as type
  • 00:49:08 which basically copies all the data into
  • 00:49:11 a whatever format you specify so I'll
  • 00:49:15 say in 32 and as you can see now all
  • 00:49:18 this stuff is here and if I go ahead and
  • 00:49:21 print file data now it is back to what
  • 00:49:26 we had originally and the reason it's
  • 00:49:27 back is that this actually makes a copy
  • 00:49:29 because the float type in the in 32 type
  • 00:49:32 are different sizes they can't just like
  • 00:49:34 in place copy everything it doesn't
  • 00:49:37 really make sense to so if I did file
  • 00:49:39 data equals file data dot as type int 32
  • 00:49:47 and then printed out file data as you
  • 00:49:51 can see now it's all floats so let's say
  • 00:49:53 you load data from a file and you could
  • 00:49:55 change up this delimiter based on how
  • 00:49:57 your data is split but I think that this
  • 00:49:59 gen from text will handle your new line
  • 00:50:03 breaks properly if that's how its
  • 00:50:04 formatted right in the comments if you
  • 00:50:07 have any questions about this okay the
  • 00:50:10 second thing I want to go through is
  • 00:50:12 what happened there I didn't want that
  • 00:50:15 to be markdown the second thing I want
  • 00:50:20 to go through with this miscellaneous
  • 00:50:21 section is some advanced indexing so
  • 00:50:27 there's some really cool stuff you can
  • 00:50:29 do with numpy I'm gonna say boolean
  • 00:50:34 masking and advanced indexing so what
  • 00:50:40 can we do here so let's say I wanted to
  • 00:50:43 learn where in file data the value is
  • 00:50:49 great
  • 00:50:49 than 50 so if I just type in file data
  • 00:50:51 greater than 50 it's pretty cool that
  • 00:50:54 you get false or true based on whether
  • 00:50:58 that specific location was greater than
  • 00:51:00 50 so as you can see there's four falses
  • 00:51:02 and then a true if we go to our data for
  • 00:51:05 falses and then 196 is in fact greater
  • 00:51:08 than 50 so that's like one way and you
  • 00:51:11 can do all sorts of cool stuff with us
  • 00:51:13 like you degree of any equal to you know
  • 00:51:16 all sorts of different combinations one
  • 00:51:19 thing that's pretty neat is you can do
  • 00:51:20 file data and then you can index based
  • 00:51:24 on where it is greater than 50 and by
  • 00:51:29 doing this you grab only the values that
  • 00:51:34 actually have a value greater than 50 so
  • 00:51:37 that is pretty cool and kind of the
  • 00:51:39 reason that this right here works is
  • 00:51:41 that one thing I did not mention until
  • 00:51:44 now is that you can can index with a
  • 00:51:51 list and numpy which is pretty coolest
  • 00:51:53 if you have the array one two three four
  • 00:51:57 five six seven eight nine and I wanted
  • 00:52:02 that say zeroth spot the second spot and
  • 00:52:06 then the last spot I could do
  • 00:52:11 MP or let's say that this is a I could
  • 00:52:16 do a of 0 1 and or I wanted to 3 and 9
  • 00:52:25 so I would do 1 2 and then 8 as you see
  • 00:52:31 that gives me 2 3 and 9 I passed in a
  • 00:52:33 list and it indexed those spots so
  • 00:52:36 basically it also works if you like had
  • 00:52:38 choosen falses it like basically if it
  • 00:52:41 is true then it knows to take it if it's
  • 00:52:43 false doesn't so that's why this up here
  • 00:52:46 works
  • 00:52:58 we could do all sorts of other things so
  • 00:53:00 let's say I wanted to figure out if any
  • 00:53:05 value in any of these columns was
  • 00:53:07 greater than 50 so I can do a NP dot any
  • 00:53:12 file data greater than 50 and the axis
  • 00:53:16 of zero so that should tell me like if
  • 00:53:19 we looked downwards on all of these or
  • 00:53:22 any of the values greater than 50 let's
  • 00:53:25 see what happens so false false false
  • 00:53:28 false true that's correct
  • 00:53:30 true these two values are greater than
  • 00:53:32 50 this even though this one isn't false
  • 00:53:37 true yeah true true false true cool so
  • 00:53:44 this is telling us yeah where what
  • 00:53:46 columns have a value greater than 50 and
  • 00:53:48 I could also do NP dot all and as you
  • 00:53:54 see there's less throughs in this case I
  • 00:53:56 think the only time that all of the
  • 00:53:58 values are greater than 50 are right
  • 00:53:59 here yeah you see there's one true in
  • 00:54:02 the fifth spot which corresponds to this
  • 00:54:05 right here what else can we do with this
  • 00:54:12 if you did axis equals one just going to
  • 00:54:17 get rows you can also do multiple
  • 00:54:21 conditions so I could do like I want
  • 00:54:24 file data to be greater than 50 and
  • 00:54:30 let's say file data is less than 100 and
  • 00:54:34 this syntax is very similar to pandas
  • 00:54:36 here and as I said before numpy builds
  • 00:54:39 is what's the base of pandas so it makes
  • 00:54:41 sense no the final truth value of an
  • 00:54:47 array with more than one element is
  • 00:54:50 ambiguous how do I do this I think if I
  • 00:54:53 do something like this it will work
  • 00:55:01 no positive let's see
  • 00:55:06 No I need to end that yeah cool so this
  • 00:55:10 is all the values that are greater than
  • 00:55:13 50 but less than 100 and so like the
  • 00:55:15 first shoe should happen at the six spot
  • 00:55:17 one two three four five six as you see
  • 00:55:22 and I could do some of them like all the
  • 00:55:26 spots if I wanted to make all this not
  • 00:55:30 so this means not greater than fifty and
  • 00:55:34 less than one hundred this is going to
  • 00:55:36 be the reverse of what we just did so
  • 00:55:39 yeah now the sixth spot is the first
  • 00:55:44 false so this meant not so yeah all
  • 00:55:48 sorts of cool stuff you can do with this
  • 00:55:50 boolean masking an advanced indexing I
  • 00:55:53 mean yeah any sort of like condition
  • 00:55:56 I'll put a link in some more information
  • 00:55:58 about this all right quick little quiz
  • 00:56:00 on indexing this is kind of using all
  • 00:56:03 sorts of advanced stuff that you just
  • 00:56:05 learned in that last section include and
  • 00:56:07 then also like some that original stuff
  • 00:56:09 so first question basically pause the
  • 00:56:12 video after I ask it and then try to
  • 00:56:14 figure out what the command would be so
  • 00:56:16 we have this matrix and how would you
  • 00:56:18 index this part of the matrix so this is
  • 00:56:26 the second and third row and the first
  • 00:56:32 and second column or 0th and first
  • 00:56:35 column so it looks something like this
  • 00:56:38 rows columns next question how would you
  • 00:56:42 Index this this is something we haven't
  • 00:56:44 done before but you potentially with
  • 00:56:47 that last section might have an idea if
  • 00:56:49 not no worries so to do this one you
  • 00:56:57 need to use two different lists within
  • 00:57:01 your indexing so it's going to look
  • 00:57:04 something like this
  • 00:57:06 we need the zeroth first second and
  • 00:57:09 third row and then
  • 00:57:12 to second third and fourth columns
  • 00:57:13 that's what that is
  • 00:57:15 and then final question how would you
  • 00:57:18 Index this this is like also if
  • 00:57:22 something we haven't immediately looked
  • 00:57:23 at but you might be able to get
  • 00:57:25 especially with that last one
  • 00:57:28 take a second all right that would look
  • 00:57:35 something like this where you get the
  • 00:57:39 0th fourth and fifth row zero fourth and
  • 00:57:43 fifth rows and then you want columns
  • 00:57:46 three onwards so this would like 300
  • 00:57:50 works you can also do like three to five
  • 00:57:52 you'd also do three like a list of three
  • 00:57:55 four but yeah that's one way to do it
  • 00:57:57 it's a fun little quiz I know it's I
  • 00:57:59 guess good to revisit this type of thing
  • 00:58:01 and like think critically about it all
  • 00:58:03 right thank you guys very much for
  • 00:58:05 watching I think this is all I have for
  • 00:58:06 this video if you've learned anything
  • 00:58:09 make sure if those video a thumbs up and
  • 00:58:12 also subscribe because I'm gonna be
  • 00:58:14 making some like very useful tutorials I
  • 00:58:16 hope at least that they're useful as
  • 00:58:19 well as I'm going to be demonstrating
  • 00:58:20 some cool projects that I'm working on
  • 00:58:22 so I'd be awesome if you subscribe
  • 00:58:23 that's all we have today Pisa
  • 00:58:28 [Music]
  • 00:58:39 you