Linear Algebra

What is Linear Algebra?

Linear algebra is the branch of mathematics concerning linear equations such as linear maps such as and their representations in vector spaces and through matrices. Linear algebra is central to almost all areas of mathematics See More

Applications of Linear Algebra in Data Science

  • Loss Functions

  • Regularization

  • Covariance Matrix

  • Support Vector Machine Classification

  • Principal Component Analysis (PCA)

  • Singular Value Decomposition

  • Word Embeddings

  • Latent Semantic Analysis (LSA)

  • Image Representation as Tensors

  • Convolution and Image Processing

SEE DETAILS: https://www.analyticsvidhya.com/blog/2019/07/10-applications-linear-algebra-data-science/

Linear Algebra Operations

# import numpy
import numpy as np 

Determinant of matrix

  • The determinant of a matrix is a special number that can be calculated from a square matrix \begin{equation} A = \begin{bmatrix} a & b \ c & d \end{bmatrix} \ det(A) = ad - bc \end{equation} Whre, A is a \(2 \times 2\) matrix.

  • np.linalg.det() - performs determinant of the matrix

  • Syntax: np.linalg.det(matrix)

Source: https://www.mathsisfun.com/algebra/matrix-determinant.html

# create matrix A
A = np.matrix("4, 5, 16, 7; 2,-3,2,3; 3,4,5,6; 4,7,8,9")
print(A)
[[ 4  5 16  7]
 [ 2 -3  2  3]
 [ 3  4  5  6]
 [ 4  7  8  9]]
# create matrix B
B = np.matrix("4,5,6,7;2,-3,3,3; 3,4,5,6; 4, 7,8,9")
print(B)
[[ 4  5  6  7]
 [ 2 -3  3  3]
 [ 3  4  5  6]
 [ 4  7  8  9]]
# determinant of A 
np.linalg.det(A) 
128.00000000000009
# determinant of B 
np.linalg.det(B)

Rank of matrix

  • np.linalg.matrix_rank() - returns rank of the matrix

  • Syntax: np.linalg.matrix_rank(matrix)

# rank of matrix A 
np.linalg.matrix_rank(A)
4
# rank of matrix B 
np.linalg.matrix_rank(B)

Inverse of a Matrix

  • np.linalg.inv() - returns the multiplicative inverse of a matrix.

  • Syntax: np.linalg.inv(matrix)

# inverse of matrix A 
np.linalg.inv(A)
matrix([[ 9.37500000e-02, -4.68750000e-01,  3.68750000e+00,
         -2.37500000e+00],
        [ 3.53252781e-17, -2.50000000e-01,  5.00000000e-01,
         -2.50000000e-01],
        [ 9.37500000e-02,  3.12500000e-02, -3.12500000e-01,
          1.25000000e-01],
        [-1.25000000e-01,  3.75000000e-01, -1.75000000e+00,
          1.25000000e+00]])
# inverse of matrix B 
np.linalg.inv(B)

System of linear equations

Consider a system of equations $\(3x + y + 2z = 2\)\( \)\(3x + 2y + 5z = -1\)\( \)\(6x + 7y + 8z = 3\)$

Now we can write the equations in the form of \(Ax = b\)

  • np.linalg.matrix_rank() - returns rank of the matrix

  • Syntax: np.linalg.matrix_rank(matrix)

### Rank of matrix 
- `np.linalg.matrix_rank()` - returns rank of the matrix 
- **Syntax:** `np.linalg.matrix_rank(matrix)`