Matrices and Matrix Arithmetic for Machine Learning
Last Updated :
23 Jul, 2025
In machine learning, the data often comes in multi-dimensional arrays so matrices are best to handle such inputs. So we need to have some general idea on basic arithmetic operations in matrices. In this article, we will discuss about matrices and matrix arithmetic for machine learning.
What is a Matrix?
Matrix is a two dimensional array where the data is in the form of rows and columns. The each element in the matrix is represented as a[ i ][ j ] where i represents the row number and j represents the column number. Each matrix has an order representing the total number of rows (m) and columns ( n ) in the matrix and it is written as m X n.
Defining a matrix:
In Python, we use numpy module to define a matrix. As we discussed that matrices are two dimensional arrays, let us create a two dimensional array (matrix) using numpy.array( ) method.
Python
#importing numpy module
import numpy as np
#defining a matrix using np.array() method
matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
#printing the matrix
print(matrix)
Output:
[[1 2 3]
[4 5 6]
[7 8 9]]
we can see that it is in the form of list of lists which is a two-dimensional array having 3 rows and 3 columns. The order of the above matrix is 3 X 3.
Matrix Arithmetic
Addition of matrices can only be done when the two matrices have the same order. It is done by using ( + ) operator which adds corresponding elements of the matrices. For example, if we have two matrices A and B then the a[i][j] is added with b[i][j] where i is row number and j is column number.
Python
import numpy as np
A = np.array([[1,2],[3,4]])
B = np.array([[5,6],[7,8]])
#adding A and B matrices
C = A + B
print(C)
Output:
[[ 6 8]
[10 12]]
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
Subtraction of matrices is possible when both the matrices have same order. Subtraction is done by using ( - ) operator in Python. When we perform subtraction on two matrices then the elements from the first matrix are subtracted with their corresponding elements in second matrix.
Python
import numpy as np
A = np.array([[1 , 2],[3 , 4]])
B = np.array([[0 , 1],[2 , 1]])
C = A - B
print(C)
Output:
[[1 1]
[1 3]]
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 with elements of matrix B and we got the resultant matrix with order 2 X 2.
Matrix Division
Division of matrices is done by using ( / ) operator. It divides elements from first matrix with corresponding elements in the second matrix. The matrices should have same dimensions.
Python
import numpy as np
A = np.array([[4 , 2],[6 , 8]])
B = np.array([[2 , 2],[3 , 4]])
C = A//B
print(C)
Output:
[[2 1]
[2 2]]
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 )
Multiplication of matrices can only be done when the columns of first matrix is equal to the rows of the second matrix. Suppose there is a matrix A of order m X n and matrix B of order n X k so here columns of matrix A = rows of matrix B = n and the order of the resulting matrix C = AB is m X k. Hence multiplication is possible for the above matrices.
Note : Matrices do not satisfy commutative property
AB is not equal to BA
Python
import numpy as np
A = np.array([[1, 2],[3, 4]])
B = np.array([[5, 6],[7, 8]])
C = A.dot(B)
print(C)
Output:
[[19 22]
[43 50]]
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]
The matrix A is of order 2 X 2 and matrix B of order 2 X 2. Since the columns of A = rows of B = 2, the multiplication is possible. The multiplication is done in a way where the 1st row of A is multiplied with 1st and 2nd column of B giving us C[0][0] and C[ 0 ][ 1 ] and the 2nd row of A is multiplied with 1st and 2nd column of B giving us C[ 1 ][ 0 ] and C[ 1 ] [ 1 ].
Matrix - Vector multiplication
A vector is a one dimensional array having either a single row or single column. A matrix having only one row is called as a row vector and matrix having only one column is called as a column vector. While multiplying the matrix with a vector, we need to check the multiplication condition which is the columns of first matrix = rows of the second matrix.
Python
import numpy as np
A = np.array([[1, 2],[1 , 1]])
V = np.array([[1] , [1]])
#matrix - vector multiplication
C = A.dot(V)
print(C)
Output:
[[3]
[2]]
Explanation : We multplied the matrix A with vector V and we got a column vector C. In the same way we can multiply a matrix with a row vector and the dot product will also be 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 we multiply a matrix with a scalar then it is multiplied with each and every element in the matrix. The order remains same even after multiplying the matrix with scalar. We use matrix - scalar multiplications while solving equations in linear algebra.
Python
import numpy as np
A = np.array([[1,2],[3,4]])
b = 2
print(A * b)
Output:
[[2 4]
[6 8]]
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]
Similar Reads
Machine Learning Tutorial Machine learning is a branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data without being explicitly programmed for every task. In simple words, ML teaches the systems to think and understand like humans by learning from the data.Do you
5 min read
Introduction to Machine Learning
Python for Machine Learning
Machine Learning with Python TutorialPython language is widely used in Machine Learning because it provides libraries like NumPy, Pandas, Scikit-learn, TensorFlow, and Keras. These libraries offer tools and functions essential for data manipulation, analysis, and building machine learning models. It is well-known for its readability an
5 min read
Pandas TutorialPandas is an open-source software library designed for data manipulation and analysis. It provides data structures like series and DataFrames to easily clean, transform and analyze large datasets and integrates with other Python libraries, such as NumPy and Matplotlib. It offers functions for data t
6 min read
NumPy Tutorial - Python LibraryNumPy (short for Numerical Python ) is one of the most fundamental libraries in Python for scientific computing. It provides support for large, multi-dimensional arrays and matrices along with a collection of mathematical functions to operate on arrays.At its core it introduces the ndarray (n-dimens
3 min read
Scikit Learn TutorialScikit-learn (also known as sklearn) is a widely-used open-source Python library for machine learning. It builds on other scientific libraries like NumPy, SciPy and Matplotlib to provide efficient tools for predictive data analysis and data mining.It offers a consistent and simple interface for a ra
3 min read
ML | Data Preprocessing in PythonData preprocessing is a important step in the data science transforming raw data into a clean structured format for analysis. It involves tasks like handling missing values, normalizing data and encoding variables. Mastering preprocessing in Python ensures reliable insights for accurate predictions
6 min read
EDA - Exploratory Data Analysis in PythonExploratory Data Analysis (EDA) is a important step in data analysis which focuses on understanding patterns, trends and relationships through statistical tools and visualizations. Python offers various libraries like pandas, numPy, matplotlib, seaborn and plotly which enables effective exploration
6 min read
Feature Engineering
Supervised Learning
Unsupervised Learning
Model Evaluation and Tuning
Advance Machine Learning Technique
Machine Learning Practice