官术网_书友最值得收藏!

  • 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:

  1. Open a new Jupyter Notebook.
  2. 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]]

  3. 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]]

  4. 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]]

  5. 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:

  1. Open a new Jupyter notebook.
  2. 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]]

  3. Create a zero-matrix placeholder to store the result:

    result = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

  4. 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.

  5. 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/.

主站蜘蛛池模板: 内丘县| 西充县| 太保市| 临朐县| 曲松县| 儋州市| 德庆县| 江都市| 长乐市| 承德县| 西贡区| 襄樊市| 清原| 英山县| 西盟| 兴文县| 永修县| 泰和县| 嘉兴市| 三河市| 甘洛县| 灵石县| 咸丰县| 岑巩县| 铅山县| 阿鲁科尔沁旗| 福海县| 磴口县| 永泰县| 江北区| 宁都县| 高青县| 东兴市| 治多县| 滨州市| 醴陵市| 米易县| 县级市| 邵东县| 安丘市| 黑河市|