Linear Algebra using Python - The Applied Approach
Linear Algebra is to organize numbers into vector and matrices in order to implement computations. Linear Algebra powers Statistics, Machine Learning, Deep Learning, Artificial Intelligence, Telecommunication, Signal Processing and Computer Graphics etc, So any computation done by computer is done by linear algebra.
All the codes used in this article can be found here
Index
- NumPy Basics
- Matplotlib basics
- Visualizing Matrix using Python
- Vectors & its Operations using Python
- Matrices & its Operations using Python
Lets start with some NumPy basics
Numerical Python is a python package, also known as NumPy which is widely used for working with arrays. It also has functions for working in domain of linear algebra, fourier transform, and matrices.
- Calculating mean of a list of numbers using NumPy.
2. Creating a NumPy array of linearly spaced numbers.
Using a function linspace we can generate a numpy array which contains list of numbers between 2 numbers which we provide as arguments.
It produces a list having 9 linearly spaced numbers between 2 to 20.
Hands on with Matplotlib basics
Matplotlib is a graph plotting library in python that is used to visualize data in python.
- Plotting a point (red) at x = 1, y = 3
Note :- Here 3rd Argument ‘ro’ means plot should look like red circle.
2. Plotting a list of numbers : [-9,9]
3. Multiple plots on same graph
4. Point to point Line plot
Linear algebra is a study of lines, planes, vectors, matrices and their mappings with each other. We perform certain mathematical operations on matrices in order to get our desired result. We can think of linear algebra and matrices as simple linear transforms which are happening on the coordinate system.
Let us look at creating a matrix using python and visualize it using different colors.
Visualizing Matrix using Python
Creating a matrix with random numbers using NumPy.
Now, visualize this Matrix using Matplotlib
This is a matrix which is an image, here each of this square is corresponds to one item in the matrix, and color of each square corresponds to the value of element in the matrix.
Vectors & its Operations using Python
vector is a ordered list of numbers. It has values and directions.
- Geometric Interpretations
First we look at 2-Dimensional Vector and plotting it on coordinate axis. Plotting vector [4,5]
Now let us look at 3-Dimensional Vector and plotting it on 3-D coordinate axis. Plotting vector [5,-4,4]
2. Vector and Scalar multiplication
vector [2,3] is multiplied with three scalar values.
- As we can see the blue square represents original vec2 plotted on axis.
- We are than multiplying vec2 with 3 & 0.5 & -2
- and results will be show as red circle symbol represents the vec2*3, black pentagon symbol on line represents the vec2*0.5, and green star represents the vec2*-2.
3. Vector and Vector Addition
In this we add two vectors and get the resultant vector.
4. Vector and Vector Multiplication (Dot Product)
Two vectors are multiplied only if they are of same dimensionality. i.e. vector 5x1 can be multiplied with 5x1, but 5x1 cannot be multiplied with 3x1.
Here we are going to use dot() function of NumPy to multiply two vectors.
Correct dimensionality vector multiplication :
These are some of the common operations of vectors which are used extensively while doing linear transformations.
Matrices & its Operations using Python
A matrix is a rectangular array or a table of numbers arranged in row and columns. The count of these row and columns of a matrix is termed as order of matrix. i.e. 3x3, 5x7
Addition, subtraction and multiplication are the basic operations on the matrix. We have some constraints before we could add or subtract two matrices.
- For addition/subtraction matrices they must be of identical order.
- For multiplication of two matrices, the number of columns in the first matrix equals the number of rows in the second matrix.
Some Matrix that can be created using NumPy functions are :
a. Identity matrix :- Main diagonal elements are 1 and all other elements are 0.
b. Zeros matrix :- All elements of the matrix are zeros.
c. Full matrix :- All elements of the matrix have same values.
d. Diagonal matrix :- A matrix A_i,j is diagonal if its entries are all zeros except on the diagonal (when i=j).
Creating your own matrix using NumPy :
Now let’s see some common operations on matrices :
Transpose of Matrix
It flips the row elements to column elements and vice versa.
Eg :-
Now this transpose concept can be used to explain symmetric matrix, The matrix A is symmetric if it is equal to its transpose.
To get transpose of a matrix we need to write MatrixName.T
Matrix Multiplication
For multiplication of two matrix, the number of columns in the first matrix equals the number of rows in the second matrix.
By following below operation we can perform matrix multiplication algebraically.
Now let us look at how we can do it by using python
These are the two ways by which we can multiply two matrices using python libraries.
Inverse of Matrix
A matrix has an inverse possible only if
A x A^-1 = A^-1 x A = I (Identity Matrix)
Let us look at linalg module of NumPy and compute the Inverse of matrix and Determinant of Matrix.
In the above example, The matrix A is a matrix of some random integers between 1 to 10 and order of matrix is 3x3. Ainverse and Determinant of matrix A are computed using linalg module of NumPy. To verify the Inverse Property, I have done matrix multiplication of A with Ainverse which is resulting in Identity Matrix.
Some more operations of matrix that can be performed using Python and Numpy
Here I have considered np as NumPy and A as matrix.
- Rank of Matrix => np.linalg.matrix_rank(A)
- Trace of Matrix => np.trace(A)
- Compute Matrix A raised to power (let say 4)=> np.linalg.matrix_power(A, 4)
- Matrix eigenvalues => val1, val2 = np.eig(A)
- Solve a linear matrix equation => np.linalg.solve(A, b) #b is the compute value.
- Singular Value Decomposition => np.linalg.svd()
- Compute the qr factorization of a matrix => np.linalg.qr()
- Compute the condition number of a matrix => np.linalg.cond()
- Raise a square matrix to the (integer) power n => np.linalg.matrix_power()
Thank You for reading this article! More articles on the way.
If you happen to find any mistakes, please let me know so that I could correct them.
Author Details :- Lakshy Gupta | Linkedin