Post

Useful Matrix Operations for AI,ML and Data Science and implemented using Python

Useful Matrix Operations for AI,ML and Data Science and implemented using Python

matrix

Useful Matrix Operations for AI,ML and Data Science

Matrix operations are fundamental in data science, machine learning, and numerical computing.

They are widely used in linear algebra, statistics, and optimization problems. Below are some of the most important matrix operations with mathematical examples and Python implementations.

1 Creating/ Initializing a matrix

1
2
3
4
5
6
7
8
9
import numpy as np

# creating a 2x2 matrix
A = np.array([[1, 2], [3, 4]])

print("2x2 Matrix:\n", A)

# use np.array() to define a 2D list (nested lists) as a matrix.
# Unlike Python lists, NumPy arrays allow mathematical operations to be performed element-wise.
1
2
3
2x2 Matrix:
 [[1 2]
 [3 4]]

2 Creating a null matrix

1
2
3
4
# creating a 2x2 null matrix
A = np.array([[0, 0], [0, 0]])

print("2x2 null Matrix:\n", A)
1
2
3
2x2 null Matrix:
 [[0 0]
 [0 0]]

3 Creating/initializing a mxn null matrix programitacilly

1
2
3
4
5
6
7
8
rows = 2     
cols = 3

matrix1 = [[0 for i in range(cols)] for j in range(rows)] 

print("2x3 null Matrix:\n", matrix1)

1
2
2x3 null Matrix:
 [[0, 0, 0], [0, 0, 0]]
1
2
matrix2=np.array(matrix1)
print(matrix2)
1
2
[[0 0 0]
 [0 0 0]]
1
2
3
4
5
6
matrix3=matrix1 * 2
print(matrix3)

# In Python, multiplying a list by an integer does not perform element-wise multiplication.
# Instead, it repeats the entire list.
# list1 * 2 creates a new list that repeats list1 
1
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
1
2
matrix3=matrix2 * 2
print(matrix3)
1
2
[[0 0 0]
 [0 0 0]]

Final program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# so we have to do
# creating/initializing a mxn null matrix programitacilly  

rows = 2     
cols = 3

matrix1 = np.array([[0 for i in range(cols)] for j in range(rows)])

print("2x3 null Matrix:\n", matrix1)

# [0 for i in range(cols)]: This is a list comprehension. For each value in the range 0 to cols - 1, 
# it places a 0 in a new list. So, if cols = 3, this will produce a list like [0, 0, 0].

# for j in range(rows): This is another list comprehension nested inside the first one.
# It repeats the list comprehension rows times. If rows = 2, it will generate a list of rows number of [0, 0, 0] lists.

# The outer list comprehension creates a list of rows sublists, each with cols zeros.
1
2
3
2x3 null Matrix:
 [[0 0 0]
 [0 0 0]]

4 Creating/ initializing a mxm identity matrix

1
2
3
4
5
6
import numpy as np 
import numpy.matlib # importing matrix library from numpy 
  
# desired 3 x 3 output matrix  
idt_mat = np.matlib.eye(3, k = 0)  
print ("Output matrix :\n", idt_mat) 
1
2
3
4
Output matrix :
 [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

5 Matrix operations (addition and subtraction)

1
2
3
4
5
6
7
8
9
10
import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

C = A + B  # Matrix Addition
D = B - A  # Matrix Subtraction

print("Matrix Addition:C=A+B\n", C)
print("Matrix Subtraction:D=B-A\n", D)
1
2
3
4
5
6
Matrix Addition:C=A+B
 [[ 6  8]
 [10 12]]
Matrix Subtraction:D=B-A
 [[4 4]
 [4 4]]
1
2
3
4
5
6
7
8
9
10
11
# alternatively
import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

C = np.add(A,B)  # Matrix Addition
D = np.subtract(B,A)  # Matrix Subtraction

print("Matrix Addition:C=A+B\n", C)
print("Matrix Subtraction:D=B-A\n", D)
1
2
3
4
5
6
Matrix Addition:C=A+B
 [[ 6  8]
 [10 12]]
Matrix Subtraction:D=B-A
 [[4 4]
 [4 4]]

6 Matrix Multiplication

Untitled

1
2
3
4
5
6
7
8
9
10
11
12
13
# matrix multiplication
E1=A@B
print(E1)
print("\n")

# alternatively
E2=np.dot(A,B)
print(E2)
print("\n")

# alternatively
E3=np.matmul(A,B)
print(E3)
1
2
3
4
5
6
7
8
9
10
[[19 22]
 [43 50]]


[[19 22]
 [43 50]]


[[19 22]
 [43 50]]

7 Finding transpose of a matrix

1
2
AT = A.T
print("Transpose of A:\n", AT)
1
2
3
Transpose of A:
 [[1 3]
 [2 4]]

8 Finding determinant of a matrix

Untitled1

1
2
det_A = np.linalg.det(A)
print("Determinant of A:", det_A)
1
Determinant of A: -2.0000000000000004

9 Finding inverse of a matrix

1
2
3
4
5
if np.linalg.det(A) != 0:  # det_A != 0: 
    A_inv = np.linalg.inv(A)
    print("Inverse of A:\n", A_inv)
else:
    print("Matrix A is singular (not invertible).")
1
2
3
Inverse of A:
 [[-2.   1. ]
 [ 1.5 -0.5]]

10 Eigenvalues and Eigenvectors

For a square matrix A, the eigenvalues λ and eigenvectors v satisfy: Av=λv

1
2
3
eigenvalues, eigenvectors = np.linalg.eig(A)
print("Eigenvalues:\n", eigenvalues)
print("Eigenvectors:\n", eigenvectors)
1
2
3
4
5
Eigenvalues:
 [-0.37228132  5.37228132]
Eigenvectors:
 [[-0.82456484 -0.41597356]
 [ 0.56576746 -0.90937671]]

More detail will be posted later with video lecture - Supratim Chakraborty

This post is licensed under CC BY 4.0 by the author.

Trending Tags