Python
Python Queue: FIFO, LIFO Example
What is Python Queue? A queue is a container that holds data. The data that is entered first will...
A Python matrix is a specialized two-dimensional rectangular array of data stored in rows and columns. The data in a matrix can be numbers, strings, expressions, symbols, etc. Matrix is one of the important data structures that can be used in mathematical and scientific calculations.
In this Python tutorial, you will learn:
Step 1)
It shows a 2x2 matrix. It has two rows and 2 columns. The data inside the matrix are numbers. The row1 has values 2,3, and row2 has values 4,5. The columns, i.e., col1, have values 2,4, and col2 has values 3,5.
Step 2)
It shows a 2x3 matrix. It has two rows and three columns. The data inside the first row, i.e., row1, has values 2,3,4, and row2 has values 5,6,7. The columns col1 has values 2,5, col2 has values 3,6, and col3 has values 4,7.
So similarly, you can have your data stored inside the nxn matrix in Python. A lot of operations can be done on a matrix-like addition, subtraction, multiplication, etc.
Python does not have a straightforward way to implement a matrix data type.
The python matrix makes use of arrays, and the same can be implemented.
In Python, the arrays are represented using the list data type. So now will make use of the list to create a python matrix.
We will create a 3x3 matrix, as shown below:
The matrix inside a list with all the rows and columns is as shown below:
List = [[Row1], [Row2], [Row3] ... [RowN]]
So as per the matrix listed above the list type with matrix data is as follows:
M1 = [[8, 14, -6], [12,7,4], [-11,3,21]]
We will make use of the matrix defined above. The example will read the data, print the matrix, display the last element from each row.
M1 = [[8, 14, -6], [12,7,4], [-11,3,21]] #To print the matrix print(M1)
Output:
The Matrix M1 = [[8, 14, -6], [12, 7, 4], [-11, 3, 21]]
M1 = [[8, 14, -6], [12,7,4], [-11,3,21]] matrix_length = len(M1) #To read the last element from each row. for i in range(matrix_length): print(M1[i][-1])
Output:
-6 4 21
M1 = [[8, 14, -6], [12,7,4], [-11,3,21]] matrix_length = len(M1) #To print the rows in the Matrix for i in range(matrix_length): print(M1[i])
Output:
[8, 14, -6] [12, 7, 4] [-11, 3, 21]
We can easily add two given matrices. The matrices here will be in the list form. Let us work on an example that will take care to add the given matrices.
Matrix 1:
M1 = [[8, 14, -6], [12,7,4], [-11,3,21]]
Matrix 2 :
M2 = [[3, 16, -6], [9,7,-4], [-1,3,13]]
Last will initialize a matrix that will store the result of M1 + M2.
Matrix 3 :
M3 = [[0,0,0], [0,0,0], [0,0,0]]
To add, the matrices will make use of a for-loop that will loop through both the matrices given.
M1 = [[8, 14, -6], [12,7,4], [-11,3,21]] M2 = [[3, 16, -6], [9,7,-4], [-1,3,13]] M3 = [[0,0,0], [0,0,0], [0,0,0]] matrix_length = len(M1) #To Add M1 and M2 matrices for i in range(len(M1)): for k in range(len(M2)): M3[i][k] = M1[i][k] + M2[i][k] #To Print the matrix print("The sum of Matrix M1 and M2 = ", M3)
Output:
The sum of Matrix M1 and M2 = [[11, 30, -12], [21, 14, 0], [-12, 6, 34]]
To multiply the matrices, we can use the for-loop on both the matrices as shown in the code below:
M1 = [[8, 14, -6], [12,7,4], [-11,3,21]] M2 = [[3, 16, -6], [9,7,-4], [-1,3,13]] M3 = [[0,0,0], [0,0,0], [0,0,0]] matrix_length = len(M1) #To Multiply M1 and M2 matrices for i in range(len(M1)): for k in range(len(M2)): M3[i][k] = M1[i][k] * M2[i][k] #To Print the matrix print("The multiplication of Matrix M1 and M2 = ", M3)
Output:
The multiplication of Matrix M1 and M2 = [[24, 224, 36], [108, 49, -16], [11, 9, 273]]
The python library Numpy helps to deal with arrays. Numpy processes an array a little faster in comparison to the list.
To work with Numpy, you need to install it first. Follow the steps given below to install Numpy.
Step 1)
The command to install Numpy is :
pip install NumPy
Step 2)
To make use of Numpy in your code, you have to import it.
import NumPy
Step 3)
You can also import Numpy using an alias, as shown below:
import NumPy as np
We are going to make use of array() method from Numpy to create a python matrix.
import numpy as np M1 = np.array([[5, -10, 15], [3, -6, 9], [-4, 8, 12]]) print(M1)
Output:
[[ 5 -10 15] [ 3 -6 9] [ -4 8 12]]
The matrix operation that can be done is addition, subtraction, multiplication, transpose, reading the rows, columns of a matrix, slicing the matrix, etc. In all the examples, we are going to make use of an array() method.
To perform addition on the matrix, we will create two matrices using numpy.array() and add them using the (+) operator.
Example:
import numpy as np M1 = np.array([[3, 6, 9], [5, -10, 15], [-7, 14, 21]]) M2 = np.array([[9, -18, 27], [11, 22, 33], [13, -26, 39]]) M3 = M1 + M2 print(M3)
Output:
[[ 12 -12 36] [ 16 12 48] [ 6 -12 60]]
To perform subtraction on the matrix, we will create two matrices using numpy.array() and subtract them using the (-) operator.
Example:
import numpy as np M1 = np.array([[3, 6, 9], [5, -10, 15], [-7, 14, 21]]) M2 = np.array([[9, -18, 27], [11, 22, 33], [13, -26, 39]]) M3 = M1 - M2 print(M3)
Output:
[[ -6 24 -18] [ -6 -32 -18] [-20 40 -18]]
First will create two matrices using numpy.arary(). To multiply them will, you can make use of numpy dot() method. Numpy.dot() is the dot product of matrix M1 and M2. Numpy.dot() handles the 2D arrays and perform matrix multiplications.
Example:
import numpy as np M1 = np.array([[3, 6], [5, -10]]) M2 = np.array([[9, -18], [11, 22]]) M3 = M1.dot(M2) print(M3)
Output:
[[ 93 78] [ -65 -310]]
The transpose of a matrix is calculated, by changing the rows as columns and columns as rows. The transpose() function from Numpy can be used to calculate the transpose of a matrix.
Example:
import numpy as np M1 = np.array([[3, 6, 9], [5, -10, 15], [4,8,12]]) M2 = M1.transpose() print(M2)
Output:
[[ 3 5 4] [ 6 -10 8] [ 9 15 12]]
Slicing will return you the elements from the matrix based on the start /end index given.
Before we work on slicing on a matrix, let us first understand how to apply slice on a simple array.
import numpy as np arr = np.array([2,4,6,8,10,12,14,16]) print(arr[3:6]) # will print the elements from 3 to 5 print(arr[:5]) # will print the elements from 0 to 4 print(arr[2:]) # will print the elements from 2 to length of the array. print(arr[-5:-1]) # will print from the end i.e. -5 to -2 print(arr[:-1]) # will print from end i.e. 0 to -2
Output:
[ 8 10 12] [ 2 4 6 8 10] [ 6 8 10 12 14 16] [ 8 10 12 14] [ 2 4 6 8 10 12 14]
Now let us implement slicing on matrix . To perform slicing on a matrix
the syntax will be M1[row_start:row_end, col_start:col_end]
The matrix M1 tthat we are going to use is as follows:
M1 = np.array([[2, 4, 6, 8, 10], [3, 6, 9, -12, -15], [4, 8, 12, 16, -20], [5, -10, 15, -20, 25]])
There are total 4 rows. The index starts from 0 to 3. The 0th row is the [2,4,6,8,10], 1st row is [3,6,9,-12,-15] followed by 2nd and 3rd.
The matrix M1 has 5 columns. The index starts from 0 to 4.The 0th column has values [2,3,4,5], 1st columns have values [4,6,8,-10] followed by 2nd, 3rd, 4th, and 5th.
Here is an example showing how to get the rows and columns data from the matrix using slicing. In the example, we are printing the 1st and 2nd row, and for columns, we want the first, second, and third column. To get that output we have used: M1[1:3, 1:4]
Example:
import numpy as np M1 = np.array([[2, 4, 6, 8, 10], [3, 6, 9, -12, -15], [4, 8, 12, 16, -20], [5, -10, 15, -20, 25]]) print(M1[1:3, 1:4]) # For 1:3, it will give first and second row. #The columns will be taken from first to third.
Output:
[[ 6 9 -12] [ 8 12 16]]
import numpy as np M1 = np.array([[2, 4, 6, 8, 10], [3, 6, 9, -12, -15], [4, 8, 12, 16, -20], [5, -10, 15, -20, 25]]) print(M1[:,3]) # This will print all rows and the third column data.
Output:
[ 8 -12 16 -20]
import numpy as np M1 = np.array([[2, 4, 6, 8, 10], [3, 6, 9, -12, -15], [4, 8, 12, 16, -20], [5, -10, 15, -20, 25]]) print(M1[:1,]) # This will print first row and all columns
Output:
[[ 2 4 6 8 10]]
import numpy as np M1 = np.array([[2, 4, 6, 8, 10], [3, 6, 9, -12, -15], [4, 8, 12, 16, -20], [5, -10, 15, -20, 25]]) print(M1[:3,:2])
Output:
[[2 4] [3 6] [4 8]]
We have seen how slicing works. Taking that into consideration, we will how to get the rows and columns from the matrix.
In the example will print the rows of the matrix.
Example:
import numpy as np M1 = np.array([[3, 6, 9], [5, -10, 15], [4,8,12]]) print(M1[0]) #first row print(M1[1]) # the second row print(M1[-1]) # -1 will print the last row
Output:
[3 6 9] [ 5 -10 15] [ 4 8 12]
To get the last row, you can make use of the index or -1. For example, the matrix has 3 rows,
so M1[0] will give you the first row,
M1[1] will give you second row
M1[2] or M1[-1] will give you the third row or last row.
import numpy as np M1 = np.array([[2, 4, 6, 8, 10], [3, 6, 9, -12, -15], [4, 8, 12, 16, -20], [5, -10, 15, -20, 25]]) print(M1[:,0]) # Will print the first Column print(M1[:,3]) # Will print the third Column print(M1[:,-1]) # -1 will give you the last column
Output:
[2 3 4 5] [ 8 -12 16 -20] [ 10 -15 -20 25]
What is Python Queue? A queue is a container that holds data. The data that is entered first will...
What is Python Sleep? Python sleep() is a function used to delay the execution of code for the...
What is Lambda Function in Python? A Lambda Function in Python programming is an anonymous...
What is Python String find()? Python String find() is a function available in Python library to find...
The formula to calculate average is done by calculating the sum of the numbers in the list divided by the...
Python is the de facto language for data scientists, statisticians, machine learning experts, and...