Sitemap
Natig Mamishov

Natig Mamishov

Data Scientist

Matrices and NumPy: From Theory to Practice

12 min readJun 11, 2026

--

In this article, we will talk about the theoretical side of matrices and their practical use with NumPy.

Press enter or click to view image in full size

Matrix

A matrix is a way of representing data in the form of rows and columns.

It looks like the following:

Our matrix has 2 rows and 3 columns, and it is written as A₂ₓ₃.

a₁₂ means the element in the 1st row and 2nd column.

Let’s look at a real example. Suppose the distribution of patients who visited a hospital over different years is as follows:

As we can see, the resulting matrix A consists of numbers arranged in 4 rows and 3 columns.

Let’s look at another example.

We can represent this data as a matrix and call it matrix A as shown below:

each row = one observation/sample

each column = one feature

Example:

column 1 = age

column 2 = salary

There are several types of matrices that we frequently use in practice. Let’s try to explain some of them below.

Square Matrix

A matrix is called a square matrix if the number of rows and columns are equal. In other words, the matrix must have the form Aₙₓₙ.

Press enter or click to view image in full size

Note: Covariance and correlation matrices, which are widely used in practice and which we will discuss later, are square matrices.

Zero Matrix

A matrix whose elements are all equal to 0 is called a zero matrix.

Press enter or click to view image in full size

Diagonal Matrix

In a square matrix, if the elements on the main diagonal are scalars and all the other elements are 0, then the matrix is called a diagonal matrix.

Identity Matrix

A square matrix whose main diagonal elements are 1 and all other elements are 0 is called an identity matrix.

Note: An identity matrix is a special case of a scalar matrix.

Scalar Matrix

A matrix obtained by multiplying an identity matrix by a scalar number is called a scalar matrix. In other words, a square matrix whose diagonal elements are all equal is called a scalar matrix.

That is, it is a square matrix with the same numbers on the main diagonal and 0 in all other positions.

Let’s look at an example.

Note: A scalar matrix is a special case of a square matrix.

Row and Column Vectors

A matrix with only one column is called a column matrix, and a matrix with only one row is called a row matrix.

In the example above, if we consider only the number of patients who visited in spring, we obtain a 1x3 row matrix (B).

If we consider only the number of patients who visited in 2018, we obtain a 4x1 column matrix (Y).

Equality of Matrices

For two or more matrices to be equal, they must have the same dimensions, and each pair of corresponding elements must be equal.

Let’s look at an example:

As we can see, the dimensions are the same, and all corresponding elements are equal.

Now let’s examine some operations on matrices.

Matrix Addition

Matrix addition is only possible between matrices of the same size.

Corresponding elements are added together and written in the same position in the resulting matrix.

Let’s look at the example below:

Matrix Subtraction

The same rule applies here.

Multiplication of a Matrix by a Scalar

Each element of the matrix is multiplied by the given scalar value.

Matrix Multiplication

To multiply two matrices, the number of columns in the first matrix must be equal to the number of rows in the second matrix.

Let’s look at the formula:

The elements of each row of the first matrix are multiplied by the corresponding elements of a column of the second matrix, and the results are then summed.

Let’s see an example:

Some properties of matrix addition and multiplication are shown in the table below.

Press enter or click to view image in full size

Matrix Transpose

The transpose of a matrix is obtained by swapping its rows and columns. In other words, rows become columns and columns become rows.

It is denoted by Aᵀ or A’.

The table below shows some properties of transpose matrices.

Sometimes a row vector is represented as the transpose of a column vector. For example:

Symmetric Matrix

A square matrix A is called symmetric if the condition A = Aᵀ is satisfied.

For example:

Inverse of a Matrix

There are several important points to note here:

Only square matrices can have an inverse. In other words, only matrices with the same number of rows and columns can have an inverse.

Not every square matrix has an inverse.

Matrix Inverse Using the Elementary Row Method (Gauss-Jordan Algorithm)

The formula is as follows:

We write the identity matrix on the right side of matrix A.

If we can transform the left side into the identity matrix, then the matrix obtained on the right side will be the inverse of A.

In other words, we apply elementary row operations to transform matrix A into the identity matrix. The matrix that appears on the right side after these operations is the inverse matrix.

Note: While trying to transform A into the identity matrix, if a row becomes entirely zeros, then the matrix A does not have an inverse.

Now let’s go through this method step by step with an example.

Let’s try to find the inverse of the given matrix A:

As we mentioned, the goal is to transform matrix A on the left side into an identity matrix.

The steps are as follows:

  1. Make the element below the main diagonal equal to 0.
  2. Make the main diagonal elements equal to 1.
  3. Make the element above the main diagonal equal to 0.

Note: The first element on the main diagonal must not be 0. If the element in the first row and first column had been 0, we would first need to swap that row with another row.

According to these steps, we first need to turn 3 into 0. To do this, we multiply the first row by -3 and add it to the second row. The result is then written in the second row.

This completes the first step. Let’s look at the result:

Next, we need to make the main diagonal elements equal to 1.

Get Natig Mamishov’s stories in your inbox

Join Medium for free to get updates from this writer.

The first diagonal element is already 1, so we only need to change -2 into 1.

To do this, we multiply the second row by -1/2. As a result, the second diagonal element becomes 1.

Let’s look at the result:

Finally, we need to make the element above the main diagonal (2) equal to 0.

To do this, we multiply the second row by -2 and add it to the first row. The result is then written in the first row.

Let’s look at the result:

We have now obtained the identity matrix on the left side.

Therefore, the matrix on the right side is the inverse of the given matrix A.

Note: The following equality is always true:

Determinant

A determinant can only be calculated for square matrices.

Determinants are mainly used to determine whether a matrix has an inverse and, in some cases, whether a system of equations has a solution.

A determinant is denoted by Δ or |A|.

For a 2 x 2 matrix, the determinant is calculated by subtracting the product of the secondary diagonal elements from the product of the main diagonal elements.

Let’s look at an example:

The determinant of a 3 x 3 matrix is calculated using the following formula:

But how do we calculate the determinant of an n x n matrix?

If we draw lines through the row and column containing a particular element of a determinant, the remaining elements form a smaller determinant whose size is one less than the original determinant.

This smaller determinant is called the minor of that element.

The minor of the element aᵢⱼ is denoted by Mᵢⱼ.

The product of this minor and (-1)ⁱ⁺ʲ is called the cofactor of the element aᵢⱼ, and it is denoted by:

Aᵢⱼ = (-1)ⁱ⁺ʲ Mᵢⱼ

Let’s examine this on a 3 x 3 matrix:

Using the formula above, we can write the cofactors as follows:

Finally, we can give the formula for the determinant of an n x n matrix:

Let’s look at the following example:

Matrix Inverse Using the Determinant Method

We calculate the inverse of a matrix using the following formula:

Let’s look at an example:

det(A) = 2 x 2–1 x 1 = 4–1 = 3

Then,

Determinants are also useful when solving systems of equations. Consider the following system of equations:

We can write this system in matrix form as follows:

After expressing this product as A x X = B, we can multiply both sides by A⁻¹ and we get: A⁻¹AX = A⁻¹B

As we mentioned earlier, A⁻¹A = I and IX = X, so we can write: X = A⁻¹B

Therefore,

Note: If the determinant of a matrix is 0, then the matrix does not have an inverse.

Let’s take an example and find the inverse of a matrix using this method:

First, we need to find the matrix of cofactors using the formula: Aᵢⱼ = (−1)ⁱ⁺ʲ Mᵢⱼ

The transpose of this matrix gives us Adj(A).

Therefore,

Substituting into the main formula, we get:

Rank of a Matrix

The rank of a matrix is the number of linearly independent rows or columns in that matrix.

Let’s look at a simple example:

As we can see, the second row is simply twice the first row. This means the second row does not provide any new information. Therefore, the rank of this matrix is 1.

Note: If Aₙₓₙ is a square matrix and rank(A) < n, then matrix A does not have an inverse.

NumPy

NumPy is one of the main Python libraries used for numerical computations, especially matrix operations.

When working with large numerical datasets and performing matrix operations such as the ones discussed above, NumPy is generally faster and more convenient than using standard Python lists.

First, we can import NumPy as follows:

To avoid writing numpy every time, we use the abbreviation np. In fact, we can choose any name we want, but np is the widely accepted convention, so we will use it as well. Let’s start by creating an array.

Now let’s create an array with 2 rows and 3 columns and check its shape:

When we write A[1][0], it returns the element located at row 1, column 0. In matrix notation, this corresponds to the element in the 2nd row and 1st column because Python indexing starts from 0. Therefore, we expect the result to be 98.

Earlier, we discussed matrix multiplication, addition, and other operations. Now let’s see how they work in NumPy:

Let’s look at the product of two matrices:

Difference Between A *B and A @ B

When we write A * B, corresponding elements are multiplied together, just like element-wise operations in addition and subtraction. When we write A@B, NumPy performs standard matrix multiplication.

Let’s also look at some NumPy functions and indexing methods that are frequently useful. We can use arange to create a simple NumPy array:

Now let’s use NumPy to create the zero and identity matrices we discussed earlier and find a transpose. We can also create matrices of any size using random. The reshape function helps us change the dimensions of an array.

Let’s look at the code:

Press enter or click to view image in full size

Let’s revisit the two-variable system of equations from above:

Now let’s try to solve this system using NumPy:

Let’s also see how to calculate the determinant, inverse matrix, and rank using NumPy:

Press enter or click to view image in full size

In this article, we covered the basic concepts of matrices and how to work with them using NumPy.

Matrices are among the concepts we use regularly in Data Science and Machine Learning because real-world data is often represented as rows and columns in the form of matrices.

NumPy makes it easy to perform operations on these matrices, including addition, multiplication, transpose, determinant calculation, finding the inverse matrix, computing rank, and many other tasks.

--

--