Change language

Python NumPy Tutorial – Reshaping Matrices and Working with Axes

Python NumPy Tutorial - Reshaping Matrices and Working with Axes

Hi and welcome to this video. In this video were  going to learn how to change the shape of a matrix or a vector. Let me begin by an example: Lets  say that you want to create a matrix containing all the numbers from 1 to 100 and you want  to lay it out in a 10 by 10 grid. So how do you do this? We know how to create the numbers  from 1 to 100 in a 1D array. So this is just by creating vector = np.arange and then we start at 1  Remember that the endpoint is not included, so we need to go up to 101. So let me also just return  the vector and now we have all the numbers from 1 to 100 but its not the matrix and its certainly  not in a 10 by 10 grid so and what i want to do is to reshape this vector into a 10 by 10  grid so my matrix is going to be defined by vector and the way im going to reshape  this vector is by applying the reshape function so the reshape function takes in the shape  you want your matrix to be in so this case i want it to be a 10 by 10 matrix so let me also  return the matrix so we can see what happens and what we get back is the numbers from 1 to 100  but each row has just 10 numbers here so lets say that we want to go back to a vector again so the  way to do this is also by the reshape method but this time we want to have the shape with just  100 consecutive numbers so we write reshape and then 100 and see what happens and now we  get as output the same thing we started with so let me print out the matrix again so one thing  to note is by using the reshape method youre not creating a new matrix you are just creating a  view into your old vector here so if i re-define an entry for instance the entry 0 comma 0 and i  want it to be minus 20 like this and i look at the original vector then the first entry here is minus  20 as well so this mean s that using this reshape method creates a view and after copy okay but we  can reshape matrices as well so let me say that my matrix here i want to reshape it to be in the  shape of for instance 50 comma 2 and run the cell so now i have 50 rows downwards with two entries  each okay sometimes it gets a bit tiresome to do all the math for instance lets say that  i know i want 50 rows but i dont know necessarily what i need to multiply 50 with to get  100 or i dont want to type it into the calculator what you can do is to write matrix dot reshape and  then we specify what we know we know the number of rows is going to be 50 and then instead of writing  out 2 we write out minus 1 and let me compile it so now you see that we get the same thing here  and the reason for this is that by writing -1 youre actually telling numpy just figure out  what you need to put there to make up 100 entries so one thing you might wondering about  is what happens if i try to reshape it into something which do not have a hundred  entries so let me try with dodge reshape and let me write 23 comma -1 try to run the cell  and then you see that numpy is really not happy with you it gives you back an error which tells  you that you cannot reshape this area here with shape 100 into a shape with 23 and the reason  is 123 is co prime so it doesnt have any common factors so what youre asking numpy to do it  cannot do so in last video we talked a bit about what are we going to do to go from a 1d area into  a 2d area with introducing the new axis and so on it happens just as often that you are given  a 2d array or even a higher dimensional area and you want to go down to 1d array because the  function you are trying to apply only except one day erase so what we have learned is that what  we can do is to use the reshape so i can write matrix dot reshape minus one and it will give  me out a 1d array but what we also can do is to use the flatten method so let me write  new vector and write matrix dot flatten like this and run the code and let me just  write new vector and you get the flattened version of the matrix matrix but there is one  difference between doing reset method and doing the flatten and that is that the flattened  method gives me a copy so if i try to reassign the first value of new vector and let me say  its equal to 1000 then if we look at new vector and it has 1000 inside the first  entry but if we write matrix then its still minus 20. so indeed  we see that using the flatten method gives us a copy instead of a view so if  you at any time want to force a copy you can use the copy function so for instance if  i want new matrix to be equal to np dot copy of just my original matrix no new matrix  is actually a copy so to test this we can write new matrix 0 comma 0 and just say that its  0 again so now the first entry of the new matrix is 0. but if i write out copy then we see that  for the original matrix its still -20. in the next video we are going to learn several new  ways to create new matrices and additionally we are going to learn to take the max the mean   and so on on a row-y basis so see you then hi and welcome back in this video we are going to  learn how to create some special kind of matrices and how to specify the axis in sum functions like  the mean and the max and so on so there are some matrices that pop up so often that we have a  special command in making them so one of them is to create a matrix with only zero values  and to do this we will use the np dot serums and then you give in the tuple with your  dimensions so for instance 3 comma 7 and if you run it we will create the 3x7 matrix we can  also use the mp.0s on vectors for instance if we only give in the number 10 it will create a vector  with 10 0 entries a similar sort of command is the ones command which creates a matrix with only  ones so it works exactly the same so you write np dot once and then you can give in a tuple with  the numbers so for instance 10 comma 5 and run it and here we have a 10 by 5 matrix and also we can  make vectors by just giving in a single number like 12 run this and here we have a vector with  only ones so if we want a vector with only two you can simply take this vector here multiply it  by two and now we have a vector with only twos another matrix that we use often especially  in linear algebra is the identity matrix so the identity matrix is the matrix consisting  of 1 along its diagonal and 0 anywhere else so for instance if i want the 3x3 matrix with  once along its diagonal and 0 everywhere else i can write np.i for the identity and then three  run this cell and then i have a three by three matrix with ones along the diagonal and zero  everywhere else so the identity matrix has always equal amounts of columns and rows so the next  thing i wanted to go through is how to use the sum function and the mean function when it comes  to matrices so first of all i need a matrix so here we have a matrix and  let me also just give it back to print it so here we have a three by four matrix  so what i want to do with this matrix is to take some of the entries so you can simply write  matrix dot sum and compile and this will give you the sum of all the entries so it works more  or less exactly the same as it did for vectors but lets say that you want to sum that only  over the rows so you want to take 7 plus 8 plus 7 plus 3 give out the number four plus five plus  two plus three give out the number and so on on the left row to do this you can specify the axis  inside the sum function so you write matrix thats sum and inside here you write the argument  axis and you want to give in the number corresponding to the axis which vanish so if i  go up here if i want to sum over all the rows i want the columns to vanish and the columns have  the axis equal to one so if i now run this with x is equal to one i end up with 25 which is the sum  of seven plus eight plus seven plus three fourteen which is four plus five plus two plus three  and eleven which is zero plus four plus three plus four if i want to sum over the columns  instead i can write matrix dot sum but this time i set xs equal to zero because i want the  rows to vanish and if i run this cell i get eleven which is seven plus four seventeen which is  eight plus five plus four and so on so this works with several different functions for instance  i can do the mean and right axis equal to 0 and now i get the mean over all the columns so 7  plus 4 plus 0 divided by 3 which gives me out of mean is 3.66667 and 8 plus 5 plus 4 divided by  3 is which is the mean of this column is 5.6667 and so on so you can do this with the max for  instance you can write matrix dot max axis equal to 1 run this and it gives me out the max over  all the rows so the first row have max 8 the second has max 5 and the last one has max 4. the  last thing i wanted to say is that if youre not completely sure if you can specify the axis inside  your function you can ask for help so for instance if i write matrix dot min and i want to see if  i can put in an axis here i can write shift tab and this will give me out the duct string and open  it here and then you see that you have this axis which you can specify okay this was everything i  wanted to say so see you again in the next video you

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically