# Linear Algebra 

## What is Linear Algebra? 
Linear algebra is a branch of mathematics, but the truth of it is that linear algebra is the mathematics of data. Matrices and vectors are the language of data [Learn More](https://machinelearningmastery.com/gentle-introduction-linear-algebra/).

## 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 


```python
# 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


```python
# 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]]



```python
# 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]]



```python
# determinant of A 
np.linalg.det(A) 
```




    128.00000000000009




```python
# determinant of B 
np.linalg.det(B)
```




    12.000000000000005



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


```python
A.shape 
```




    (4, 4)




```python
# rank of matrix A 
np.linalg.matrix_rank(A)
```




    4




```python
# rank of matrix B 
np.linalg.matrix_rank(B)
```




    4



### Inverse of a Matrix 
- `np.linalg.inv()` - returns the multiplicative inverse of a matrix.
- **Syntax:** `np.linalg.inv(matrix)`


```python
# 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]])




```python
# inverse of matrix B 
np.linalg.inv(B)
```




    matrix([[ 1.50000000e+00, -2.08166817e-17, -1.00000000e+00,
             -5.00000000e-01],
            [ 2.50000000e-01, -1.66666667e-01, -3.33333333e-01,
              8.33333333e-02],
            [ 1.00000000e+00,  3.33333333e-01, -3.33333333e+00,
              1.33333333e+00],
            [-1.75000000e+00, -1.66666667e-01,  3.66666667e+00,
             -9.16666667e-01]])



6354dc8b678ba290ad0c9cab20cfb12507c2e5cc
ipynb
Jupyter Notebook
03 - Working with NumPy/notebooks/09-Linear Algebra.ipynb
hossainlab/PY4R
cd905ce424de67b4b9fea2b371f286db9d147cb6
CC0-1.0
2022-01-11T16:38:47.000Z
2022-01-19T20:46:28.000Z
03 - Working with NumPy/notebooks/09-Linear Algebra.ipynb
hossainlab/PY4R
cd905ce424de67b4b9fea2b371f286db9d147cb6
CC0-1.0
03 - Working with NumPy/notebooks/09-Linear Algebra.ipynb
hossainlab/PY4R
cd905ce424de67b4b9fea2b371f286db9d147cb6
CC0-1.0
2021-09-06T13:05:27.000Z
2021-09-11T14:42:27.000Z
Qwen/Qwen-72B
1. YES
2. YES