This article was published as part of the Data Science Blogathon
Introduction
If you start learning the deep learningDeep learning, A subdiscipline of artificial intelligence, relies on artificial neural networks to analyze and process large volumes of data. This technique allows machines to learn patterns and perform complex tasks, such as speech recognition and computer vision. Its ability to continuously improve as more data is provided to it makes it a key tool in various industries, from health..., The first thing you'll be exposed to is linear algebra concepts that give you a better intuition on how algorithms actually work under the hood., allowing you to make better decisions. En Deep Learning, a red neuronalNeural networks are computational models inspired by the functioning of the human brain. They use structures known as artificial neurons to process and learn from data. These networks are fundamental in the field of artificial intelligence, enabling significant advancements in tasks such as image recognition, Natural Language Processing and Time Series Prediction, among others. Their ability to learn complex patterns makes them powerful tools.. of progress is a very simple and very useful network. Underhood, the feedforward neural network is just a composite function, which multiplies some matrices and vectors together.

Image source: Link
It is not that vectors and matrices are the only way to perform these operations, but they become highly efficient if you do. The central data structures behind deep learning include
- Scalars
- Cartoon vector,
- Matrices and
- Tensioners.
Matrix operations are used in the description of many deep learning algorithms.

Image source: Link
Then, if you really want to be a professional in the field of Deep Learning, you can't stop mastering some of these concepts. Then, in this article, We will discuss important linear algebra matrix operations that are used in the description of deep learning methods..
Table of Contents
The topics we will discuss in this article are as follows:
- What are matrices?
- How to add and subtract different matrices?
- How to find the shape and size of a given array?
- How to convert a dense matrix to a sparse matrix?
- How to find the transpose of a matrix?
- How to find the mean, the variance and standard deviation of a matrix?
- How to find the trace of a matrix?
- How to extract minimum and maximum elements from an array?
- How to find the determinant of a matrix?
- How to multiply the given matrices?
- How to apply the particular operation to each element of an array?
- How to find the inverse of a matrix?
- How to reshape the matrix to a different size?
What are matrices?
Arrays are rectangular arrays that consist of numbers and can be viewed as 2North Dakota-order of tensioners. If m and n are positive integers, namely, m, n ∈ ℕ then the m × n matrix contains m * n number of elements, with m number of rows and n number of columns.
The pictorial representation of an m × n matrix is shown below:

Image source: Link
Sometimes, instead of describing the complete components of the array, we use the following abbreviation of a matrix:

For instance-
In this example, with the help of numpy library, we will create an array. And also check the dimension"Dimension" It is a term that is used in various disciplines, such as physics, Mathematics and philosophy. It refers to the extent to which an object or phenomenon can be analyzed or described. In physics, for instance, there is talk of spatial and temporal dimensions, while in mathematics it can refer to the number of coordinates necessary to represent a space. Understanding it is fundamental to the study and... of the formed matrix.
import numpy as np
matrix = np.array([[45,34],[67,58]])
# Create a matrix
print("The original matrix is given by n", matrix)
# Check the dimension of the matrix
print("The dimension of the given matrix is", matrix.ndim)
Production:
The original matrix is given by [[45 34] [67 58]] The dimension of the given matrix is 2
Matrix addition and subtraction

Image source: Link
In this section, we will perform addition and subtraction of matrices using the methods add and subtract. These methods take two arguments and return the sum and difference of those matrices respectively. If the shape of the matrices is not the same, it throws an error that says, addition or subtraction is not possible.
matrix_1 = np.array([[45,34],[67,58]])
matrix_2 = np.array([[35,24],[57,48]])
# Add the two matrices
print("The result after adding matrix 1 and matrix 2 is given by n" , np.add(matrix_1, matrix_2))
# Subtract one matrix from the other matrices
print("The result after subtracting matrix 1 from matrix 2 is given by n" , np.subtract(matrix_1, matrix_2))
print("The result after subtracting matrix 2 from matrix 1 is given by n" , np.subtract(matrix_2, matrix_1))
Production:
The result after adding matrix 1 and matrix 2 is given by [[ 80 58] [124 106]] The result after subtracting matrix 1 from matrix 2 is given by [[10 10] [10 10]] The result after subtracting matrix 2 from matrix 1 is given by [[-10 -10] [-10 -10]]
Shape and size of an array
In this section, we will find the way, namely, the number of rows and columns in the given matrix and the size, namely, the number of elements in the array of a given array.
matrix = np.array([[45,34,75],[67,58,89]])
# Finding number of rows and columns in the matrix
print("The number of rows and columns in the given matrix are " + str(matrix.shape[0]) + " and " + str(matrix.shape[1]) + " respectively")
# Number of elements in the matrix
print("The size of the given matrix is" , matrix.size)
Production:
The number of rows and columns in the given matrix are 2 and 3 respectively The size of the given matrix is 6
Converting a given dense matrix to a sparse matrix
Let's first understand what exactly does it mean by sparse and dense matrix.
A sparse matrix is a matrix that consists mainly of zero values. And sparse matrices are different from matrices with mostly nonzero values, which are known as dense matrices.

Image source: Link
from scipy import sparse
# Create a Dense Matrix
dense_matrix = np.array([[0,0],[0,17],[78,0]])
# Convert Dense matrix to Sparse matrix
sparse_matrix = sparse.csr_matrix(dense_matrix)
print("The sparse matrix corresponding to a given dense matrix is given by n" , sparse_matrix)
Production:
The sparse matrix corresponding to a given dense matrix is given by (1, 1) 17 (2, 0) 78
Matrix transpose
En Matrix Transpose, we can convert a row vector to a column vector and vice versa, namely, row becomes columns and columns become rows.
If we have the matrix A = [aij]mxn, then the transpose of this matrix is AT = [afrom the]n × m
Image source: Link
import numpy as np
matrix = np.array([[45,34],[67,58]])
print("The original matrix is given by n" , matrix)
print("The transpose matrix of the given matrix is n" , matrix.T)
Production:
The original matrix is given by [[45 34] [67 58]] The transpose matrix of the given matrix is [[45 67] [34 58]]
Media, variance and standard deviation of a matrix
In this section, we will try to find some statistical things related to an array. Here we calculate the mean, the variance and standard deviation of the matrix using the numpy functions.
import numpy as np
matrix = np.array([[45,34],[67,58], [23,89]])
# Finding the mean of a matrix elements
print("The mean of the elements of a matrix is equal to", np.mean(matrix))
# Finding the Variance of a matrix elements
print("The variance of the elements of a matrix is equal to", np.var(matrix))
# Finding the Standard Deviation of a matrix elements
print("The standard deviation of the elements of a matrix is equal to", e.g. std(matrix))
print("The standard deviation of the elements of a matrix is equal to", np.sqrt(np.var(matrix)))
Production:
The mean of the elements of a matrix is equal to 52.666666666666664 The variance of the elements of a matrix is equal to 473.5555555555555 The standard deviation of the elements of a matrix is equal to 21.761331658599286 The standard deviation of the elements of a matrix is equal to 21.761331658599286
Trace of a matrix

Image source: Link
In this section, will try to find the trace of a matrix, namely, the sum of all diagonal elements present in a given matrix.
import numpy as np
matrix = np.array([[1,2,3],[4,5,6], [7,8,9]])
# Get the diagonal elements of a matrix
print("The diagonal elements of a given matrix are n", matrix.diagonal())
# Finding the trace of the matrix
print("The trace of a given matrix is equal to", matrix.diagonal().sum())
Production:
The diagonal elements of a given matrix are [1 5 9] The trace of a given matrix is equal to 15
Finding minimum and maximum elements of a matrix
In this section, we will try to find the minimum and maximum elements of a matrix, namely, the item with the highest and lowest value among all items.
import numpy as np
matrix = np.array([[1,2,3],[4,5,6], [7,8,9]])
# Find the minimum element of the matrix
print("The minimum element in a given matrix is", e.g. min(matrix))
# Find the maximum element of the matrix
print("The maximum element in a given matrix is", np.max(matrix))
Production:
The minimum element in a given matrix is 1 The maximum element in a given matrix is 9
Determinant of a matrix

Image source: Link
In this section, we will try to find the determinant of a matrix. Here, to calculate the determinant, we use the linear algebra module present in the Numpy package.
import numpy as np
matrix = np.array([[1,2,4],[3,4,6], [7,8,5]])
# Find the determinant of the matrix
print("The determinant of the given matrix is equal to", np.linalg.det(matrix))
Production:
The determinant of the given matrix is equal to 9.999999999999993
Matrix multiplication
An array of shape (mxn) and a matrix B of form (nxp) multiplied from C of form (mxp). Remember that when multiplying the matrices is that the number of columns in the first matrix is the same as the number of rows in the second matrix to perform the multiplication without errors.

Image source: Link
In this section, we will try to find the multiplication of two matrices.
import numpy as np
matrix_1 = np.array([[45,34],[67,58]])
matrix_2 = np.array([[35,24],[57,48]])
print("The matrix multiplication of given two matrices is given by n", e.g. matmul(matrix_1, matrix_2))
Production:
The matrix multiplication of given two matrices is given by [[3513 2712] [5651 4392]]
Smart operations with elements using an inline function (Lambda)
In this example, we will try to add a certain value to each of the elements of an array.
import numpy as np
matrix = np.array([[1,2,4],[3,4,6], [7,8,5]])
addition = lambda i:i+5
add_5_vec = np.vectorize(addition)
print("The matrix after adding 5 to all its elements is n", add_5_vec(matrix))
Production:
The matrix after adding 5 to all its elements is [[ 6 7 9] [ 8 9 11] [12 13 10]]
Inverse of a matrix
In this section, we will try to find the inverse of a matrix.
import numpy as np
matrix = np.array([[1,2,4],[3,4,6], [7,8,5]])
# Finding the inverse of a matrix
print("The inverse matrix of a given matrix is n", np.linalg.inv(matrix))
Production:
The inverse matrix of a given matrix is [[-2.8 2.2 -0.4] [ 2.7 -2.3 0.6] [-0.4 0.6 -0.2]]
Reshape a given Matrix
In this section, we will try to reshape a given matrix, namely, change the shape of the given matrix. But here we have to notice that the size remains constant after reshaping the matrix, namely, the number of elements remains the same.
import numpy as np
matrix = np.array([[1,2,4],[3,4,6],[7,8,5],[9,2,1]])
print("The reshaped matrix is given by n", matrix.reshape(6,2))
Production:
The reshaped matrix is given by [[1 2] [4 3] [4 6] [7 8] [5 9] [2 1]]
Other blog posts of mine
You can also check out my previous blog posts.
Past Data Science Blog Posts.
Here it is my Linkedin profile in case you want to connect with me. I will be happy to be connected with you.
For any query, you can email me at Gmail.
Final notes
Thank you for reading!
I hope you liked the article. If you like, share it with your friends too. Anything not mentioned or do you want to share your thoughts? Feel free to comment below and I'll get back to you. 😉
The media shown in this article is not the property of DataPeaker and is used at the author's discretion.



