Difference between revisions of "Python/C3/Matrices/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 1: Line 1:
 
{| border=1
 
{| border=1
!Visual Cue
+
|'''Time'''
!Narration
+
|'''Narration'''
 +
 
 
|-
 
|-
| 0:01
+
| 00:01
 
| Hello friends and welcome to the tutorial on 'Matrices'.
 
| Hello friends and welcome to the tutorial on 'Matrices'.
 
 
 
|-
 
|-
| 0:05
+
| 00:05
 
| At the end of this tutorial, you will be able to,
 
| At the end of this tutorial, you will be able to,
  
Line 16: Line 17:
  
 
|-
 
|-
| 0:31
+
| 00:31
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with lists", "Getting started with arrays", "Accessing parts of arrays".
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with lists", "Getting started with arrays", "Accessing parts of arrays".
  
 
|-
 
|-
0:42
+
00:42
 
| Let us start our ipython interpreter by pylab loaded
 
| Let us start our ipython interpreter by pylab loaded
  
 
|-
 
|-
|0:47
+
|00:47
 
|Type ipython hypen pylab in terminal
 
|Type ipython hypen pylab in terminal
  
 
|-
 
|-
0:52
+
00:52
 
| All matrix operations are done using arrays.
 
| All matrix operations are done using arrays.
 
   
 
   
 
|-
 
|-
0:55
+
00:55
 
| Thus all the operations on arrays are valid on matrices only.  
 
| Thus all the operations on arrays are valid on matrices only.  
  
 
|-
 
|-
1:00
+
01:00
 
| A matrix may be created as,
 
| A matrix may be created as,
  
 
|-
 
|-
| 1:02
+
| 01:02
 
|Type in the terminal m1 = array within bracket and square bracket 1 comma 2 comma 3 comma 4 hit enter.
 
|Type in the terminal m1 = array within bracket and square bracket 1 comma 2 comma 3 comma 4 hit enter.
  
 
|-
 
|-
1:16
+
01:16
 
| Using the method shape , we can find out the shape or size of the matrix,
 
| Using the method shape , we can find out the shape or size of the matrix,
  
 
|-
 
|-
|1:20
+
|01:20
 
|Type m1 dot shape and hit enter
 
|Type m1 dot shape and hit enter
  
 
|-
 
|-
|1:27
+
|01:27
 
|We can see the output
 
|We can see the output
  
 
|-
 
|-
1:29
+
01:29
 
| Since it is a one row four column matrix it returned a tuple, one by four.
 
| Since it is a one row four column matrix it returned a tuple, one by four.
  
 
|-
 
|-
1:46
+
01:46
 
| A list can also be converted to a matrix as follows,
 
| A list can also be converted to a matrix as follows,
  
  
 
|-
 
|-
|1:50
+
|01:50
 
|Type l1 = within square bracket square bracket 1 comma 2 comma 3 comma 4 comma in another square bracket 5 comma 6 comma 7 comma 8  
 
|Type l1 = within square bracket square bracket 1 comma 2 comma 3 comma 4 comma in another square bracket 5 comma 6 comma 7 comma 8  
 
  Type m2 = array within bracket 11
 
  Type m2 = array within bracket 11
  
 
|-
 
|-
|2:28
+
|02:28
 
|Sorry you have to type l1 array .
 
|Sorry you have to type l1 array .
  
 
|-
 
|-
| 2:35
+
| 02:35
 
| Pause the video here, try out the following exercise and resume the video.
 
| Pause the video here, try out the following exercise and resume the video.
  
 
|-
 
|-
| 2:43
+
| 02:43
 
| Create a two dimensional matrix m3 of order 2 by 4 with elements 5, 6, 7, 8, 9, 10, 11, 12.
 
| Create a two dimensional matrix m3 of order 2 by 4 with elements 5, 6, 7, 8, 9, 10, 11, 12.
  
 
|-
 
|-
| 2:51
+
| 02:51
 
| Switch to terminal for solution.  
 
| Switch to terminal for solution.  
  
 
|-
 
|-
2:54
+
02:54
 
| m3 can be created as,
 
| m3 can be created as,
  
 
|-
 
|-
|2:56
+
|02:56
 
|Type m3 = array within closing bracket inside square bracket square bracket 5 comma 6 comma 7 comma 8 comma in another square bracket 9 comma 10 comma 11 comma 12
 
|Type m3 = array within closing bracket inside square bracket square bracket 5 comma 6 comma 7 comma 8 comma in another square bracket 9 comma 10 comma 11 comma 12
  
 
|-
 
|-
| 3:31
+
| 03:31
 
| Let us now move to matrix operations.
 
| Let us now move to matrix operations.
  
 
|-
 
|-
| 3:34  
+
| 03:34  
 
| We can do matrix addition and subtraction easily.
 
| We can do matrix addition and subtraction easily.
  
 
|-
 
|-
| 3:37  
+
| 03:37  
 
| m3+m2 does element by element addition, that is matrix addition.  
 
| m3+m2 does element by element addition, that is matrix addition.  
  
 
|-
 
|-
| 3:43  
+
| 03:43  
 
| Note that both the matrices should be of the same order.
 
| Note that both the matrices should be of the same order.
  
 
|-
 
|-
|3:47
+
|03:47
 
|Type m3+m2 and hit enter so you can see the output.
 
|Type m3+m2 and hit enter so you can see the output.
  
 
|-
 
|-
| 3:55
+
| 03:55
 
| Similarly,m3 minus m2 does matrix subtraction, that is element by element subtraction.
 
| Similarly,m3 minus m2 does matrix subtraction, that is element by element subtraction.
  
 
|-
 
|-
|4:02
+
|04:02
 
|You can try out by typing m3 minus m2
 
|You can try out by typing m3 minus m2
  
 
|-
 
|-
| 4:09
+
| 04:09
 
| Now let us try,matrix multiplication
 
| Now let us try,matrix multiplication
  
 
|-
 
|-
|4:13
+
|04:13
 
|Type m3 star m2
 
|Type m3 star m2
  
 
|-
 
|-
4:20
+
04:20
 
| Note that in arrays m3 star m2 does element wise multiplication and not matrix multiplication,
 
| Note that in arrays m3 star m2 does element wise multiplication and not matrix multiplication,
  
 
|-
 
|-
4:28
+
04:28
 
| Matrix multiplication in matrices are done using the function  dot()
 
| Matrix multiplication in matrices are done using the function  dot()
  
 
|-
 
|-
|4:37
+
|04:37
 
|Type dot within bracket m3 comma m2 and hit enter.  
 
|Type dot within bracket m3 comma m2 and hit enter.  
  
 
|-
 
|-
|4:47
+
|04:47
 
|So we can see error value show in the command.
 
|So we can see error value show in the command.
  
 
|-
 
|-
| 4:50
+
| 04:50
 
| Due to size mismatch, the multiplication could not be done and it returned an error.
 
| Due to size mismatch, the multiplication could not be done and it returned an error.
  
 
|-
 
|-
4:56
+
04:56
 
| Now let us see an example for matrix multiplication.
 
| Now let us see an example for matrix multiplication.
  
 
|-
 
|-
5:00
+
05:00
 
| For doing matrix multiplication we need to have two matrices of the order n by m and m by r and the resulting matrix will be of the order n by r.
 
| For doing matrix multiplication we need to have two matrices of the order n by m and m by r and the resulting matrix will be of the order n by r.
  
 
|-
 
|-
5:11
+
05:11
 
| Thus let us first create two matrices which are compatible for multiplication.
 
| Thus let us first create two matrices which are compatible for multiplication.
  
 
|-
 
|-
|5:16
+
|05:16
 
|Type m1.shape and hit enter
 
|Type m1.shape and hit enter
  
 
|-
 
|-
| 5:24
+
| 05:24
 
| matrix m1 is of the shape one by four,
 
| matrix m1 is of the shape one by four,
  
 
|-
 
|-
5:28
+
05:28
 
| let us create another one, of the order four by two,
 
| let us create another one, of the order four by two,
  
 
|-
 
|-
|5:33
+
|05:33
 
|So type m4 = array in closing bracket within square bracket within square bracket 1 comma 2 comma  within square bracket 3 comma 4 comma  within square bracket 5 comma 6 within square bracket comma  within square bracket 7 comma 8  
 
|So type m4 = array in closing bracket within square bracket within square bracket 1 comma 2 comma  within square bracket 3 comma 4 comma  within square bracket 5 comma 6 within square bracket comma  within square bracket 7 comma 8  
 
  Type dot within bracket m1 comma  m4
 
  Type dot within bracket m1 comma  m4
  
 
|-
 
|-
| 6:10
+
| 06:10
 
| Thus the dot() function is used for matrix multiplication.
 
| Thus the dot() function is used for matrix multiplication.
  
 
|-
 
|-
| 6:15
+
| 06:15
 
| As we already learnt in arrays, the function  identity()  which creates an identity matrix of the order n by n,
 
| As we already learnt in arrays, the function  identity()  which creates an identity matrix of the order n by n,
  
 
|-
 
|-
6:24
+
06:24
 
| the function zeros()  which creates a matrix of the order m by n with all zeros,
 
| the function zeros()  which creates a matrix of the order m by n with all zeros,
  
 
|-
 
|-
6:30
+
06:30
 
| the function  zeros like function()  which creates a matrix with zeros with the shape of the matrix passed,
 
| the function  zeros like function()  which creates a matrix with zeros with the shape of the matrix passed,
  
 
|-
 
|-
6:39
+
06:39
 
| the function  ones()  which creates a matrix of order m by n with all ones,
 
| the function  ones()  which creates a matrix of order m by n with all ones,
  
 
|-
 
|-
6:47
+
06:47
 
| the function ones underscore like()  which creates a matrix with ones with the shape of the matrix passed;
 
| the function ones underscore like()  which creates a matrix with ones with the shape of the matrix passed;
  
 
|-
 
|-
6:53
+
06:53
 
| all these functions can also be used with matrices.
 
| all these functions can also be used with matrices.
  
 
|-
 
|-
| 6:57
+
| 06:57
 
|So now Let us see, how to find out the transpose of a matrix we can do,
 
|So now Let us see, how to find out the transpose of a matrix we can do,
  
 
|-
 
|-
|7:03
+
|07:03
 
|Type print m4
 
|Type print m4
 
  m4 dot T
 
  m4 dot T
  
 
|-
 
|-
| 7:14
+
| 07:14
 
| As you saw, Matrix name dot capital T will give the transpose of a matrix
 
| As you saw, Matrix name dot capital T will give the transpose of a matrix
  
 
|-
 
|-
| 7:21
+
| 07:21
 
|Pause the video here, try out the following exercise and resume the video.
 
|Pause the video here, try out the following exercise and resume the video.
  
 
|-
 
|-
| 7:26
+
| 07:26
 
| Find out the Frobenius norm of inverse of a 4 by 4 matrix, the matrix being,
 
| Find out the Frobenius norm of inverse of a 4 by 4 matrix, the matrix being,
  
 
|-
 
|-
|7:33  
+
|07:33  
 
|m5 = arrange  within bracket 1 comma 17 dot reshape to 4 comma 4
 
|m5 = arrange  within bracket 1 comma 17 dot reshape to 4 comma 4
  
 
|-
 
|-
| 7:44
+
| 07:44
 
|The Frobenius norm of a matrix is defined as, the square root of the sum of the absolute squares of its elements
 
|The Frobenius norm of a matrix is defined as, the square root of the sum of the absolute squares of its elements
  
 
|-
 
|-
| 7:54
+
| 07:54
 
| Switch to terminal for the solution
 
| Switch to terminal for the solution
  
 
|-
 
|-
| 7:58  
+
| 07:58  
 
| Let us create the matrix m5 by using the data provided in the question
 
| Let us create the matrix m5 by using the data provided in the question
  
 
|-
 
|-
|8:03
+
|08:03
 
|Type m5 = arrange  within bracket 1 comma 17.reshape within square bracket 4 comma 4
 
|Type m5 = arrange  within bracket 1 comma 17.reshape within square bracket 4 comma 4
 
Then type  print m5
 
Then type  print m5
  
 
|-
 
|-
8:20
+
08:20
 
| The inverse of a matrix A, A raise to minus one, is also called the reciprocal matrix, such that A multiplied by A inverse will give 1.
 
| The inverse of a matrix A, A raise to minus one, is also called the reciprocal matrix, such that A multiplied by A inverse will give 1.
  
 
|-
 
|-
8:33
+
08:33
 
| The Frobenius norm of a matrix is defined as square root of sum of squares of elements in the matrix.  
 
| The Frobenius norm of a matrix is defined as square root of sum of squares of elements in the matrix.  
  
 
|-
 
|-
8:41
+
08:41
 
| The inverse of a matrix can be found using the function  inv(A) .
 
| The inverse of a matrix can be found using the function  inv(A) .
  
 
|-
 
|-
| 8:47
+
| 08:47
 
| Type in the terminal im5 = inv  within bracket m5  
 
| Type in the terminal im5 = inv  within bracket m5  
  
 
|-
 
|-
| 8:57
+
| 08:57
 
| And the Frobenius norm of the matrix  im5  can be found out as,
 
| And the Frobenius norm of the matrix  im5  can be found out as,
 
  sum = 0
 
  sum = 0
Line 274: Line 275:
  
 
|-
 
|-
| 9:52
+
| 09:52
 
| Thus we have successfully obtained the Frobenius norm of the matrix m5
 
| Thus we have successfully obtained the Frobenius norm of the matrix m5
  
 
|-
 
|-
9:58
+
09:58
 
| Pause the video here, try out the following exercise and resume the video.
 
| Pause the video here, try out the following exercise and resume the video.
  

Revision as of 14:48, 10 July 2014

Time Narration
00:01 Hello friends and welcome to the tutorial on 'Matrices'.
00:05 At the end of this tutorial, you will be able to,
  1. Create matrices using data.
  2. Create matrices from lists.
  3. Do basic matrix operations like addition,multiplication.
  4. Perform operations to find out the -- - inverse of a matrix. - determinant of a matrix. - eigen values and eigen vectors of a matrix. - norm of a matrix. - singular value decomposition of a matrix.
00:31 Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with lists", "Getting started with arrays", "Accessing parts of arrays".
00:42 Let us start our ipython interpreter by pylab loaded
00:47 Type ipython hypen pylab in terminal
00:52 All matrix operations are done using arrays.
00:55 Thus all the operations on arrays are valid on matrices only.
01:00 A matrix may be created as,
01:02 Type in the terminal m1 = array within bracket and square bracket 1 comma 2 comma 3 comma 4 hit enter.
01:16 Using the method shape , we can find out the shape or size of the matrix,
01:20 Type m1 dot shape and hit enter
01:27 We can see the output
01:29 Since it is a one row four column matrix it returned a tuple, one by four.
01:46 A list can also be converted to a matrix as follows,


01:50 Type l1 = within square bracket square bracket 1 comma 2 comma 3 comma 4 comma in another square bracket 5 comma 6 comma 7 comma 8
Type m2 = array within bracket 11
02:28 Sorry you have to type l1 array .
02:35 Pause the video here, try out the following exercise and resume the video.
02:43 Create a two dimensional matrix m3 of order 2 by 4 with elements 5, 6, 7, 8, 9, 10, 11, 12.
02:51 Switch to terminal for solution.
02:54 m3 can be created as,
02:56 Type m3 = array within closing bracket inside square bracket square bracket 5 comma 6 comma 7 comma 8 comma in another square bracket 9 comma 10 comma 11 comma 12
03:31 Let us now move to matrix operations.
03:34 We can do matrix addition and subtraction easily.
03:37 m3+m2 does element by element addition, that is matrix addition.
03:43 Note that both the matrices should be of the same order.
03:47 Type m3+m2 and hit enter so you can see the output.
03:55 Similarly,m3 minus m2 does matrix subtraction, that is element by element subtraction.
04:02 You can try out by typing m3 minus m2
04:09 Now let us try,matrix multiplication
04:13 Type m3 star m2
04:20 Note that in arrays m3 star m2 does element wise multiplication and not matrix multiplication,
04:28 Matrix multiplication in matrices are done using the function dot()
04:37 Type dot within bracket m3 comma m2 and hit enter.
04:47 So we can see error value show in the command.
04:50 Due to size mismatch, the multiplication could not be done and it returned an error.
04:56 Now let us see an example for matrix multiplication.
05:00 For doing matrix multiplication we need to have two matrices of the order n by m and m by r and the resulting matrix will be of the order n by r.
05:11 Thus let us first create two matrices which are compatible for multiplication.
05:16 Type m1.shape and hit enter
05:24 matrix m1 is of the shape one by four,
05:28 let us create another one, of the order four by two,
05:33 So type m4 = array in closing bracket within square bracket within square bracket 1 comma 2 comma within square bracket 3 comma 4 comma within square bracket 5 comma 6 within square bracket comma within square bracket 7 comma 8
Type dot within bracket m1 comma  m4
06:10 Thus the dot() function is used for matrix multiplication.
06:15 As we already learnt in arrays, the function identity() which creates an identity matrix of the order n by n,
06:24 the function zeros() which creates a matrix of the order m by n with all zeros,
06:30 the function zeros like function() which creates a matrix with zeros with the shape of the matrix passed,
06:39 the function ones() which creates a matrix of order m by n with all ones,
06:47 the function ones underscore like() which creates a matrix with ones with the shape of the matrix passed;
06:53 all these functions can also be used with matrices.
06:57 So now Let us see, how to find out the transpose of a matrix we can do,
07:03 Type print m4
m4 dot T
07:14 As you saw, Matrix name dot capital T will give the transpose of a matrix
07:21 Pause the video here, try out the following exercise and resume the video.
07:26 Find out the Frobenius norm of inverse of a 4 by 4 matrix, the matrix being,
07:33 m5 = arrange within bracket 1 comma 17 dot reshape to 4 comma 4
07:44 The Frobenius norm of a matrix is defined as, the square root of the sum of the absolute squares of its elements
07:54 Switch to terminal for the solution
07:58 Let us create the matrix m5 by using the data provided in the question
08:03 Type m5 = arrange within bracket 1 comma 17.reshape within square bracket 4 comma 4

Then type print m5

08:20 The inverse of a matrix A, A raise to minus one, is also called the reciprocal matrix, such that A multiplied by A inverse will give 1.
08:33 The Frobenius norm of a matrix is defined as square root of sum of squares of elements in the matrix.
08:41 The inverse of a matrix can be found using the function inv(A) .
08:47 Type in the terminal im5 = inv within bracket m5
08:57 And the Frobenius norm of the matrix im5 can be found out as,
sum = 0

for each in im5 dot flatten function():

   sum plus= each star each

print sqrt within bracket sum

09:52 Thus we have successfully obtained the Frobenius norm of the matrix m5
09:58 Pause the video here, try out the following exercise and resume the video.
10:04 Find out the infinity norm of the matrix im5.
10:08 The infinity norm of a matrix is defined as the maximum value of sum of the absolute of elements in each row.
10:16 Switch to terminal for the solution
10:20 sum underscore rows = square bracket

for i in im5 colon

   sum underscore rows.append  within bracket abs  within bracket i.sum()

print max within square bracket sum underscore rows

11:01 Well! to find out the Frobenius norm and Infinity norm we have an even easier method, and let us see that now.
11:10 The norm of a matrix can be found out using the method norm().
11:19 In order to find out the Frobenius norm of the matrix im5, we do,
11:25 In the terminal type norm within bracket im5 and hit enter
11:34 And to find out the Infinity norm of the matrix im5, we do,
11:39 norm within bracket im5,ord=inf
11:51 This is easier when compared to the code we wrote.
11:55 Read the documentation of norm to read up more about ord and the possible type of norms the norm function produces.
12:04 Now let us find out the determinant of a the matrix m5.
12:11 The determinant of a square matrix can be obtained by using the function det() and the determinant of m5 can be found out as,
12:20 So type det within bracket m5
12:26 Hence we get the determinant.
12:29 Let us now move on to eigen vectors and eigen values
12:34 The eigen values and eigen vector of a square matrix can be computed using the function eig() and eigvals().
12:46 Let us find out the eigen values and eigen vectors of the matrix m5. We find them as,
12:53 Typing eig within bracket m5 in the terminal.
13:02 Note that it returned a tuple of two matrices.
13:06 The first element in the tuple are the eigen values and the second element in the tuple are the eigen vectors.
13:11 Thus eigen values are given by,eig within bracket m5 and in square bracket 0
13:30 and the eigen vectors are given by,eig within bracket m5 within square bracket 1
13:44 The eigen values can also be computed using the function eigvals() as,
13:50 Typing on the terminal eigvals within bracket m5
13:58 Now let us learn how to do the singular value decomposition or S V D of a matrix.
14:06 Suppose M is an m (cross) in matrix, whose entries come from the field K, which is either the field of real numbers or the field of complex numbers.
14:18 Then there exists a factorization of the form

M = U Sigma V star

14:25 where U is an (m by m) unitary matrix over K, the matrix Sigma is an (m by n) diagonal matrix and the non-negative real numbers on the diagonal, and V* is an (n by n) unitary matrix over K,which denotes the conjugate transpose of V.
14:53 Such a factorization is called the singular-value decomposition of M.
14:58 The SVD of matrix m5 can be found as
15:01 So now open the terminal and type svd within brackets m5
15:09 Notice that it returned a tuple of 3 elements.
15:12 The first one U the next one Sigma and the third one V star
15:19 This brings us to the end of the end of this tutorial.
15:22 In this tutorial, we have learnt to, 1. Create matrices using arrays.
15:25 2. Add,subtract and multiply the elements of matrix.
15:28 3. Find out the inverse of a matrix,using the function inv() .
15:32 4. Use the function det() to find the determinant of a matrix.
15:36 5. Calculate the norm of a matrix using the for loop and also using the function norm()
15:43 6. Finding out the eigen vectors and eigen values of a matrix, using functions eig() and eigvals()
15:50 7. Calculate singular value decomposition(SVD) of a matrix using the function svd() .
15:58 Here are some self assessment questions for you to solve
16:01 1. A and B are two array objects. Element wise multiplication in matrices are done by,
   A * B
  multiply within bracket A comma  B
  dot within bracket A comma  B 
  element underscore multiply within bracket A comma B  
16:19 2. eig within bracket A within square bracket 1 and eigvals within bracket A are the same.Is it True or False?
16:31 3. norm within bracket A comma ord= within is equal to fro is the same as norm within bracket A . Is it True or False?
16:43 Look at the answers,
16:47 1. Element wise multiplication between two matrices, A and B is done as, A into B
16:53 2. False. eig within bracket A within square bracket 0and eigvals within bracket A are same, that is both will give the eigen values of matrix A.
17:06 3. norm within bracket A comma ord=is equal to fro and norm(A) are same, since the order=is equal to fro stands for Frobenius norm.
17:22 Hence answer is true.
17:26 Hope you have enjoyed this tutorial and found it useful.
17:30 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha