- Coding Videos/
- Python /
- Python: Numpy | tutorial on numpy arrays with examples

# Python: Numpy | tutorial on numpy arrays with examples

### Download Video link >

Python: Numpy | tutorial on numpy arrays with examples

This Python Numpy arrays tutorial covers how to install, creating arrays, indexing, slicing, reshape, random functions, math and statistical functions, and many more examples, from basics to advanced. Get my Jupyter Notebook here, https://github.com/joeyajames/Python/tree/master/Numpy

source

### View Comments source >

### Transcript view all >

00:00 hi I'm Joe James in this video we're

00:02 gonna learn how to use Python numpy

00:04 numpy is pythons numerical Python

00:07 library it lets you do math functions

00:09 and statistical functions it also has a

00:12 really awesome built-in

00:14 multi-dimensional array data structure

00:16 that it's based on so we'll learn how to

00:19 use the numpy array and I'll tell you

00:22 it's a lot faster than using Python

00:23 lists because it's usually stored in a

00:26 contiguous block of memory so it can

00:28 iterate through the items in the array

00:31 much quicker and it's also much faster

00:33 to code because you don't have direct

00:34 for loops so num pie n dimensional

00:38 arrays are sometimes also called indie

00:40 arrays and they're called this because

00:42 you can have any number of dimensions so

00:45 one dimension or ten dimensions or 100

00:48 dimensions if you really want there's no

00:50 limit so to install you can use pip pip

00:53 install numpy kind of typical way to

00:56 install that there's other ways to

00:57 install if you have a package manager

00:59 use whatever you like and to import

01:02 we're gonna do import numpy as MP that's

01:05 the standard convention for importing

01:08 numpy now we're going to learn how to

01:12 instantiate a numpy array there are a

01:15 lot of different ways to create an

01:16 empire array and you can see all the

01:18 details on the Constructors here on this

01:20 documents page but I'm covering most of

01:23 the most popular ones here the ones that

01:25 are going to use most commonly so a lot

01:27 of different ones here and I got

01:28 examples of every one of them so first

01:30 you can pass in a list here we passed in

01:33 a list 1 3 5 7 9 and 11 and you can see

01:36 that's in square brackets you could also

01:38 have an existing list that's assigned to

01:40 a variable and you can just pass in that

01:42 variable that will work as well here we

01:45 print out a and you can see we had a

01:46 numpy array 1 3 5 7 9 11 we can also use

01:51 the a range function this is a specific

01:54 function to numpy it's very similar

01:56 though to the range function in python

01:58 so there's a start a stop and a step so

02:03 here we set a step of two and we're

02:05 starting from 1 to 12 the 12 is not

02:07 inclusive so we're just getting the odd

02:09 numbers from 1 to 11

02:11 so we basically get a repeat of exactly

02:14 what we just did above one three five

02:16 seven nine and eleven with this a range

02:18 function but the a range function is

02:20 very versatile so you can use that very

02:22 often for creating a arrays of existing

02:25 numbers that you want to use so in P dot

02:30 linspace is linear spacing it's a

02:33 controlled number of items so the

02:35 parameters are start/stop and number of

02:38 items instead of setting the step you

02:40 get to decide how many items you need in

02:43 this case we're going to go from 5 to 8

02:44 and it's a total of 13 items and they're

02:48 all going to be equally spaced so in

02:50 this case we get five five and a quarter

02:53 five and a half five and three-quarters

02:55 six and so on all the way up to eight so

02:59 you can control linear spacing and you

03:01 control number of items if we want an

03:05 array full of zeros we can say NP zeros

03:08 and then we just pass in our dimensions

03:11 so four rows and two columns so what we

03:15 get is a two dimensional array of zeros

03:19 and if we pass in NP ones and two comma

03:24 three what we get is a two dimensional

03:27 array of ones so three ones and three

03:31 ones and here you'll notice that we also

03:34 set the data type and we'll talk more

03:36 about that in this video

03:37 but here we set the data type to NP is

03:40 16 which is one of the defined data

03:42 types of numpy we can pass in a number

03:47 here we pass in 88 and we say we want an

03:49 array full of the number 88 so we can

03:53 pass that in and we use NP duffl to do

03:55 that the array will be full of 88 and

03:58 the dimensions of the array are 6 comma

04:01 nothing so it's a one-dimensional array

04:03 with six elements and you can see the

04:06 result is 688 s down here if we use from

04:12 string then we can pass in a string of

04:15 values and they are separated by a space

04:18 so we have SEP space we also defined our

04:22 data type as NP int we didn't say what

04:25 you

04:25 sighs but numpy will decide that on its

04:28 own so from string we'll convert this

04:32 separated by spaces converted to integer

04:36 type and when we get back is in four

04:39 items of integer array 25 30 35 40 we

04:45 can also use NP array and then pass in a

04:48 two-dimensional list this is just a

04:50 standard Python list it's two dimensions

04:53 so we have 1 3 5 and 7 9 11 where there

04:57 are two lists embedded into a list and

05:00 you see we get exactly the same type of

05:02 data structure in numpy a

05:05 two-dimensional array with 1 3 5 and 7 9

05:08 11 in pita zeros like gives us something

05:14 that is exactly the same shape as array

05:16 a above 1 3 5 7 9 11 that's going to

05:21 take the shape of that array and it's

05:23 going to fill it with zeros so we get a

05:25 two-dimensional array full of zeros and

05:28 we assign this to be instead of a a by

05:30 the way so those are some of the

05:34 constructors for numpy arrays then let's

05:36 look at some of the attributes there are

05:39 a number of standard attributes so that

05:40 you can analyze how an array what size

05:43 shape and how much base in memory it

05:45 takes up a DES size tells you the number

05:48 of items in the array in this case 6 AE

05:52 shape tells you the shape or the

05:54 dimensions of the array so here we have

05:57 a shape that's 2 by 3 mind you a is this

06:02 1 3 5 7 9 11 it's a two-dimensional

06:06 array it's 2 by 3 in dim gives you the

06:10 number of dimensions what we just saw

06:11 the size there are two dimensions so

06:13 that answer is 2 and then item size

06:17 tells you how many bytes each item in

06:19 the array takes up in memory now every

06:23 item in an array is the same data type

06:25 so they all take up the same amount of

06:27 memory so if this array is defined as

06:31 int 64 which is our D type or data type

06:34 in 60 for each item takes up 8 bytes so

06:38 item size is

06:39 and in bytes is the size of the whole

06:43 array in memory which in this case is 48

06:46 we have six items each item is eight

06:49 bytes so it's 48 bytes for the entire

06:52 array in memory we could get the same

06:54 value by multiplying a dot size times a

06:58 dot item size would also give us 48

07:01 those are some of the key attributes of

07:03 numpy arrays you can see a complete list

07:06 of attributes and methods at this link

07:07 here let's look at indexing and slicing

07:12 we're not going to cover this in a lot

07:14 of detail there's tons of examples

07:17 available online and I don't think I

07:19 need to dig into this so much also I've

07:21 covered this in so much detail already

07:23 in using regular lists and tuples that

07:26 I'm not going to cover it so much detail

07:28 here but I'll show you a few examples so

07:31 print a we have 1 3 5 7 9 11 that's a

07:35 two-dimensional array and we can use an

07:38 index index 1 to get the one item which

07:41 in this case is also an array 7 9 11

07:43 it's the one title just the lower value

07:46 here and if we want to pass in two

07:49 indexes 0 comma 2 we use square brackets

07:54 around each one of those so 0 tells us

07:57 we want to get an item out of this 0

07:59 with item in this array and 2 tells us

08:02 we need to get the number 2 item in it

08:04 which is 5 so we get a 5 back which is

08:07 an integer

08:09 another thing is cool and implies that

08:11 you can slice multiple dimensions of the

08:13 data by passing in multiple sets of

08:16 parameters separated by a comma so

08:19 inside a we have a square bracket for

08:22 the whole array and then we can have use

08:24 a colon to separate from 2 and step or

08:30 start stop and step for the X dimension

08:34 and then a comma and then we can pass in

08:37 start stop and step if we want to for

08:40 the Y dimension so we can slice rows and

08:44 we can slice columns any way we want by

08:46 passing in multiple parameters separated

08:49 by a comma to slice the different

08:51 dimensions and here since we

08:53 all of the items from the X dimension

08:56 and then from the Y dimension we take

08:59 only a column 1 because 1 2 to the 2 is

09:03 not inclusive so this basically says

09:05 that we want the one column of the array

09:07 which is 3 & 9 so you can see we get

09:10 back 3 & 9 there are a lot of different

09:15 ways to manipulate arrays we can reshape

09:17 we can swap axes we can flatten the

09:20 array we can change the shape of the

09:21 array whatever we want to do I have a

09:23 few examples here but they're covering a

09:25 lot more detail on this array

09:26 manipulation routines documentation so

09:30 if we have a new range function a range

09:33 negative 9 to negative 3 and that's

09:37 going to give us back a 6 element array

09:39 with 1 dimension and then we do dot

09:42 reshape 2 comma 3 to shape it into a 2

09:45 rows and 3 columns array so we get 9 8 7

09:50 6 5 4 that gives us a two dimensional

09:53 array and then if we swap axes 0 comma 1

09:58 you can see that we get exactly the same

10:00 values but they're reorganized

10:02 we swap the X and y axis for the arrays

10:05 so 9 8 & 7 is now a column 6 5 & 4 is

10:09 also a column and then we can flatten

10:13 this back to a linear array if we want

10:15 to one dimensional we do see dot flatten

10:18 and we get 9 6 8 now the art items are

10:21 reorganized because of the swapping axes

10:24 but yeah we get a flatter array now and

10:30 we mentioned e-type before we can use

10:33 data type to say space if we want now in

10:36 really large arrays this is pretty

10:38 important because if you have millions

10:39 or tens of millions or hundreds of

10:40 millions of items the amount of space

10:43 that data takes up is pretty important

10:45 so here we use just for an example 0 to

10:50 100 so 100 element array and when we

10:53 print that data type which you can see

10:55 is in 64 now we didn't specify a data

10:59 type numpy just selected that as a

11:01 default in 64 so that's 8 bytes per

11:05 element

11:06 and then when we look at the number of

11:08 bytes of that array we can see it's 800

11:10 bytes for the 100 elements but if we

11:14 know that we're not going to use any

11:16 really large integers 10 digit integers

11:19 or anything crazy like that

11:20 we can limit this to a smaller int type

11:23 in this case we'll pass in int 8 as the

11:26 data type and then we can shrink it us

11:30 down to 8 bytes per item and then the

11:34 whole array only takes up 100 bytes so

11:37 if we know that we're not going to use

11:38 any really large elements in the array

11:40 no more than 8 bytes or 256 then we can

11:45 define the data type to be smaller than

11:47 in 64 then we can save a lot of space

11:50 what I'm saying is use a data type that

11:54 makes sense for the data that you want

11:56 to store up casting rounding print

12:01 formatting data type of most elements is

12:04 up cast to the most precise element as I

12:07 showed you up above we're getting on in

12:09 64 and the same thing in floating-point

12:11 you'll get float 64 so if you want to

12:15 down cast those you have to do it

12:16 explicitly so here we have e equals NP

12:20 array and then we pass in one floating

12:23 point value and then a bunch of small

12:25 integers but this data type is going to

12:29 be float64 because we have one floating

12:31 point value so when we print out EDD

12:35 type we get float 64 so each one of

12:38 these is taking up eight bytes and

12:40 they're also all going to be floating

12:41 point values every item in this array

12:44 has to be the same data type and in this

12:46 case we have one floating point value so

12:48 they're all floating point values this

12:52 is handy we can round off all of the

12:55 items in the array to four digit four

12:57 decimal places or however many we want

12:59 in this case four and then we print out

13:01 the array again you can see we have all

13:04 the elements rounded off to four decimal

13:06 places and then if you want to set print

13:11 formatting we can set it to precision

13:13 too or to print two decimal places only

13:16 and then we can also suppress scientific

13:18 notation if you want

13:19 saying suppress equals true and so this

13:22 is just print formatting for numpy

13:25 arrays and then here you can see even

13:27 though we have four decimal places of

13:29 precision in this value we only print it

13:31 two of them what kinds of data types are

13:36 available in numpy and here you can see

13:39 we use pretty print to show the MPSC

13:42 types which is all of the available data

13:45 types for numpy arrays you can see we

13:48 have complex numbers up to 256 bits we

13:52 have floating-point values from 16 bit

13:55 all the way up to 128 bit precision and

13:57 we have integers from 8-bit up to 64 bit

14:00 and then there's also boolean objects

14:03 bytes strings and so on strings are very

14:07 seldom used

14:08 you're going to see the unsigned

14:10 integers integers and floating point

14:11 values account for most of what you do

14:14 in numpy because it's all numerical

14:15 operations so we can read and write with

14:20 files in numpy pretty easily actually

14:23 it's really handy to load in an entire

14:25 document of text or numbers in a single

14:30 line of code so we can use the loadtxt

14:32 function or we can use Jen from text and

14:35 I put links here to their documentation

14:37 you'll find that they both are very

14:39 similar Jen from text has some fault

14:42 tolerance so that if there's a missing

14:44 value or an invalid value it can handle

14:46 that you can set let's say values to

14:49 zero in case they're invalid or whatever

14:51 you want to do however you want to

14:52 handle that load from text your data

14:54 values all have to be there so we passed

14:58 in the file name we're going to skip the

15:00 first row because in this text file the

15:02 first row is headers and then we put our

15:05 delimiter is commas and we define our

15:07 data type and then you can see our array

15:11 printed out here and our data type

15:13 prints out is int 32 and then if we want

15:18 to save this values but this time we use

15:21 a delimiter of semicolon instead of

15:23 comma let's say we can put in our file

15:25 name we pass in F which is our array

15:28 name and we define the a delimiter and

15:31 the format

15:33 we want to save these as integers so we

15:36 do % D to force it to be stored as an

15:40 integer and I want to put headers on my

15:42 top of my file so I put headers equals a

15:45 b c d e f e g h i j for the name of the

15:49 column headers and then we have to put

15:51 comments equals nothing because if i

15:54 don't put that the default is for this

15:57 header to have a hashtag in front of it

15:59 which makes it a comment known to use

16:03 jen from text the syntax is very similar

16:06 so we pass in the file name skip header

16:09 equals 1 we have one row header so we

16:11 want to skip that delimiter equals comma

16:13 and d type equals int 32 and you can see

16:17 we loaded in exactly the same data using

16:20 jen from text so two different ways to

16:23 load data mathematical functions are

16:27 probably the most powerful and most

16:29 important function of numpy so I really

16:32 gave you a quick introduction of those

16:34 here and you're going to find a lot more

16:36 information at the documentation on this

16:38 page now the nice thing about

16:41 mathematical functions is that you don't

16:42 have to iterate you don't have to use

16:44 for loops to iterate through every

16:46 element in your array nested for-loops

16:48 are gone you apply one function one time

16:52 to the whole array at the same time and

16:54 python takes care of everything else

16:56 behind the scenes so if we want to print

16:59 G greater than four python is going to

17:03 apply this comparison to every element

17:06 in your numpy array numpy does this for

17:08 you so you can see the result here of

17:11 the comparisons for each element in the

17:14 array and if we want to multiply every

17:17 item we square it and subtract one let's

17:20 say G squared minus one we can apply

17:23 that mathematical function to every

17:26 single element in the array at one time

17:28 and it prints out the result here we can

17:34 also get the min the max the sum of the

17:37 entire array the average of the array

17:39 variants standard deviation these are

17:43 all built-in functions that are very

17:44 easy to use

17:45 so you can see the results here and then

17:49 if we want the sum of a specific axis we

17:51 can say sum of axis 1 or min of axis 0

17:55 it's pretty easy to pass in an axis as

17:57 an optional parameter and then we can

18:01 get the index of an element so the index

18:03 of the min or index of the Max element

18:06 or an array of indices that would put

18:09 the array in a sorted order so column

18:12 operations you can do operations on a

18:15 specific row or a specific column pretty

18:17 easy to use the row just with the square

18:20 brackets and an index in it if you want

18:23 to do it on a column we can use square

18:25 brackets and then use an empty colon in

18:29 the first item and then after the comma

18:32 we pass in parameters in the second one

18:35 so here's how we can define columns this

18:38 is going to be column 2 because the 3 is

18:41 not inclusive so this gives us the

18:43 second column and we print out the max

18:46 so here we print out just the values of

18:49 that column then we get the max by

18:51 putting dot max after that and it gives

18:54 us 8 which is the max of these items and

18:58 if we wanted to we could set a pointer

19:00 to that or assign it to a variable

19:01 column 3 but something you need to be

19:04 aware of here is that we're not actually

19:06 copying or duplicating this data the

19:08 data is still in array G just because

19:13 we're working with column 3 now we're

19:16 still affecting the values that are

19:18 stored in the G array so when we print

19:21 column 3 standard deviation we get 2

19:25 point 9 8 and then when we say column 3

19:28 x equals 100 beware this applies to the

19:33 G data so when we print out the change

19:35 in G after that multiplication there

19:38 that we did to column 3 you can see the

19:40 column 3 all the numbers just suddenly

19:42 got bigger so when you assign some

19:44 subset of your array to a new variable

19:46 like this beware that it may not be

19:48 copying the values it's actually just

19:52 assigning a variable as a pointer to

19:54 those values in the array

19:58 and lastly let's look at numpy random

20:00 functions so first let's set our

20:03 printing options to 5 decimal places

20:05 then we'll generate a random array of

20:08 floating-point values that has 6 items

20:11 in it so here you can see those six

20:13 items floating point values limited to 5

20:18 precision 5 decimal places and we use

20:22 random dot R and int to generate an

20:24 array of integers and here we pass in

20:26 arguments from 10 to 99 then we want 8

20:30 items so the result we got is this array

20:34 of 8 items between 10 and 99 and if we

20:39 just want to shuffle that array that's

20:41 array H so we can do random shuffle only

20:44 passing an H is an in-place shuffle so

20:48 we don't need to assign the result to H

20:50 again it's already an in-place shuffle

20:53 so now we get shuffled values when we

20:55 print out H again if we want to randomly

20:59 select one of those values here we do

21:02 random choice from H and we get 33 and

21:06 then if we want to do an in-place sort

21:09 we can call H sort they'll do an

21:13 in-place sort on our numpy array that

21:17 covers this video on numpy arrays

21:19 I hope this introduction was helpful for

21:21 you and useful and if so please click

21:23 thumbs up and subscribe to my channel

21:24 I'm Joe James thanks for watching

## Leave a Reply