Basics of Linear Algebra

Meharaj Ul Mahmmud
3 min readMay 2, 2021

--

Linear Algebra Operations Using Raw Python.

Photo by Antoine Dautry on Unsplash

Being a Data Science major, I have been comprehending Linear Algebra and Matrix Operations for a long time. This whole time, I have been using the most popular Numpy library. Linear Algebra is one of the most important topics in Data Science. In this post, I will try to implement some of the basic Numpy Linear Algebra functions using raw Python code.

These basic operations are at the root of every machine learning and deep learning model. Therefore we should have a clear concept of these functions.

Zeros Matrix

This function will return us a matrix with all the ZEROs.

Ones Matrix

This function will return us a matrix with only ONEs.

Identity Matrix

This will give us a n by n square matrix with ones in the main diagonal and zeros everywhere else.

Matrix Addition

This function will return us the addition of two matrices. Matrix addition is the operation of adding two matrices by adding the corresponding entries together.

Matrix Subtraction

This function will subtract the second matrix from the first one. Subtraction of matrix is possible by subtracting the element of another matrix if they have the same order.

Matrix Multiplication

This matrix will multiply a 2D matrix by a 2D matrix.

There are a few things to keep in mind.

  1. Order matters now. AB != BA.
  2. Matrices can be multiplied if the number of columns in the 1st equals the number of rows in the 2nd.
  3. Multiplication is the dot product of rows and columns. Rows of the 1st matrix with columns of the 2nd.
Matrix Multiplication

Matrix Transpose

This will transpose a given matrix. The transpose of a matrix is simply a flipped version of the original matrix. We can transpose a matrix by switching its rows with its columns.

Matrix Inverse

Please refer to this link for Inverse of a Matrix.

Summary

So, I basically tried to implement the basic operations of Linear Algebra that are used in Machine Learning. I hope you got to know a little bit about how to multiply, add and subtract these mathematical objects. On top of that, you have learned what inverse and transpose Matrices are and what you can do with them. Although there are also other parts of Linear Algebra used in Machine Learning, this post will help you learn the most important concepts.

--

--