Basic linear algebra operations

Linear algebra constitutes a set of vital operations for matrices and arrays. The NumPy package is built with a special module called linalg to deal with all linear algebra requirements. The following segment discusses some frequently used functions of the linalg module in detail.

The dot function of the linalg module helps in matrix multiplication. For 2D arrays, it behaves exactly like matrix multiplication. It requires the last dimension of the first array to be equal to the last dimension of the second array. The arrays need not have equal numbers of dimensions. For an  N-dimensional array, the output will have 2N-2 dimensions:

# For 2D arrays
In [23]: array_1 = np.random.randn(2, 4)
In [24]: array_2 = np.random.randn(4, 2)
In [25]: np.dot(array_1, array_2)
Out[25]:
array([[-2.89783151, 5.34861977],
[-0.98078998, -3.47603638]])

# For N dimensional arrays
In [37]: array_1 = np.random.randn(2, 4, 2)
In [38]: array_2 = np.random.randn(1, 1, 2, 1)
In [39]: np.dot(array_1, array_2).shape
Out[39]: (2, 4, 1, 1, 1)

The linalg.multidot function can help in computing the product of several arrays at once, instead of using a nested sequence of dot functions. This function automatically finds the most efficient order for evaluating the sequence of products.

The linalg.svd function helps in singular value decomposition and returns three arrays as the result of decomposition. It accepts an array with two or more dimensions as the input:

In [42]: array_svd = np.random.randn(4, 3)
In [43]: np.linalg.svd(array_svd)
Out[43]:
(array([[-0.31366226, 0.27266983, 0.17962633, -0.89162858],
[ 0.72860587, 0.51810374, 0.44793275, -0.00763141],
[-0.59309456, 0.61499855, 0.26103908, 0.44930416],
[-0.13779807, -0.52820115, 0.83603183, 0.05537156]]),
array([1.68668514, 0.91044852, 0.65293131]),
array([[ 0.43322222, 0.10710679, 0.89490035],
[-0.73052453, 0.62326903, 0.27905131],
[-0.52787538, -0.77463789, 0.34825813]]))

Eigenvalues and eigenvectors of an array can be calculated with the linalg.eig function. The eig function requires the last two dimensions of the input array to be a square. The same function returns both the eigenvalues and the eigenvectors:

In [50]: np.linalg.eig(np.random.randn(5, 5))
Out[50]:
(array([ 2.52146488+0.j , -2.80191144+0.j ,
0.57756977+0.j , -0.65032217+1.22149327j,
-0.65032217-1.22149327j]),
array([[-0.85628289+0.j , -0.04688595+0.j ,
-0.71887813+0.j , -0.51046122-0.03158232j,
-0.51046122+0.03158232j],
[ 0.15793025+0.j , 0.7517844 +0.j ,
0.45393309+0.j , 0.52887467+0.j ,
0.52887467-0.j ],
[-0.35226803+0.j , 0.33640372+0.j ,
0.51482125+0.j , 0.40554944-0.02802925j,
0.40554944+0.02802925j],
[ 0.08722806+0.j , -0.07904384+0.j ,
-0.03872718+0.j , -0.41252898+0.16212983j,
-0.41252898-0.16212983j],
[ 0.33186767+0.j , 0.55964858+0.j ,
0.10304501+0.j , 0.14346541-0.27643973j,
0.14346541+0.27643973j]]))

The linalg module also has functions to solve linear equations. The linalg.solve function takes in a coefficient matrix and the dependent variable, and solves for the exact solution. It requires that all rows of the coefficient matrix must be linearly independent:

In [51]: a = np.array([[1, 2, 3], [5, 4, 2], [8, 9, 7]])
In [52]: b = np.array([6, 19, 47])
In [53]: np.linalg.solve(a, b)
Out[53]: array([-6.27272727, 15.81818182, -6.45454545])

If the best possible solution is needed instead of the exact solution, the least-squares solution could be obtained from the linalg.lstsq function.

The linalg.det function computes the determinant of a square array. If there are more than two dimensions in the input array, it is treated as a stack of matrices and the determinant is computed for each stack. The last two dimensions must, however, correspond to a square matrix:

In [55]: np.linalg.det(np.random.randn(3,3))
Out[55]: -0.08292700167707867
In [56]: np.linalg.det(np.random.randn(2,3,3))
Out[56]: array([-0.22575897, 1.47647984])

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.189.150