- The Python Workshop
- Andrew Bird Dr Lau Cher Han Mario Corchero Jiménez Graham Lee Corey Wade
- 813字
- 2021-06-11 12:51:28
Matrix Operations
You will continue to look at how to use nested lists for some basic matrix operations. First, you look at how to add two matrices in Python. Matrix addition requires both matrices to have the same dimensions; the results will also be of the same dimensions.
In Exercise 23, Implementing Matrix Operations (Addition and Subtraction), you will be using the following matrix data, X and Y, in figures 2.7 and 2.8:

Figure 2.7: Matrix data for matrix X

Figure 2.8: Matrix data for matrix Y
Exercise 23: Implementing Matrix Operations (Addition and Subtraction)
In this exercise you will add and subtract the X and Y matrixes using Python.
The following steps will enable you to complete the exercise:
- Open a new Jupyter Notebook.
- Create two nested lists, X and Y, to store the values:
X = [[1,2,3],[4,5,6],[7,8,9]]
Y = [[10,11,12],[13,14,15],[16,17,18]]
- Initialize a 3 x 3 zero matrix called result as a placeholder:
# Initialize a result placeholder
result = [[0,0,0],
[0,0,0],
[0,0,0]]
- Now, implement the algorithm by iterating through the cells and columns of the matrix:
# iterate through rows
for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
print(result)
You'll use the nested list method. As you learned in the previous section, you first iterate the rows in matrix X, then iterate the columns. You do not have to iterate matrix Y again because both matrixes are of the same dimensions. The result of a particular row (denoted by i) and a particular column (denoted by j) equals the sum of the respective row and column in matrixes X and Y.
You should get the following output:
[[11, 13, 15], [17, 19, 21], [23, 25, 27]]
- You can also perform subtraction using two matrices using the same algorithm with a different operator. The idea behind this is exactly the same as in step 3, except you are doing subtraction. You can implement the following code to try out matrix subtraction:
X = [[10,11,12],[13,14,15],[16,17,18]]
Y = [[1,2,3],[4,5,6],[7,8,9]]
# Initialize a result placeholder
result = [[0,0,0],
[0,0,0],
[0,0,0]]
# iterate through rows
for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[i][j] = X[i][j] - Y[i][j]
print(result)
You should get the following output:
[[9, 9, 9], [9, 9, 9], [9, 9, 9]]
In this exercise, you were able to perform basic addition and subtraction using two matrices. In the next topic, you will be using multiplication operators for matrixes.
Matrix Multiplication Operations
You can look at how to use nested lists to perform matrix multiplication for the two matrices shown in figures 2.9 and 2.10:

Figure 2.9: The data of matrix X

Figure 2.10: The data of matrix Y
For a matrix multiplication operation, the number of columns in the first matrix (X) must be equal the number of rows in the second matrix (Y). The result will have the same number of rows as the first matrix and the same number of columns as the second matrix. In this case, the result matrix will be a 3 x 4 matrix.
Exercise 24: Implementing Matrix Operations (Multiplication)
In this exercise, your end goal will be to multiply two matrixes, X and Y, and get an output value. The following steps will enable you to complete the exercise:
- Open a new Jupyter notebook.
- Create two nested lists, X and Y, to store the value of matrices X and Y:
X = [[1, 2], [4, 5], [3, 6]]
Y = [[1,2,3,4],[5,6,7,8]]
- Create a zero-matrix placeholder to store the result:
result = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
- Implement the matrix multiplication algorithm to compute the result:
# iterating by row of X
for i in range(len(X)):
# iterating by column by Y
for j in range(len(Y[0])):
# iterating by rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
You may have noticed that this algorithm is slightly different from the one you used in Exercise 23, Implementing Matrix Operations (Addition and Subtraction), step 3. This is because you need to iterate the rows of the second matrix, Y, as the matrixes have different shapes, which is what is mentioned in the preceding code snippet.
- Now, print the final result:
for r in result:
print(r)
You should get the following output:

Figure 2.11: Output of multiplying matrix X and matrix Y
Note
There are packages that data scientists use to perform matrix calculations, such as NumPy. You can find out more at https://docs.scipy.org/doc/numpy/.
- 大數(shù)據(jù)戰(zhàn)爭(zhēng):人工智能時(shí)代不能不說(shuō)的事
- 計(jì)算機(jī)圖形學(xué)
- Hadoop 2.x Administration Cookbook
- 樂(lè)高創(chuàng)意機(jī)器人教程(中級(jí) 下冊(cè) 10~16歲) (青少年iCAN+創(chuàng)新創(chuàng)意實(shí)踐指導(dǎo)叢書(shū))
- 數(shù)據(jù)挖掘方法及天體光譜挖掘技術(shù)
- 控制系統(tǒng)計(jì)算機(jī)仿真
- JavaScript典型應(yīng)用與最佳實(shí)踐
- 具比例時(shí)滯遞歸神經(jīng)網(wǎng)絡(luò)的穩(wěn)定性及其仿真與應(yīng)用
- 聊天機(jī)器人:入門(mén)、進(jìn)階與實(shí)戰(zhàn)
- Google SketchUp for Game Design:Beginner's Guide
- 空間機(jī)械臂建模、規(guī)劃與控制
- Applied Data Visualization with R and ggplot2
- 典型Hadoop云計(jì)算
- 手把手教你學(xué)Flash CS3
- 電氣控制及Micro800 PLC程序設(shè)計(jì)