Python-3.4.3/C3/Basic-Matrix-Operations/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time
Narration
00:01 Welcome to the spoken tutorial on Basic Matrix Operations.
00:07 In this tutorial, you will learn to, Create matrices from lists
00:13 Perform basic matrix operations like

addition

00:19 subtraction and multiplication
00:23 Perform operations to find out

determinant of a matrix

00:29 inverse of a matrix

Eigen values and Eigen vectors of a matrix

00:37 To record this tutorial, I am using

Ubuntu Linux 16.04 operating system

00:44 Python 3.4.3 and IPython 5.1.0
00:51 To practise this tutorial, you should have basic knowledge about
00:56 Lists
00:58 Arrays and accessing parts of arrays and

Theoretical knowledge of matrix operations

01:06 If not, see the relevant Python tutorials on this website.
01:11 In Python, we create a matrix using numpy matrix class.
01:16 Matrix operations can be done using numpy operators and functions.
01:22 Let us start ipython.
01:25 Open the terminal.
01:27 Type ipython3 and press Enter.
01:31

From here onwards, remember to press the Enter key after typing every command on the terminal.

01:38 Let us create a matrix m1.
01:41 Type from numpy import matrix
01:47 Then type, m1 is equal to matrix inside brackets inside square brackets 1 comma 2 comma 3 comma 4
01:57 Now type m1
02:00 This creates a matrix with one row and four columns.
02:05 This can be verified by typing m1.shape

This gives the output as (1, 4)

02:15 A list can also be converted to a matrix as follows,

Type as shown.

02:23 You can see the matrix m2 with values from list l1.
02:29 To convert an array to a matrix, use the asmatrix method in numpy module.
02:36 We can use arange and reshape methods to generate an array.
02:42 Type as shown.

arange is a method available in numpy.

02:49 Here it returns an array of evenly spaced values between 1 and 9.
02:55 reshape is used to change the shape of the array to 2 rows and 4 columns.
03:02 asmatrix is a method available in numpy and it interprets the input as a matrix.
03:09 Pause the video.

Try this exercise and then resume the video.

03:15 Create a two dimensional matrix m3 of shape 2 by 4 with the elements 5, 6, 7, 8, 9, 10, 11, 12.
03:25 Hint: Use arange() and reshape() methods and asmatrix() function.
03:31 Switch back to the terminal for the solution.
03:35 Type, m3 is equal to asmatrix inside brackets arange inside brackets 5 comma 13 dot reshape inside brackets 2 comma 4
03:48 Type m3 You can see the required output.
03:54 Next let us see some matrix operations.

Type, m3 plus m2

04:02 It performs element by element addition, that is matrix addition.
04:07 Note that both the matrices should be of the same shape.
04:12 Similarly, type m3 minus m2
04:17 It performs matrix subtraction, that is element by element subtraction.
04:24 Note that both the matrices should be of the same shape.
04:28 Now we can multiply a scalar i.e a number by a matrix as shown.
04:36 Next we will check the size of m2 by typing,

m2 dot shape.

04:43 We get a tuple (2, 4).

Matrix m2 is of the shape, two by four.

04:49 Let us create another matrix, of the order 4 by 2.
04:55

Type, m4 is equal to asmatrix inside brackets arange inside brackets 1 comma 9 dot reshape inside brackets 4 comma 2

05:07 Now to check the shape, type m4.shape

We get (4,2) as the shape of m4.

05:16 The multiplication operator asterisk is used for matrix multiplication.
05:22 Type m2 asterisk m4
05:27 Now we get output as multiplication of m2 and m4.
05:33 Let us now see, how to find out the transpose of a matrix.
05:38 To see the content of m4, type print inside brackets m4
05:46 Now type, print inside brackets m4 dot capital T
05:53 As you saw, m4 dot capital T will give the transpose of a matrix.
05:59 We can get the determinant of a square matrix by using the function det() in numpy.linalg module.
06:09 Pause the video.

Try this exercise and then resume the video.

06:15 Find out the determinant of this 3 by 3 matrix.
06:20 Switch to the terminal for the solution.
06:23 Type as shown.
06:26 The determinant of m5 can be found by issuing the command

det inside brackets m5'

06:35 We get determinant of m5 as output.
06:39 We can get the inverse of a square matrix by using inv() function in numpy.linalg module.
06:48 Let us find the inverse of the matrix m5.
06:52 Type as shown.

Then to see the inverse, type im5

07:02 Type from numpy import eye, allclose
07:09 Then type, allclose inside brackets im5 asterisk m5 comma asmatrix inside brackets eye inside brackets 3
07:22 This returns True.
07:25 We know that multiplication of a matrix with its inverse gives the identity matrix.
07:31 Identity matrix is created using eye() function. It is present in the numpy module.
07:40 Here asmatrix inside brackets eye inside brackets 3 gives identity matrix of size 3.
07:48 allclose is a function that returns True if two arrays are element wise equal.
07:55 To know more about these, we will check the documentation.
08:00 Type the function name followed by a question mark in IPython console.
08:05 Type eye question mark
08:11 To quit the documentation, press q.
08:15 It is a good practice to read documentation of new functions that you come across.
08:22 Let us now move onto Eigen vectors and Eigen values.
08:27

Given a square matrix A

eig inside brackets A inside square brackets 0 gives its eigenvalues

08:37 eig inside brackets A inside square brackets 1 gives its eigenvector
08:43 eigvals inside brackets A gives its eigenvalues
08:49 eig and eigvals functions are present in numpy.linalg module.
08:58 Let us find out the eigenvalues and eigenvectors of the matrix m6.

Type as shown.

09:07 Now to see the value, type,eig inside brackets m6
09:14 diag inside brackets again inside brackets 1 comma 2 comma 3

creates a diagonal matrix with 1,2,3 as diagonal elements and 0 elsewhere .

09:26 diag() function is present in numpy module.
09:31 Note that eig inside brackets m6 returned a tuple of one array and one matrix.
09:38 The first element in the tuple is an array of three eigen values.
09:43 The second element in the tuple is a matrix of three eigen vectors.
09:48 To get eigen values type,eig underscore value is equal to eig inside brackets m6 inside square brackets 0
10:00 Then type eig underscore value
10:04 As you can see eig underscore value contains eigenvalues.
10:09 To get eigen vectors type,eig underscore vector is equal to eig inside brackets m6 inside square brackets 1
10:20 Then type eig underscore vector
10:25 eig underscore vector contains eigen vector.
10:29 The eigen values can also be computed using eigvals() function.

Type as shown.

10:39 Then type eig underscore value1
10:44 You can see that, eig underscore value and eig underscore value1 are same.
10:52 This brings us to the end of this tutorial. Let us summarize.
10:58 In this tutorial, we have learnt to,

Create matrices using arrays

11:03 Add, subtract and multiply matrices
11:07 Take scalar multiple of a matrix
11:11 Use the function det() to find the determinant of a matrix
11:16 Find out the inverse of a matrix using the function inv()
11:21 Find out the eigen vectors and eigen values of a matrix, using the functions eig() and eigvals()
11:30 Here are some self assessment questions for you to solve
11:34 First. A and B are two matrix objects of appropriate sizes. Which one of the below is correct for matrix multiplication?
11:45 Second. eig inside brackets A inside square brackets 1 and eigvals inside brackets A are the same. True or False?
11:56 And the answers,

First. Matrix multiplication between A and B is done by, A asterisk B

12:05 Second. False. eig inside brackets A inside square brackets 0 and eigvals inside brackets A are same, that is both will give the eigen values of matrix A.
12:19 Please post your timed queries in this forum.
12:23 Please post your general queries on Python in this forum.
12:28 FOSSEE team coordinates the TBC project.
12:32 Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

12:42 This is Priya from IIT Bombay signing off.

Thanks for watching.

Contributors and Content Editors

PoojaMoolya