In machine learning, data is frequently represented in the form of multi-dimensional arrays, making matrices an ideal structure for handling such inputs. Therefore, it is important to have a foundational understanding of basic matrix operations. In this article, we will explore matrices and their arithmetic operations in the context of machine learning.

What is a Matrix?

A matrix is a two-dimensional array in which data is organized in rows and columns. Each element in a matrix is denoted as a[i][j], where i represents the row index and j represents the column index. Every matrix has an order, defined by the number of rows (m) and columns (n), and it is typically expressed as m × n.

Defining a matrix:

In Python, the NumPy library is commonly used to work with matrices. Since matrices are essentially two-dimensional arrays, we can create them using the numpy.array() method. This function allows us to define a matrix by passing a list of lists, where each inner list represents a row of the matrix.

Output:

As observed, the matrix is represented as a list of lists, forming a two-dimensional array with 3 rows and 3 columns. Therefore, the order of the matrix is 3 × 3.

Matrix Operations in NumPy

Matrix Addition

Matrix addition is only possible when the two matrices have the same order, meaning they have an equal number of rows and columns. In Python, this operation can be performed using the + operator, which adds corresponding elements from both matrices. For instance, given two matrices A and B, each element a[i][j] is added to the corresponding element b[i][j], where i denotes the row index and j denotes the column index.

Output:

Explanation: In the above example, we have two matrices, A and B.

A = [1 ,2] B = [5,6] [3, 4] [7, 8]

C = [1 + 5 2+6] C = [6 8] [3+ 7 4+8] ==> [10 12]

We can see that the corresponding elements are added, and we get the final matrix with the same order.

Matrix Subtraction

Matrix subtraction is possible only when both matrices have the same order—that is, they contain an equal number of rows and columns. In Python, subtraction is performed using the - operator. During this operation, each element of the second matrix is subtracted from the corresponding element in the first matrix.

Output:

Explanation: A = [1 ,2] B = [0 , 1] [3, 4] [2, 1]

C = [1 – 0 2 – 1] C = [1 1] [3 -2 4 – 1] ==> [1 3]

The elements of the matrix A are subtracted from the elements of matrix B, and we get the resultant matrix with order 2 X 2.

Matrix Division

Matrix division, in the element-wise sense, is performed using the / operator in Python. This operation divides each element of the first matrix by the corresponding element in the second matrix. For this to be valid, both matrices must have the same dimensions.

Output:

Explanation : A = [4 ,2] B = [2 , 2] [6, 8] [3, 4]

C = [4/2 2 /2] C = [2 1] [6/3 8/4] ==> [2 2]

Matrix – Matrix Multiplication (Dot product)

Matrix multiplication is defined only when the number of columns in the first matrix is equal to the number of rows in the second matrix. Suppose matrix A has an order of m × n, and matrix B has an order of n × k. Since the number of columns in A equals the number of rows in B (both are n), the multiplication is valid. The resulting matrix C = AB will have the order m × k.

Note: Matrices do not satisfy the commutative property 

AB is not equal to BA

Output:

Explanation : [1, 2] [5, 6] [1*5 + 2*7 1*6 + 2*8] [19 22] [3, 4] X [7, 8] ==> [3*5 + 4*7 3*6 + 4*8] ==> [43 50]

Matrix A is of order 2 × 2, and matrix B is also of order 2 × 2. Since the number of columns in A equals the number of rows in B (both are 2), multiplication is possible. The multiplication is performed by multiplying the first row of A with the first and second columns of B, producing the elements C[0][0] and C[0][1] of the resulting matrix C. Similarly, the second row of A is multiplied by the first and second columns of B, yielding the elements C[1][0] and C[1][1].

Matrix – Vector multiplication

A vector is a one-dimensional array consisting of either a single row or a single column. A matrix with only one row is referred to as a row vector, while a matrix with only one column is known as a column vector. When multiplying a matrix by a vector, it is important to ensure that the multiplication condition is satisfied—that is, the number of columns in the matrix must equal the number of rows in the vector.

Output:

Explanation: We multiplied matrix A by vector V and obtained a column vector C. Similarly, multiplying a matrix by a row vector results in a dot product that is also a row vector.

A = [1 , 2] V = [ 1 ] [1 * 1 + 2 * 1] [ 3 ] [1 , 1] [ 1 ] ==> A . V = [1 * 1 + 1 * 1 ] ==> A . V = [ 2 ]

Matrix – Scalar multiplication

When a matrix is multiplied by a scalar, the scalar is applied to each element within the matrix individually. The order of the matrix remains unchanged after scalar multiplication. Scalar multiplication of matrices is commonly used when solving equations in linear algebra.

Output:

Explanation : In the above matrix, we can see that b=2 is multiplied with each element in the matrix.

A*b = [1*2, 2*2] [2, 4] [3*2 , 4*2] ==> [6 , 8]

Leave a Comment