October 2019
Beginner to intermediate
674 pages
16h 43m
English
Some of the NumPy array-creation routines are extremely useful to perform matrix operations such as constructing the diagonal matrix (diag), the upper triangular matrix (triu), and the lower triangular matrix (tril).
The diag function works only on 1D and 2D arrays. If the input array is 2D, the output is a 1D array with the diagonal elements of the input array. If the input is a 1D array, the output is a matrix with the input array along its diagonal. Here, a parameter k helps to offset the position from the main diagonal and can be positive or negative:
# The 2D input matrix for diag functionIn [68]: arr_a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])In [69]: arr_aOut [69]:array([[1, 2, 3],[4, 5, 6],[7, 8, ...
Read now
Unlock full access