Coding

Python NumPy Tutorial for Beginners

  • 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 of than
  • 00:00:25 the numpy library so it's super
  • 00:00:27 important and kind of because it's
  • 00:00:29 important it's because it's this like
  • 00:00:31 base the way we're going to do this
  • 00:00:34 video is I'm going to start off with
  • 00:00:35 kind of a background information on how
  • 00:00:38 a numpy works and I think really having
  • 00:00:40 that 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:57 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
  • 00:01:14 to begin numpy is a multi-dimensional
  • 00:01:16 array library so what that means is you
  • 00:01:19 can use the UM PI to store all sorts of
  • 00:01:21 data and one-dimensional arrays
  • 00:01:23 two-dimensional arrays three dimensional
  • 00:01:25 arrays four dimensional arrays etc and
  • 00:01:28 so the common question you kind of ask
  • 00:01:30 or I'm commonly asked when you know you
  • 00:01:34 first bring up numpy is why do you use
  • 00:01:35 numpy over lists so the main difference
  • 00:01:40 comes from the speed so lists they are
  • 00:01:43 very slow meanwhile numpy is very fast
  • 00:01:49 and so why are lists slow and numpy
  • 00:01:52 passed well one reason is because numpy
  • 00:01:55 uses fixed types so what that means is
  • 00:01:59 imagine we have this three by four
  • 00:02:01 matrix three rows four columns and it's
  • 00:02:06 all integer values and we're going to
  • 00:02:08 kind of look at how the
  • 00:02:09 your values differ between numpy and
  • 00:02:12 lists so let's just zoom in on that five
  • 00:02:15 that's there in 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
  • 00:02:29 eight 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 four bytes and so it
  • 00:02:52 represents five in a total memory space
  • 00:02:55 of four bytes
  • 00:02:56 today in 32 and you also you can even
  • 00:02:59 specify so by default it's in 32 but you
  • 00:03:01 could even specify that you didn't need
  • 00:03:03 all four bytes I represent this value so
  • 00:03:06 you could specify within numpy that you
  • 00:03:09 want to maybe in in 16 which is 16 bits
  • 00:03:11 or 2 bytes or even if you get really
  • 00:03:14 small values into 8 which is just a
  • 00:03:16 single byte on the other hand with lists
  • 00:03:22 there's a lot more information you need
  • 00:03:24 a store as an integer so in in lists
  • 00:03:27 lists use of built-in int type for
  • 00:03:30 Python and so that built in int type
  • 00:03:32 consists of 4 different things it
  • 00:03:34 consists of the object value which you
  • 00:03:38 know has its own bits associated with it
  • 00:03:41 object type the reference count how many
  • 00:03:44 times that that integer has been
  • 00:03:46 specifically like pointed at and the
  • 00:03:49 size of that integer value and so if we
  • 00:03:53 break this up into the actual binary
  • 00:03:55 that it represents we can take the
  • 00:03:57 object value and that's represented as a
  • 00:04:01 long which is like 8 bytes the object
  • 00:04:03 type same deal a reference count same
  • 00:04:06 deal and then the size I believe is a
  • 00:04:07 little bit smaller I think it's only 4
  • 00:04:10 bytes but as you can see that's a single
  • 00:04:12 integer within the lists using the built
  • 00:04:14 in into
  • 00:04:15 hi it requires a lot more space than
  • 00:04:18 numpy so basically the takeaway from
  • 00:04:20 this is that because numpy uses less
  • 00:04:26 bytes of memory the computer can read
  • 00:04:29 less bytes of memory quicker obviously
  • 00:04:31 so it's faster in that regard another
  • 00:04:35 reason that I didn't specifically say is
  • 00:04:37 that when we're iterating through each
  • 00:04:40 item in a numpy array we don't have to
  • 00:04:44 do type checking each time so in Python
  • 00:04:46 built-in lists you could have a list of
  • 00:04:48 like an integer then a flow 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:14 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:28 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:53 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:02 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:36 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:59 so the first benefit is that our CPUs or
  • 00:07:03 our computers have these Cindi vector
  • 00:07:08 processing units and so when this memory
  • 00:07:11 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:23 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 these values at one
  • 00:07:33 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 like
  • 00:07:52 perform all sorts of operations while as
  • 00:07:54 in the list case you may be 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:10 slower because you'd have to do more
  • 00:08:11 like longer memory lookups within your
  • 00:08:14 computer ok so we've kind of gone over
  • 00:08:17 some of the performance benefits but how
  • 00:08:19 are lists different from numpy well
  • 00:08:21 lists we can do insertion deletion
  • 00:08:23 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 we try to
  • 00:08:48 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:19 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 able to find
  • 00:09:44 even more but yeah it's pretty powerful
  • 00:09:46 the meth 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 that video I use numpy to store
  • 00:10:10 the board and then in future videos that
  • 00:10:13 I'm going to do you can you can actually
  • 00:10:15 store images through num pipes like PNG
  • 00:10:19 images you can use numpy to store all
  • 00:10:23 the information and like do all sorts of
  • 00:10:25 cool stuff that all push future videos
  • 00:10:27 on let's see also another I think useful
  • 00:10:32 reason to know numpy is 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 so
  • 00:11:01 knowing numpy will help you kind of be
  • 00:11:06 able to do some stuff with machine
  • 00:11:07 learning
  • 00:11:08 all right get started the code the first
  • 00:11:10 thing you're gonna want to do is import
  • 00:11:11 the numpy library and just so we're on
  • 00:11:14 the same page I'm just a Jupiter
  • 00:11:15 notebook to use to code this up but you
  • 00:11:17 can use whatever editor you prefer also
  • 00:11:20 all this code that I'll be going through
  • 00:11:22 will be on my github and the link to
  • 00:11:23 that will be in the description okay so
  • 00:11:27 import numpy as NP if that works for you
  • 00:11:29 great if it didn't work you'll have to
  • 00:11:31 do a pip install so you can go ahead
  • 00:11:34 into your terminal and type in pip
  • 00:11:37 install a numpy and so it's already
  • 00:11:47 installed for me so and if pip doesn't
  • 00:11:51 work for you try pip 3 install numpy
  • 00:11:53 that should work so the first thing
  • 00:11:55 that's important to know is how to
  • 00:11:56 initialize an array so we'll just say
  • 00:11:58 that a equals NP dot 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 and print hey okay cool so we
  • 00:12:19 can also initialize a little bit more
  • 00:12:21 complex arrays so we could do like a 2d
  • 00:12:24 array of floats and I could do that
  • 00:12:26 following way we have a list within a
  • 00:12:29 list
  • 00:12:34 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:53 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:13:00 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:36 dimension because it only house 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 that 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:39 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 fights as it does if we left this as an
  • 00:14:50 inch 32 it will tell me four bites 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 betta item
  • 00:15:34 size these are floats and I believe that
  • 00:15:37 this is an 8 byte type so if I do B dot
  • 00:15:39 item size as you see it's 8
  • 00:15:41 so floats are gonna be bigger than
  • 00:15:45 floats are bigger than integers usually
  • 00:15:49 unless you define this as like int 64
  • 00:15:52 and so yeah you really I usually don't
  • 00:15:55 even worry about the datatype too much I
  • 00:15:56 don't specify it but if you really want
  • 00:15:58 to be efficient try to specify this so
  • 00:16:01 that it fits all your data but if yeah I
  • 00:16:06 guess it fits all your data as tightly
  • 00:16:08 as possible all right so now that we've
  • 00:16:09 gone through some of the basics let's
  • 00:16:11 actually show how we access slush change
  • 00:16:13 specific elements rows columns etc so
  • 00:16:16 imagine we have the array there's going
  • 00:16:21 to be a two-dimensional array so I'm
  • 00:16:25 gonna make this kind of long and you'll
  • 00:16:26 see why in 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 start at the 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:21 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:31 the negative notation similar to lists
  • 00:17:34 so I could also say the negative 2nd
  • 00:17:36 element would be 13 as well because this
  • 00:17:39 would be negative 1 and then negative 2
  • 00:17:41 so there's a couple different ways to do
  • 00:17:43 this but we'll stick with the first one
  • 00:17:45 okay let's say we wanted to get a
  • 00:17:47 specific I can't spell row that's pretty
  • 00:17:56 straightforward as well so in this case
  • 00:17:58 if we wanted the first row we would do 0
  • 00:18:00 and then because we want all columns we
  • 00:18:03 use the basic slave syntax similar to
  • 00:18:05 lists I can just do a single column and
  • 00:18:07 that will get me everything in the road
  • 00:18:09 that's nice what if we want to eat that
  • 00:18:12 specific column well if you know how to
  • 00:18:15 do rows you probably know how to do
  • 00:18:16 columns a let's say we wanted this row
  • 00:18:20 or this column right here 3 & 10 that
  • 00:18:23 would be all the rows and then 0 1 2
  • 00:18:27 column that gives me the 3 10 and from
  • 00:18:31 here we can do even some more like
  • 00:18:33 tricky stuff so we're just say getting a
  • 00:18:40 little more fancy
  • 00:18:42 and we have the start index this is just
  • 00:18:47 a reminder start index and index and
  • 00:18:50 then finally the step size so if I
  • 00:18:55 wanted to let's say get between the
  • 00:18:58 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:16 should be 1 so I start at the two 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 yeah what
  • 00:19:42 happened there Oh see that was going to
  • 00:19:44 come backwards I didn't want to change
  • 00:19:45 it there I wanted to change the 6 to be
  • 00:19:48 negative 2 okay its exclusive so when
  • 00:19:52 this to actually be negative 1 a little
  • 00:19:55 bit more of a fancy way to do that okay
  • 00:19:57 so that's how you access elements and
  • 00:19:59 then if we wanted to change something
  • 00:20:01 it's pretty straightforward to it say I
  • 00:20:03 wanted to change that 13 that I
  • 00:20:05 originally accessed well I can just do
  • 00:20:09 like twice if I print out a now that
  • 00:20:14 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:50 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:21:00 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:26 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 yeah the
  • 00:21:59 second element but the first index like
  • 00:22:02 that so that gives me the 4 and you can
  • 00:22:04 do similar type stuff with like the
  • 00:22:07 colons in here so each one of these
  • 00:22:09 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 and you can
  • 00:22:21 kind of play around with this and see
  • 00:22:23 how changing different things changes
  • 00:22:26 what you get and if you wanted to
  • 00:22:28 replace in this case basically just have
  • 00:22:33 to create a subsequence that's the same
  • 00:22:35 dimension so if I did b1 this it gives
  • 00:22:41 me 3 4 7 8 let's say I wanted to change
  • 00:22:43 that to 9 9 8 8 as long as it's the same
  • 00:22:52 dimension it's gonna work so 9 9 8 8 if
  • 00:22:56 I try to like do something like 9 9
  • 00:22:57 1988 it's gonna have an area alright so
  • 00:23:01 that's the basics of indexing I think it
  • 00:23:04 by the it at the end of the video I'll
  • 00:23:05 do a little like challenge problem on
  • 00:23:09 like some advanced indexing so look at
  • 00:23:11 the end of the video for that alright
  • 00:23:13 next let's go through how to initialize
  • 00:23:15 all sorts of different types of arrays
  • 00:23:16 so to start off let's initialize in all
  • 00:23:20 zeros matrix and to do that there's a
  • 00:23:25 nice built-in function called NP zeros
  • 00:23:28 and we can first I guess actually all
  • 00:23:32 you really need to do is specify a shape
  • 00:23:34 so I did like MP zeros 5 it's gonna just
  • 00:23:37 give me a vector of 5 like 5 but I also
  • 00:23:40 can pass in a more complex shape so if I
  • 00:23:44 wanted it to be like a two by two or two
  • 00:23:47 by three let's say as you see there I
  • 00:23:49 could do three dimensional two by three
  • 00:23:51 by three could even do four dimensional
  • 00:23:54 if I want it to 2 by 3 by 3 by 2 yeah it
  • 00:23:58 gets pretty crazy but yeah you can do
  • 00:24:00 all sorts of zeros with that next let's
  • 00:24:07 do in all ones matrix pretty similar to
  • 00:24:10 the last one and P dot ones of let's say
  • 00:24:15 4 by 2 by 2 and there you go and you're
  • 00:24:21 getting also specify the datatype here
  • 00:24:24 so if you wanted like all ones but in 32
  • 00:24:28 you can go ahead and do that so all ones
  • 00:24:32 all zeros however you might want to
  • 00:24:35 initialize some matrix that's not ones
  • 00:24:39 or zeros and the other number so for
  • 00:24:42 that you can do NP full and this one
  • 00:24:46 takes in two parameters so the first is
  • 00:24:48 the shape so 2 by 2 and then the next is
  • 00:24:52 the value so if I wanted at all 99's
  • 00:24:56 then it's a 2 by 2 with 99 another
  • 00:25:00 useful and you can you know that has a
  • 00:25:04 data type too so
  • 00:25:05 that to be float32 there you go and I'll
  • 00:25:14 put a link in the description to a list
  • 00:25:16 full of these like array creation
  • 00:25:18 routines useful to know is there's also
  • 00:25:22 this full like there's this full like
  • 00:25:30 method and basically that just allows us
  • 00:25:33 to take a shape that's already built so
  • 00:25:36 let's imagine we wanted to reuse that
  • 00:25:40 this array that we I guess had in the
  • 00:25:44 last section
  • 00:25:45 a I think it's still loaded and they
  • 00:25:48 would just make sure well I can pass in
  • 00:25:51 and make a array that's the same shy
  • 00:25:54 size of fours let's say my doing full
  • 00:26:00 like they're actually I think I don't
  • 00:26:04 even have to pass in a doubt shape I
  • 00:26:08 just have the pass in hey there you go
  • 00:26:11 if I didn't use full like I would have
  • 00:26:13 the new full of a dot shape I don't know
  • 00:26:17 if that's that useful for you but I
  • 00:26:18 guess it's potentially good to know ok
  • 00:26:26 next one let's say we wanted to
  • 00:26:28 initialize a array or a matrix of random
  • 00:26:35 numbers so random decimal numbers to
  • 00:26:39 start so do that we do NP random 3 and
  • 00:26:45 and we specify the shape so let's say 4
  • 00:26:48 by 2 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:20 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 I could also pass in something like
  • 00:27:37 a touch 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 that 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:08 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:32 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:02 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 maybe do a Google search
  • 00:29:10 how to get it but yeah Rihanna Brandon
  • 00:29:13 0-7 with size 3 by 3 is here
  • 00:29:17 you could also specify like a different
  • 00:29:19 parameter so that's I went 4 to 7 and I
  • 00:29:23 think and if I keep running this too
  • 00:29:25 it's kind of cool you can see it
  • 00:29:26 changing and so it looks like that 7 is
  • 00:29:28 exclusive so if I wanted it include 7 I
  • 00:29:31 would stop a little bit later you could
  • 00:29:33 also throw in like negative numbers here
  • 00:29:37 cool
  • 00:29:38 all right what else other than random
  • 00:29:42 integers maybe you wanted to do like the
  • 00:29:43 identity matrix we do identity 3 this
  • 00:29:48 one 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:33 happens okay and then if I specify the
  • 00:30:37 axis equals zero
  • 00:30:39 oh no did do anything what I can do is
  • 00:30:42 make this a two dimensional array I
  • 00:30:44 think because it was a vector it didn't
  • 00:30:46 do what I wanted to what I wanted to do
  • 00:30:48 is one two three or one two three one
  • 00:30:52 two three one two three so if I wanted
  • 00:30:56 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 they'll be
  • 00:31:05 basically making it rose
  • 00:31:07 there you go so if I made this equal the
  • 00:31:11 one that's 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:39 using everything that we kind of just
  • 00:31:40 went through so all these different
  • 00:31:43 methods so look at this picture and then
  • 00:31:46 try to put it together without just
  • 00:31:49 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 once so once and it's gonna
  • 00:32:13 be a 5×5 of ones print output so this is
  • 00:32:21 what I have now
  • 00:32:22 okay and now basically what we're gonna
  • 00:32:24 do is fill in this middle part with
  • 00:32:26 zeros so Z I want to just say equals NP
  • 00:32:30 dot zeros and that's going to be a 3×3
  • 00:32:33 and if I print Z now we have this now
  • 00:32:40 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 I'll
  • 00:32:55 put the middle part so that's gonna be
  • 00:32:58 the first row to the third row so I want
  • 00:33:04 the first row to the third row and that
  • 00:33:08 I want the same thing with columns
  • 00:33:10 because it's the middle first column to
  • 00:33:12 the third column and actually this is
  • 00:33:15 exclusive value so it needs to go to
  • 00:33:17 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 could
  • 00:33:35 also do negative one so basically the
  • 00:33:37 from the first element to the last
  • 00:33:39 government yeah and as you see it didn't
  • 00:33:43 change this last initialization I want
  • 00:33:45 to
  • 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 got to be
  • 00:33:50 really careful about so I'm just gonna
  • 00:33:52 quickly mention it I won't do
  • 00:33:59 explanation ones there you go okay so
  • 00:34:02 imagine we have two arrays or we have
  • 00:34:07 one array let's call it a and so you
  • 00:34:12 know a is just a normal array as you can
  • 00:34:15 see and let's say we want to make be a
  • 00:34:17 direct copy of a so now I'm going to
  • 00:34:20 just do B equals a and then print out B
  • 00:34:25 and as you can see it's still 1 2 3 and
  • 00:34:28 so I'm like okay I have this copy like
  • 00:34:31 things are cool it's fine
  • 00:34:32 I want to change the first element in B
  • 00:34:35 so I'm going to do B 0 equals 100 here's
  • 00:34:40 the issue I print out B looks good the
  • 00:34:45 issue is in if I print out a look what
  • 00:34:48 happens I just printed out a and a now
  • 00:34:52 has a 100 instead of the 1 2 3 that I
  • 00:34:56 initially said it as and that's because
  • 00:34:59 when we did B equals a we just said that
  • 00:35:01 the the variable named B points the same
  • 00:35:06 thing as a does we didn't tell like
  • 00:35:08 numpy to make a copy of what is the
  • 00:35:12 contents of a so that's the that's why
  • 00:35:15 the because we're just pointing at the
  • 00:35:17 same exact thing that a is pointing when
  • 00:35:19 we change the value it also changes the
  • 00:35:21 value of a so if we want to prevent that
  • 00:35:23 we can use this dot copy function oh
  • 00:35:26 sorry I shouldn't do it yet B equals a
  • 00:35:29 dot copy and then when we run the sale
  • 00:35:32 as you can see 1 2 3 still there because
  • 00:35:36 now we're just copying the contents of
  • 00:35:38 what's in a and if I print B it has the
  • 00:35:43 100 200 100 2 3 okay so one of the big
  • 00:35:46 uses of numpy is all the math
  • 00:35:48 capabilities it offers so there's two
  • 00:35:52 kind of show
  • 00:35:52 some of that one thing that it can do is
  • 00:35:55 element-wise wants to make those four
  • 00:35:59 values element-wise addition
  • 00:36:01 subtractions element-wise I guess
  • 00:36:03 arithmetic so here we have a printout a
  • 00:36:09 and if I wanted to do something like a
  • 00:36:12 plus two adds to each element you can do
  • 00:36:16 a minus to subtract two from each
  • 00:36:19 element a times two as you can see a
  • 00:36:24 divided by 2 divides everything by 2
  • 00:36:31 one thing to note with and you can also
  • 00:36:33 do stuff like a plus equals two so now
  • 00:36:37 have I printed out a in this column it's
  • 00:36:41 going to be two plus everything it's
  • 00:36:46 kind of cool you can do like the same
  • 00:36:47 type of math that you can do in Python
  • 00:36:51 you can also create another array and
  • 00:36:54 Peter array and that's like let's say 1
  • 00:36:59 0 1 0 I could do something like a plus B
  • 00:37:06 and that should be 2 2 4 4 oh and
  • 00:37:11 because I added I rerun this okay to 244
  • 00:37:15 like we expect so all sorts of useful
  • 00:37:17 things you could even do like a to the
  • 00:37:20 second power 1 4 9 16 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 sign of all the values so let's say we
  • 00:37:37 had a we do MP dot sign pass in 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 as I
  • 00:38:05 mentioned
  • 00:38:06 or I have this on my github so if you
  • 00:38:08 look in the description you can find
  • 00:38:09 this exact notebook say yeah look up
  • 00:38:13 their routines right here
  • 00:38:15 for math all sorts of cool stuff alright
  • 00:38:20 moving on we're gonna still be in math
  • 00:38:24 but let's jump into linear algebra type
  • 00:38:27 stuff so here we are doing a linear
  • 00:38:30 algebra okay so this was kind of like
  • 00:38:32 the basic all sorts of function G you do
  • 00:38:35 on elements linear algebra so this is
  • 00:38:38 like really I feel like when I'm using
  • 00:38:39 MATLAB it would be doing these linear
  • 00:38:42 algebra type stuff so let's say we have
  • 00:38:44 two matrices and the big difference with
  • 00:38:47 linear algebra is like we're not doing
  • 00:38:50 element wise so like in this case this B
  • 00:38:54 we're doing element wise computation so
  • 00:38:57 like eight times be in you know linear
  • 00:39:03 algebra are trying to multiply matrices
  • 00:39:05 and that's a different process so let's
  • 00:39:07 say we have two matrices will have a and
  • 00:39:12 I'm going to use this syntax we learned
  • 00:39:14 about earlier I want to say this is a
  • 00:39:16 two by three matrix of all twos actually
  • 00:39:22 let's make this two by three matrix of
  • 00:39:25 ones so we have a as you can see and
  • 00:39:30 then we'll have B which is equal to NP
  • 00:39:36 dot full it's going to be a three by two
  • 00:39:39 and it's going to be a value two so if I
  • 00:39:43 print 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 can walk through the whole thing but we
  • 00:40:08 should end up with a two by two matrix
  • 00:40:12 at 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 pass in
  • 00:40:29 B we get six six six six
  • 00:40:34 did I say enough sixes I don't know but
  • 00:40:37 yeah did multiply those two matrices you
  • 00:40:42 know and if I try to switch up this
  • 00:40:43 dimension in the middle it's not going
  • 00:40:46 to work because it's now incompatible
  • 00:40:50 that's matrix multiplication you could
  • 00:40:53 also want to do maybe some other stuff
  • 00:40:54 with matrices so let's imagine I wanted
  • 00:40:57 to create the herb to find the let's say
  • 00:41:03 determinant of a matrix so we could as a
  • 00:41:06 sanity check you know makes C equal the
  • 00:41:09 identity matrix and if you are familiar
  • 00:41:12 with linear algebra you know that the
  • 00:41:14 identity matrix has a determinant of one
  • 00:41:18 so if I do a linear algebra determinant
  • 00:41:21 of C we should get one one point now as
  • 00:41:25 we get so find determinant you know and
  • 00:41:32 there's all sorts of other good things
  • 00:41:33 like eigen values you know the inverse
  • 00:41:37 of a matrix so what what do you multiply
  • 00:41:41 by a matrix to get the identity matrix
  • 00:41:44 and so yeah all sorts of good stuff on
  • 00:41:47 that like I guess I'll do em and if you
  • 00:41:53 want 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 6 you could also
  • 00:42:59 do it on like a row basis so if I sit
  • 00:43:02 actually C 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 going to give me all
  • 00:43:16 the values that are up top here because
  • 00:43:17 those are all the the mins so yeah you
  • 00:43:21 do all sorts of cool stuff with min to
  • 00:43:23 the max with this same thing with max
  • 00:43:28 let's say x equals 0 x equals 1 3 & 6 is
  • 00:43:35 the biggest value 3 is the biggest value
  • 00:43:37 and the 6 is the biggest value you can
  • 00:43:39 also do in feed out some of stats if I
  • 00:43:42 do it just as is it's gonna sum up all
  • 00:43:45 of the elements in the matrix and then
  • 00:43:48 same thing I can do row or column so I
  • 00:43:52 actually equals 0 is going to add up all
  • 00:43:55 these terms going downwards next let's
  • 00:43:58 talk a little bit about reorganizing
  • 00:43:59 arrays so kind of the I would say the
  • 00:44:02 key method within reorganizing arrays so
  • 00:44:04 if I have the array I'm going to call it
  • 00:44:06 before and let's say that that is equal
  • 00:44:11 to this value right here so we have it
  • 00:44:16 before I'll print 4 out looks like that
  • 00:44:21 so let's say we wanted to instead of
  • 00:44:23 this shape that it currently
  • 00:44:26 is 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 that has the
  • 00:45:17 same amount of values like it's fair
  • 00:45:18 game so as you see two by two by two
  • 00:45:22 still works with the reshape what
  • 00:45:25 doesn't work is like if I wanted it to
  • 00:45:26 be two by three the values don't fit in
  • 00:45:30 so when you get errors but using that of
  • 00:45:31 shape it's usually because there's a
  • 00:45:33 mismatch between the shape you're trying
  • 00:45:35 to resize it to versus the original
  • 00:45:37 shape moving onwards let's look at
  • 00:45:41 vertical stacks so vertically stacking
  • 00:45:46 vectors or matrices and you know
  • 00:45:50 dimensions are important in vertical
  • 00:45:52 stack as well so vertical stacking of
  • 00:45:55 matrices let's say we had these two
  • 00:45:58 arrays if I wanted to stack you know one
  • 00:46:04 two three four on top of five six seven
  • 00:46:06 eight
  • 00:46:07 I can do NP dot V stack and I can pass
  • 00:46:10 in V 1 V 2 and as you see now they're
  • 00:46:15 part of the same matrix and one two
  • 00:46:19 three four is on top of five six seven
  • 00:46:21 eight what I can even do is keep passing
  • 00:46:23 these in so let's say I wanted like
  • 00:46:25 three copies of this five six seven
  • 00:46:28 eight and only one copy of it
  • 00:46:29 or I could enter tweet we've them that's
  • 00:46:32 a vertical stack horizontal stacks are
  • 00:46:36 pretty similar and also note here like I
  • 00:46:39 can't do that
  • 00:46:40 the sizes from this mass mitch miss
  • 00:46:42 match so yep horizontal stack very
  • 00:46:46 similar let's see we had well use a some
  • 00:46:54 notation we've learning before we had
  • 00:46:56 these two matrices so if I printed out
  • 00:46:59 each one you know like that and then
  • 00:47:02 each two is this well I want each to do
  • 00:47:05 the on the back of each one I can just
  • 00:47:07 do an MP dot H stack
  • 00:47:09 horizontal stack and that it will be H 1
  • 00:47:12 and H 2 and that did not work because it
  • 00:47:18 did not surround this in parenthesis
  • 00:47:20 either parenthesis or brackets I think
  • 00:47:23 they both work yeah there you go so now
  • 00:47:25 we've horizontally stacked to the zeros
  • 00:47:28 on top of the earth to the right of the
  • 00:47:30 ones alright let's get into some
  • 00:47:31 miscellaneous things so first off
  • 00:47:34 imagine you have you know some sort of
  • 00:47:37 text file with all sorts of data and for
  • 00:47:40 whatever reason you choose you don't
  • 00:47:41 want to use pandas but you want to load
  • 00:47:44 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 Jen from text and I pass
  • 00:48:18 in the name of the file which is
  • 00:48:20 data.txt and then I pass in a delimiter
  • 00:48:23 which is the separator and that's a
  • 00:48:26 comma and if I do that I see that I get
  • 00:48:29 that data that I just showed you I get
  • 00:48:35 that increase
  • 00:48:38 zoom here I get that as an array so
  • 00:48:44 that's pretty nice so I'll just call
  • 00:48:47 this file data equals and file data yeah
  • 00:48:55 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 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 and the in 32
  • 00:49:32 type are different sizes they can't just
  • 00:49:34 like 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 into
  • 00:49:47 32 and then printed out file data as you
  • 00:49:51 can see now it's all floats
  • 00:49:53 so let's how you load data from a file
  • 00:49:54 and you could change up this delimiter
  • 00:49:57 based on how your data is split but I
  • 00:49:59 think that this gem from text will
  • 00:50:01 handle your new line breaks properly if
  • 00:50:04 that's how its formatted right in the
  • 00:50:07 comments if you have any questions about
  • 00:50:08 this okay the second thing I want to go
  • 00:50:11 through is what happened there I didn't
  • 00:50:15 want that to be markdown the second
  • 00:50:20 thing I want to go through with this
  • 00:50:21 miscellaneous section is some advanced
  • 00:50:26 indexing so there's some really cool
  • 00:50:28 stuff you can do with numpy I'm gonna
  • 00:50:31 say boolean masking and advanced
  • 00:50:38 indexing so what can we do here so let's
  • 00:50:42 say I wanted to learn where in file data
  • 00:50:47 the value is 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 a 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:03 and then at true if we go to our data
  • 00:51:04 for falses and then 196 is in fact
  • 00:51:07 greater than 50 so that's like one way
  • 00:51:11 and you can do all sorts of cool stuff
  • 00:51:12 with us like you did greater than equal
  • 00:51:15 to you know all sorts of different
  • 00:51:17 combinations one thing that's pretty
  • 00:51:19 neat is you can do file data and then
  • 00:51:22 you can index based on where it is
  • 00:51:26 greater than 50 and by doing this you
  • 00:51:30 grab only the values that actually have
  • 00:51:34 a value of greater than 50 so that is
  • 00:51:37 pretty cool and kind of the reason that
  • 00:51:39 this right here works is that one thing
  • 00:51:42 I did not mention until now is that you
  • 00:51:47 can can index with a list and numpy
  • 00:51:52 which is pretty cool so if you have the
  • 00:51:54 array one two three four five six seven
  • 00:51:59 eight nine and I wanted that say zeroth
  • 00:52:03 spot the second spot and then the last
  • 00:52:07 spot I could do MP or let's say that
  • 00:52:13 this is a I could do a of zero one and
  • 00:52:23 or I wanted two three and nine so I
  • 00:52:25 would do one two and then eight as you
  • 00:52:30 see that gives me two three and nine I
  • 00:52:32 passed in a list and it indexed those
  • 00:52:35 spots so basically it also works if you
  • 00:52:38 like had choosen falses it like
  • 00:52:40 basically if it is true then it knows to
  • 00:52:43 take it if it's false doesn't so that's
  • 00:52:45 why this up here 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:20 we looked downwards on all of these or
  • 00:53:22 any of the values greater than 50
  • 00:53:25 let's see what happens so false false
  • 00:53:27 false 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
  • 00:54:42 no the file truth value of an array with
  • 00:54:48 more than one element is ambiguous how
  • 00:54:52 do I do this I think if I do something
  • 00:54:54 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 true should happen at the sixth
  • 00:55:17 spot
  • 00:55:18 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 50 and
  • 00:55:34 less than 100 this is going to be the
  • 00:55:36 reverse of what we just did so yeah now
  • 00:55:39 the sixth spot is the first false so
  • 00:55:46 this meant not so yeah all sorts of cool
  • 00:55:49 stuff you can do with this boolean
  • 00:55:50 masking an advanced indexing I mean yeah
  • 00:55:55 any sort of like condition I'll put a
  • 00:55:57 link in some more information about this
  • 00:55:59 all right quick little quiz on indexing
  • 00:56:02 this is kind of using all sorts of and
  • 00:56:04 stuff that you just learned in that last
  • 00:56:06 section include and then also like some
  • 00:56:08 that original stuff so first question
  • 00:56:10 basically pause the video after I ask it
  • 00:56:13 and then try to figure out what the
  • 00:56:15 command would be so we have this matrix
  • 00:56:17 and how would you index this part of the
  • 00:56:20 matrix so this is the second and third
  • 00:56:30 row and the first and second column or
  • 00:56:33 zeroth and first column so it looks
  • 00:56:36 something like this rows columns next
  • 00:56:41 question how would you Index this this
  • 00:56:44 is something we haven't done before but
  • 00:56:46 you potentially with that last section
  • 00:56:48 might have an idea if not no worries so
  • 00:56:54 to do this one you need to use two
  • 00:56:59 different lists within your indexing so
  • 00:57:04 it's going to look something like this
  • 00:57:06 we need the zeroth first second and
  • 00:57:09 third row and then
  • 00:57:12 second third and fourth columns that's
  • 00:57:14 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 hope they 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 zeroth fourth and fifth row zero fourth
  • 00:57:42 and fifth rows and then you want columns
  • 00:57:46 three onwards so this would like 300
  • 00:57:50 works you could also do like three to
  • 00:57:52 five you'd also do three like a list of
  • 00:57:55 three four but yeah that's one way to do
  • 00:57:57 it it's a fun little quiz I don't know
  • 00:57:59 it's I guess good to revisit this type
  • 00:58:01 of thing and like think critically about
  • 00:58:02 it alright thank you guys very much for
  • 00:58:05 watching I think this is all I have for
  • 00:58:07 this video
  • 00:58:07 Pizza