How to work with matrices in Python

by Alex
How to work with matrices in Python

A matrix is a two-dimensional array consisting of M rows and N columns. Matrices are often used in mathematical calculations. Programmers work with matrices mostly in the scientific field, but they can also be used for other things, such as quickly generating levels in a video game.

Matrices and the NumPy library

A programmer can implement all the functions for working with matrices: multiplication, addition, transposition, etc. on their own. This is much easier to do in Python than in lower-level languages such as C. But it doesn’t make sense to write the same algorithms every time, so the NumPy library was developed. It is used for complex scientific calculations and provides the programmer with functions for working with two-dimensional arrays. Instead of writing dozens of lines of code to perform simple operations on matrices, the programmer can use a single function from NumPy. The library is written in Python, C, and Fortran, so the functions work even faster than pure Python.

Connecting the NumPy Library

NumPy is not built into the Python interpreter, so you must install it before importing it. You can use the pip utility to do this. Type the command in the console:

pip install numpy

Now that the library is installed, you can connect it using the import command. For convenience, let’s rename numpy when importing it to np as follows:

import numpy as np
In the examples below we will use this import, so the reference to the library will be through np and not numpy!

Creating

The array() function is used to create a matrix. A list is passed to the function. Here is an example of creation, we pass a two-dimensional list as a function argument:

a = np.array([[3, 3, 3], [2, 5, 5]])

The second parameter can specify the type of the matrix elements:

a = np.array([[3, 3, 3], [2, 5, 5]], int)
print(a)

Then the console will print:

[[3 3 3]
 [2 5 5]]

Note that if you change int to str, the element type is changed to string. In addition, when output to the console, NumPy automatically formatted the output to look like a matrix, with the elements arranged under each other. You can use int, float, bool, complex, bytes, str, buffers as element types. You can also use other NumPy types: logical, integer, unsigned integer, real, complex. Here are a few examples:

  • np.bool8 is a logical variable that occupies 1 byte of memory.
  • np.int64 – an integer that occupies 8 bytes.
  • np.uint16 – unsigned integer that takes 2 bytes of memory.
  • np.float32 – real number, taking 4 bytes in memory.
  • np.complex64 – complex number, consisting of 4 bytes real number of real part and 4 bytes imaginary part.

You can also find out the size of a matrix by using the shape attribute:

size = a.shape
print(size) # Print (2, 3)

The first number (2) is the number of rows, the second number (3) is the number of columns.

Zero matrix

If you want to create a matrix consisting only of zeros, use zeros():

a_of_zeros = np.zeros((2,2))
print(a_of_zeros)

The result of this code is as follows:

[[0. 0.]
 [0. 0.]]

Getting row, column, and element

To get the row of a two-dimensional matrix, you just need to refer to it by its index as follows:

temp = a[0]
print(temp) #Will print [3 3 3]

Getting the column is not so easy anymore. We use slices, we don’t specify anything as the first element of the slice, and the second element is the number of the column we are looking for. Example:

arr = np.array([[3,3,3],[2,5,5]], str)
temp = arr[:,2]
print(temp) # will print ['3' '5']

To get the item, you need to specify the column number and row number where it is located. For example, the element in row 2 and column 3 is 5, we check (remember that numbering starts with 0):

arr = np.array([[3,3,3],[2,5,5]], str)
temp = arr[1][2]
print(temp) # print 5

Multiplication and addition

To add matrices, you need to add all their corresponding elements. Python uses the usual “+” operator to add them. An example of addition:

arr1 = np.array([[3,3,3],[2,5,5]])
arr2 = np.array([[2,4,2],[1,3,8]])
temp = arr1 + arr2
print(temp)

The resulting matrix will be:

[[ 5 7 5]
 [ 3 8 13]]

It is important to remember that only matrices with the same number of rows and columns can be added, otherwise the Python program will end with a ValueError exception. Matrix multiplication is very different from addition. You can’t just multiply the corresponding elements of two matrices. First, the matrices must be consistent, that is, the number of columns of one must equal the number of rows of the other and vice versa, otherwise the program will cause an error. Multiplication in NumPy is done using the dot() method. An example of multiplication:

arr1 = np.array([[3,3],[2,5]])
arr2 = np.array([[2,4],[1,3]])
temp = arr1.dot(arr2)
print(temp)

The result of executing this code is as follows:

[[ 9 21]
 [ 9 23]]
How did it get there? Take number 21, its position is 1 row and 2 columns, then we take 1 row of the first matrix and multiply by 2 columns of the second matrix. And the elements are multiplied positively, that is, 1 by 1 and 2 by 2, and the results are added

: [3,3] * [4,3] = 3 * 4 + 3 * 3 = 21.

Transpose and Inverse

A transpose matrix is a matrix with rows and columns reversed. The NumPy library uses the transpose() method to transpose two-dimensional matrices. Example:

arr1 = np.array([[3,3],[2,5]])
temp = arr1.transpose()
print(temp)

The result will be a matrix:

[[3 2]
 [3 5]]

To get the inverse matrix, you need to use the linalg (linear algebra) module. Let’s use the inv() function:

arr1 = np.array([[3,3],[2,5]])
temp = np.linalg.inv(arr1)
print(temp)

The resulting matrix will be:

[[ 0.55555556 -0.33333333]
 [-0.22222222 0.33333333]]

Getting the maximum and minimum element

To get the maximum or minimum element, you can loop through all the elements of the matrix using two for loops. This is a standard search algorithm, known to almost every programmer:

arr = np.array([[3,3],[2,5]])
min = arr[0][0]
for i in range(arr.shape[0])
    for j in range(arr.shape[1]):
        if min > arr[i][j]:
            min = arr[i][j]
print("Minimum element:", min) # Prints "Minimum element: 2"

NumPy allows you to find the maximum and minimum element using amax() and amin(). You must pass the matrix itself as an argument to the function. Example:

arr1 = np.array([[3,3],[2,5]])
min = np.amin(arr1)
max = np.amax(arr1)
print("Minimum element:", min) # Prints "Minimum element: 2"
print("Maximum element:", max) # Prints "Maximum element: 5"

As you can see, the results of the implementation in pure Python and the implementation using the NumPy library are the same.

Conclusion

In Python you can implement all the functions you need to work with matrices. To simplify the work of programmers, the NumPy library was created. It allows to make complex mathematical calculations easily and without errors, saving the programmer from having to write the same code every time.

Related Posts

LEAVE A COMMENT