Full code walkthrough and tutorial on how to use numpy.

If you liked the video – please hit the like button. It means more than you know. Thanks for watching!!

Always looking for suggestions on what video to make next — leave me a comment with your project! Happy Coding!

source

00:00 [Music]

00:09 hello and welcome to my channel today I

00:12 want to show you how we can use numpy a

00:14 scientific computing package in Python

00:16 we can do a lot of different things in

00:18 this video will serve as a quick

00:19 overview of all the different use cases

00:21 for numpy so let's jump right in

00:23 open up your text editor of choice and

00:25 then open up a terminal or command

00:27 problem depending on your operating

00:28 system I'm on Mac so I'll open up the

00:30 terminal and type in pip 30 install

00:33 numpy execute this if you don't already

00:36 have a numpy I already have it so I'm

00:38 going to cancel this command create a

00:39 new file for this example I'll name mine

00:42 MP example dot pi the first thing we

00:45 need to do is to import numpy so type in

00:48 import numpy and then as MP from here

00:51 we're going to dive right into why numpy

00:53 is so good

00:54 numpy is so good because it decreases

00:55 the operational time that you need

00:57 whenever dealing with large amounts of

00:59 data it does this by using a raise

01:01 let's look at a few sample arrays

01:03 looking at these two arrays we have one

01:05 array that is just numbers we can define

01:07 an array by using the function MP array

01:10 the second array has a few items denoted

01:13 by quotations as strings let's print

01:15 these out and see what they look like in

01:17 the console we can do this by typing in

01:19 print and then the names of the

01:20 variables so print numbers and then

01:22 print strings go ahead and save your

01:24 file and open up a new terminal execute

01:27 your script my typing in Python 3 and

01:28 then the name of your script so mono is

01:31 empty example dot pi and then hit

01:32 execute we can see from the output in

01:34 the terminal ray is a set of numbers the

01:37 second array however changes everything

01:38 to string since we have a few strings in

01:41 our array num PI does this automatically

01:43 because it wants to change all standard

01:44 array values to the same data type

01:46 this makes the operations happen much

01:48 quicker every array within the num pod

01:50 doesn't have to be the same data type

01:52 though we can use a thing called a

01:54 structured array to have different data

01:55 types appear in the same agree pasting

01:57 in an example we can see a structured

01:59 array is called the same way as a

02:00 standard array and this one we can see

02:02 that we have a string an integer and

02:04 then a float number when using a

02:06 structure to write we have to define

02:07 these values so for this example we have

02:10 a string and integer and a float we can

02:12 see he

02:13 you 16 stands for a string integer and

02:16 then a float number if you want to

02:18 quickly see all the different data types

02:19 that may be important to you I'll post

02:21 those in the description below let's

02:23 look at what we get in a terminal

02:24 whenever we print our structured array

02:27 save your file and read execute your

02:29 script we can see that we do get a

02:32 string and integer in a float the only

02:34 downside to using structured arrays

02:36 instead of standard ones is that you

02:37 cannot use mathematical functions on

02:39 these inherently since we've already

02:40 discussed data types let's look at how

02:42 we can figure out the data type of an

02:44 array we can check the data type of an

02:46 array by using the command D type type

02:48 in print they're ready that you want to

02:50 check I'll check the numbers 1 and then

02:52 D type when we run the script we can see

02:55 that the D type for the numbers array is

02:57 an integer next let's look at a function

02:59 called shape we can be the shape I'm not

03:01 ready by typing in the function dot

03:02 shape print numbers dot shape will

03:06 return this value after executing the

03:08 command we can see that we're given a

03:10 tuple of 2 and 32 just denotes how many

03:12 arrays that we have in that set and then

03:14 the number 30 indicates the number of

03:16 items in each or right we can reshape

03:18 these sets by a command called reshape

03:20 we can reshape the numbers are ready by

03:22 typing in print and then numbers dot

03:24 reshape and then the shape of the new of

03:26 ready that we want so 3 & 2 save it and

03:29 execute it now instead of our two we

03:31 have three sets of 0 1 2 3 & 4 5 if you

03:35 want to transpose your data set you can

03:37 do that with the command transpose you

03:38 can see this by typing in print there

03:40 right and then transpose instead of our

03:44 sets being 0 1 2 3 & 4 5 there now 0 3 1

03:47 4 and 2 5 now that we know how to figure

03:49 out the shapes of different data sets we

03:51 need to be able to call the data that we

03:53 want we can do this by indexing any

03:54 array

03:55 pasting in a new sample we can see it

03:56 more clearly with this array it's just a

03:58 number set from 0 to 10 let's say that

04:01 we wanted to get every other value in

04:02 the survey starting at 1 and ending at 9

04:04 we can do this by typing in a variable

04:06 name there ready that we want to index

04:09 and then how we want to index it so for

04:11 us we want to start at 1 we want to end

04:14 at 9:00 and we want our step size to be

04:16 to save this and print it out on your

04:18 terminal when we print this to the

04:19 terminal we get exactly what we expect

04:21 we started one and into 7 in our step

04:23 - we can also index with negative

04:25 numbers we can demonstrate negative

04:27 indexing by creating a new variable call

04:29 the data set that you want to index and

04:31 then let's use the value negative one an

04:33 ending value of negative 10 and then a

04:35 step size of negative one save this in

04:38 printer to terminal when we print this

04:39 to the terminal we see that we have

04:41 successfully negatively indexed array

04:43 next we need to learn how to validate

04:45 our data numpy has a masking feature

04:47 built in so let's import that now type

04:49 import numpy ma as ma a numpy mask is

04:53 just a way to remove data from your data

04:55 set using a sample here we can see that

04:58 we have an array of all positive numbers

04:59 except one whenever you identify a mask

05:01 value is true it removes the array value

05:04 from that array when we save and print

05:06 this to the terminal we can see it being

05:08 removed years since we've created a mask

05:10 with the true value in the fourth

05:11 position we have removed the minus 1

05:13 value manually so now our data set is

05:16 only one to three blank five we can also

05:19 do this conditionally using the masks

05:21 where your command here we had the same

05:23 thing except we're using a conditional

05:24 format of every value that is less than

05:27 zero and we've returned a new array

05:28 where this condition applies we can

05:30 print MC and we should expect MC to

05:32 equal MX and here it does this is a good

05:35 way to remove data from your set that

05:37 isn't valid now that we know how to work

05:38 with arrays and validate the data let's

05:40 see all the things that we can do with

05:41 them let's import the module may have to

05:43 see all the functions that were you have

05:44 available to us let's work with these

05:47 for sample arrays I know these examples

05:49 will not benefit everyone so I'm just

05:50 going to show you all of them very

05:52 quickly we can take the sine of a data

05:53 set

05:54 I have a cumulative sum of a data set

05:55 add two arrays together raise exponents

05:58 to every value in the data set find all

06:00 the absolute values convert an array to

06:02 a matrix from none Pike or we can

06:04 convert strings to all upper cases given

06:07 two dates we can find all the business

06:08 days in between them we have access to

06:10 financial operations here we have a

06:12 future value function we can use numpy

06:14 for linear algebra operations like

06:16 taking the dot product between two

06:18 arrays we can find out when a logical

06:20 statement applies to two different

06:21 arrays here as an example we have

06:23 greater than and less than we can create

06:24 a random array of a specified shape and

06:27 we also have access to a large

06:28 statistical library I hope this is a

06:30 good introduction on how useful the

06:32 numpy package is there's so many

06:34 different things that we can use it for

06:35 if you

06:36 any questions or comments feel free to

06:37 leave them below until next time

06:40 [Music]

## Leave a Reply