Python-3.4.3/C3/Advanced-Matrix-Operations/English

From Script | Spoken-Tutorial
Revision as of 11:06, 21 November 2018 by Nancyvarkey (Talk | contribs)

Jump to: navigation, search

Title of script: Advanced Matrix Operations

Author: Puneeth, Thirumalesh H S, Arun KP

Keywords: Python, IPython, array, matrices, norm, svd, video tutorial


Visual Cue
Narration
Show Slide title Welcome to the spoken tutorial on Advanced matrix operations.
Show Slide

Objectives


In this tutorial, you will learn to,
  • find Frobenius and infinity norm of a matrix
  • find singular value decomposition of a matrix.
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3 and
  • IPython 5.1.0
Show Slide

Pre-requisites


To practise this tutorial, you should know about
  • Lists, arrays and accessing parts of arrays and
  • performing basic matrix operations

If not, see the relevant Python tutorials on this website.

Show Slide


flatten()

First we will see about flatten function.
  • flatten() function returns a copy of the array, collapsed into one dimension.
  • It can be used to convert a multidimensional matrix into a single dimension matrix
Open terminal Let us start ipython.


Open the terminal.

Type,

ipython3

Type, ipython3 and press Enter.


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

Type,

from numpy import asmatrix,arange


a = asmatrix(arange(1,10).reshape(3,3))

Now let us see how to create arrays


Type from numpy import asmatrix,arange


a is equal to asmatrix inside brackets arange inside brackets 1 comma 10 dot reshape inside brackets 3 comma 3

Type a


a.flatten()


Highlight numpy import asmatrix,arange

Then type, a


Now type, a dot flatten open and close brackets


First we imported arange function from numpy module.

Highlight (arange(1,10).reshape(3,3)) Here, we can see 3 by 3 matrix is converted into one dimensional matrix.
Show Slide

Frobenius norm of a matrix

Next we will see about frobenius norm.

It is defined as the square root of the sum of the absolute squares of its elements.

Pause the video.


Try this exercise and then resume the video.

Show Slide

Assignment 1: Frobenius norm

Find out the frobenius norm of the inverse of the given 4 by 4 matrix.
Switch to terminal Switch back to the terminal for the solution.
Type,

m = asmatrix(arange(1,17).reshape(4,4))


Highlight asmatrix(arange(1,17).reshape(4,4))

Type

m is equal to asmatrix inside brackets arange inside brackets 1 comma 17 dot reshape inside brackets 4 comma 4


Here, we have used asmatrix, arange and reshape functions.


We created a matrix of size 4 by 4 containing elements from 1 to 16.

Type,

m[0,1] = 0

m[1,3] =0

Now type,

m inside square brackets 0 comma 1 is equal to 0

m inside square brackets 1 comma 3 is equal to 0

Type m Then type, m


We changed the value of element at row 0 column 1 and row 1 column 3 to 0.

Type,

from numpy.linalg import inv, norm

im = inv(m)

norm(im)


Highlight numpy.linalg import inv, norm

In order to find out the Frobenius norm of the inverse of matrix m, type as shown.


norm function is available in numpy.linalg module.

Show Slide

Infinity norm

Next, we will see about infinity norm of a matrix.


It is defined as the maximum value of sum of the absolute value of elements in each row.

Pause the video.


Try this exercise and then resume the video.

Slide Assignment 2: Infinity norm Find the infinity norm of the matrix im.
Switch to terminal Switch back to the terminal for the solution.
Type,

from numpy import infnorm(im,ord=inf)

To find out the Infinity norm of the matrix im, type as shown.


Here value for ord parameter is passed as inf to calculate infinity norm.

Type, norm? To know more about norms type norm question mark


Press q to exit.

Show Slide

Singular value decomposition

Next we will see about singular value decomposition.


In linear algebra, the singular value decomposition is factorization of real or complex matrix.

Type,

from numpy import matrix

from numpy.linalg import svd

m1 = matrix([[3,2,2],[2,3,-2]])

U,sigma,V_conjugate = svd(m1)


The SVD of matrix m1 can be found using svd function available in the numpy.linalg module.


Type as shown.

svd returns a tuple of 3 elements.


We have unpacked these values into variable U, sigma and V underscore conjugate.

Type,

U

sigma

V_conjugate

Type, Capital U


Type, sigma


Type, Capital V underscore conjugate

Narration only We can validate the singular value decomposition by comparing the product of:

U, sigma and V underscore conjugate with m1


sigma is a one dimensional array which contains only the diagonal elements of the matrix.

Type,

from numpy import diag,allclose

from numpy.matlib import zeros

smat = zeros((2,3))

Type as shown.


We first convert this array to a matrix.

Type,

smat

Type smat


smat is a 2 by 3 zero matrix

Type,

smat[:2, :2] = diag(sigma)

Now type,

smat inside square brackets colon 2 comma colon 2 is equal to diag inside brackets sigma

Type smat Then type smat


This replaces values at row 0 column 0 and row 1 column 1 in smat with values from sigma.

Type

allclose(m1, U * smat * V_conjugate)

Type as shown.


It returns True.


It means elements in m1 and in product of U, sigma and V underscore conjugate are equal.

Show Slide

Summary


This brings us to the end of this tutorial. Let us summarize.


In this tutorial, we have learnt to,

  • Calculate the norm of a matrix using the function norm()
  • Calculate SVD of a matrix using the function svd()
Show Slide

Self assessment questions slide.


Here is a self assessment question for you to solve

1. norm inside brackets A comma ord is equal to inside single quotes fro is the same as norm inside brackets A

True or False.

Show Slide

Solution of self assessment questions on slide

And the answer is True since the order is equal to inside single quotes fro stands for Frobenius norm.
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.
Slide TBC FOSSEE team coordinates the TBC project.
Show Slide

Acknowledgment

http://spoken-tutorial.org

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.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Priyacst