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

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

主站蜘蛛池模板: 柏乡县| 绵竹市| 兴化市| 长治市| 澄迈县| 杭州市| 中方县| 邯郸县| 莒南县| 岐山县| 高平市| 宝清县| 定陶县| 德州市| 揭东县| 筠连县| 库车县| 临沧市| 东光县| 宁明县| 丰镇市| 五寨县| 扎赉特旗| 佛学| 萨迦县| 安乡县| 义马市| 大宁县| 乐山市| 柳河县| 专栏| 隆昌县| 勃利县| 甘孜县| 镇雄县| 安塞县| 四平市| 重庆市| 天长市| 福泉市| 大同市|