Python-3.4.3/C3/Basic-Matrix-Operations/English
Title of script: Basic Matrix Operations
Author: Puneeth, Thirumalesh H S, Arun KP
Keywords: Python, IPython, matrices, determinant, reshape, arange, eigen values, eigen vectors, transpose of matrix
|
|
Show Slide title | Welcome to the spoken tutorial on Basic Matrix Operations. |
Show Slide
Objectives
|
In this tutorial, you will learn to,
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisites
|
To practise this tutorial, you should have basic knowledge about
If not, see the relevant Python tutorials on this website. |
Slide: | * In python, we create a matrix using numpy matrix class.
|
Type,
ipython3 |
Let us start ipython.
|
Type ipython3
|
Type ipython3 and press Enter.
|
Type,
m1 = matrix([1,2,3,4]) m1 Point to the output |
Let us create a matrix m1.
from numpy import matrix
m1 is equal to matrix inside brackets inside square brackets 1 comma 2 comma 3 comma 4
m1
|
Type,
m1.shape
|
This can be verified by typing
m1.shape
|
Type,
l1 = [[1,2,3,4],[5,6,7,8]] m2 = matrix(l1) print(m2)
|
A list can also be converted to a matrix as follows,
|
Slide:asmatrix | * To convert an array to a matrix, use the asmatrix method in numpy module.
|
Highlight according to narration
from numpy import asmatrix,arange m2_array = asmatrix(arange(1,9).reshape(2,4)) m2_array |
Type as shown.
|
Pause the video.
| |
Show Slide
Assignment 1 |
Create a two dimensional matrix m3 of shape 2 by 4 with the elements 5, 6, 7, 8, 9, 10, 11, 12.
|
Switch to the terminal | Switch back to the terminal for the solution. |
Type,
m3 = asmatrix(arange(5,13).reshape(2,4))
|
Type,
m3 is equal to asmatrix inside brackets arange inside brackets 5 comma 13 dot reshape inside brackets 2 comma 4
You can see the required output. |
Type,
m3 + m2 |
Next let us see some matrix operations.
|
Type,
m3 - m2 |
Similarly, type m3 minus m2
|
Type,
6.5 * m2 |
Now we can multiply a scalar i.e a number by a matrix as shown. |
Type,
|
Next we will check the size of m2 by typing,
m2.shape.
|
Type,
m4 = asmatrix(arange(1,9).reshape(4,2)) |
Let us create another matrix, of the order 4 by 2.
m4 is equal to asmatrix inside brackets arange inside brackets 1 comma 9 dot reshape inside brackets 4 comma 2 |
Type
m4.shape |
Now to check the shape, type,
m4.shape
|
Type,
m2 * m4 Highlight the output |
The multiplication operator asterisk is used for matrix multiplication.
m2 asterisk m4
|
Type,
print (m4)
|
Let us now see, how to find out the transpose of a matrix.
print inside brackets m4 |
Type,
print(m4.T)
|
Now type,
print inside brackets m4 dot capital T
|
Show Slide:Determinant of a matrix | The determinant of a square matrix is obtained by using the function det() in numpy.linalg module. |
Pause the video.
| |
Show Slide: Exercise | Find out the determinant of this 3 by 3 matrix. |
Switch to the terminal for solution. | Switch to the terminal for the solution. |
Type,
from numpy.linalg import det m5 = matrix([[2,-3,1],[2,0,-1],[1,4,5]]) det(m5) |
Type as shown.
det inside brackets m5
|
Show Slide
Inverse of a matrix |
The inverse of a square matrix can be obtained using inv() function in numpy.linalg module. |
Type,
from numpy.linalg import inv im5 = inv(m5)
im5 |
Let us find the inverse of the matrix m5.
im5 |
Type,
from numpy import eye,allclose allclose(im5 * m5, asmatrix(eye(3)))
eye? |
Type,
from numpy import eye,allclose
allclose inside brackets im5 asterisk m5 comma asmatrix inside brackets eye inside brackets 3
eye question mark
|
Show Slide
eigenvectors and eigenvalues
|
Let us now move onto eigenvectors and eigenvalues
eig and eigvals functions are present in numpy.linalg module. |
Type,
from numpy import diag from numpy.linalg import eig m6=asmatrix(diag((1, 2, 3)))
eig(m6)
(array([1., 2., 3.]), matrix([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]))
|
Let us find out the eigenvalues and eigenvectors of the matrix m6.
|
Type,
eig_value = eig(m6)[0] eig_value |
To get eigenvalues type,eig underscore value is equal to eig inside brackets m6 inside square brackets 0
eig underscore value
|
Type,
eig_vector = eig(m6)[1] eig_vector |
To get eigenvectors type,eig underscore vector is equal to eig inside brackets m6 inside square brackets 1
eig underscore vector
|
Type,
from numpy.linalg import eigvals eig_value1 = eigvals(m6)
eig_value1
|
The eigenvalues can also be computed using eigvals() function.
eig underscore value1 You can see that, eig underscore value and eig underscore value1 are same. |
Show Slide
Summary
|
This brings us to the end of this tutorial. Let us summarize.
|
Show Slide
Self assessment questions
|
Here are some self assessment questions for you to solve
|
Show Slide 13
Solution of self assessment questions
|
And the answers,
|
Show Slide Forum | Please post your timed queries in this forum. |
Show Slide Fossee Forum | Please post your general queries on Python in this forum. |
Show slide TBC | FOSSEE team coordinates the TBC project. |
Show Slide
Acknowledgment |
Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
Previous slide | This is Priya from IIT Bombay signing off.
Thanks for watching. |