Math for Data Science — Lecture 01 Basic Matrix Operations via Python

Aakash Goel
Math for data science
3 min readNov 29, 2021

In this lecture, we will study Matrix Operations (Initialization, Vector, Multiplication, Transpose, Special Matrix) via Python.

Matrix Definition

Image 01: Matrix Definition

Matrix Notation

Image 02: Matrix Notation

Matrix Initialization

import numpy as np
print(np.zeros((2,3)))
print(np.random.randint(low = 5,high=12, size=(2, 3)))
A = np.matrix([[1, 2], [3, 4], [5,6]])
print("Type of Matrix A:{}, having {} rows and {} columns".format(type(A),A.shape[0],A.shape[1]))
Image 03: Matrix Initialization — code

Vectors

Image 04: Vector Definition
  • Row vector is having 1 row and N columns
  • Column vector is having N rows and 1 column
Image 05: Row and Column Vector — code
Image 06: Convert Row vector to Column Vector — code

Matrix Equality

Image 07: Check Matrix Equality — code

Matrix Multiplication

Image 08: Matrix Multiplication Shape

In below Image 09, matrix multiplication is shown both via python code and hand as well.

Image 09: Matrix Multiplication calculation by hand
  • Matrix multiplication isn’t commutative (AB != BA)
Image 10: Matrix Not Commutative — code

Matrix Transpose

Image 11: Matrix Transpose — code

Rules of Matrix Transpose

Image 12.1: Rules of Matrix transpose — code
Image 12.2: Rules of Matrix transpose — code

Special Matrix

Image 13: Special Matrices Definition
Image 14: Check if Special Matrix — code
Image 15: Caller Function (Special Matrix) — code

Reduce size of Sparse Matrix using CSR (Compressed Sparse Row)

Image 16: Dense and Sparse Matrix storage comparison — code

Properties of Determinants

Image 17: Properties of Determinants

WORK IN PROGRESS !! Jupyter Notebook/.py will be uploaded soon..

Our Next lecture is about using Elementary row operations to find Row Echelon Form (REF), Reduced Row Echelon Form (RREF) and Rank of Matrix (https://medium.com/math-for-data-science/math-for-data-science-lecture-02-elementary-row-operations-via-python-46885a33a84c).

Meanwhile, Please feel free to clap if you liked the article. Also, please subscribe to my YouTube Channel https://www.youtube.com/channel/UC4yh4xPxRP0-bLG_ldnLCHA?sub_confirmation=1

--

--