0% found this document useful (0 votes)
2 views2 pages

numpy

The document provides an overview of using the numpy package for linear algebra in Python, including how to define matrices and vectors, perform transposition, and execute basic matrix operations. It also covers normalization and diagonalization of matrices with examples. Key functions and their usage are illustrated through code snippets.

Uploaded by

sriletsoin03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

numpy

The document provides an overview of using the numpy package for linear algebra in Python, including how to define matrices and vectors, perform transposition, and execute basic matrix operations. It also covers normalization and diagonalization of matrices with examples. Key functions and their usage are illustrated through code snippets.

Uploaded by

sriletsoin03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Ling 5702: Lecture Notes 6

Numpy
We can use linear algebra in Python programs using the numpy package:

import numpy

6.1 Matrix and vector definition


Matrices can be defined from Python lists of lists:

hmatrix-expri → numpy.matrix ( hlist-expri )

For example:

print ( numpy.matrix( [[1,2],[3,4]] ) )

prints:

[[1 2]
[3 4]]

Transpose (row) vectors can be defined from simple lists of numbers. For example:

print ( numpy.matrix( [1,2] ) )

prints:

[[1 2]]

6.2 Transposition
Matrices can be transposed by accessing a .T member:

hmatrix-expri → hmatrix-expri .T

For example:

print ( numpy.matrix( [[1,2],[3,4]] ).T )

prints:

[[1 3]
[2 4]]

1
6.3 Matrix operations
Matrices can be added/subtracted/multiplied/divided using the usual operators:
hmatrix-expri → hmatrix/num-expri + hmatrix/num-expri
hmatrix-expri → hmatrix/num-expri - hmatrix/num-expri
hmatrix-expri → hmatrix/num-expri * hmatrix/num-expri
hmatrix-expri → hmatrix/num-expri / hmatrix/num-expri
For example:
print ( numpy.matrix( [[1,2],[3,4]] ) * numpy.matrix( [[1,2],[3,4]] ) )
prints:
[[ 7 10]
[15 22]]

6.4 Normalization
Matrices can be normalized:
hnum-expri → numpy.linalg.norm ( hmatrix-expri, hnum-expri )
For example:
v = numpy.matrix( [1,1] ).T
print ( numpy.linalg.norm(v,2) )
prints:
1.41421356237

6.5 Diagonalization
Matrices can be diagonalized:
hmatrix-expri → numpy.diagflat ( hmatrix-expri )
For example:
v = numpy.matrix( [1,1] ).T
print ( numpy.diagflat(v) )
prints:
[[1 0]
[0 1]]

You might also like