<a href="https://colab.research.google.com/github/paulc00/ML-Intro/blob/master/Linear_Algebra_Basics.ipynb" target="_parent"></a>

# Some Linear Algebra Basics
## Python Imports
At the beginning of our Python source code we can import packages to extend the functionality of Python. 

In this case we will be importing the NumPy library which supports *efficient* linear algebra calculations.


```
# Import the NumPy library for linear algebra

# we assign 'np' as an alias that we can use later when calling or using
# anything in the NumPy package
import numpy as np
```

## Summing a vector
The following cells will create a vector $$x = \begin{bmatrix}3&1&4&2\end{bmatrix}$$ and then calculate the sum of its elements, $$s = \sum_{i=1}^n x_i$$


```
# Create a vector, x

# np.array is used to define vectors or matrices in NumPy. Here we will 
# initialize our arrays with python lists.
x = np.array([3, 1, 4, 2])

# Display x (using an f-string)
print(f"x =\n{x}")
```

    x =
    [3 1 4 2]



```
# Calculate the sum the elements of x
s = x.sum()

# Display the sum
print(f"Sum of elements of x = {s}")
```

    Sum of elements of x = 10


## Vector Dot-Product
In the next few cells we will create a new vector $y = \begin{bmatrix}1\\2\\3\\4\end{bmatrix}$.

Then we'll calculate the dot product of $x$ and $y$, $$d = xy = x \cdot y = \begin{bmatrix}3&1&4&2\end{bmatrix} \cdot \begin{bmatrix}1\\2\\3\\4\end{bmatrix}$$

*Please note: While linear algebra rules for the dot-product demand that number of columns in the first argument equal the number of rows in the second argument, NumPy will automatically transpose one of the vectors if two row vectors or two column vectors are passed. So, in NumPy, $\begin{bmatrix}3&1&4&2\end{bmatrix} \cdot \begin{bmatrix}1&2&3&4\end{bmatrix}$ or $\begin{bmatrix}3\\1\\4\\2\end{bmatrix} \cdot \begin{bmatrix}1\\2\\3\\4\end{bmatrix}$ will produce the same dot-product as shown above.*



```
# Create a column vector y
y = np.array([[1],
              [2],
              [3],
              [4]])

# Display our vectors
print(f"x =\n{x}\n\ny =\n{y}")
```

    x =
    [3 1 4 2]
    
    y =
    [[1]
     [2]
     [3]
     [4]]



```
# Calculate the dot product of x and y
d = x @ y

# Display the dot product
print(f"d = x . y = {d}")
```

    d = x . y = [25]


## Matrix Dot-Product
In the remaining cells we create two matrices $X = \begin{bmatrix}3&1\\4&2\end{bmatrix}$ and $Y = \begin{bmatrix}1&2\\3&4\end{bmatrix}$.

Then we'll calculate the dot product of $X$ and $Y$,$$D = XY = X\cdot Y = \begin{bmatrix}3&1\\4&2\end{bmatrix} \cdot \begin{bmatrix}1&2\\3&4\end{bmatrix}$$


```
# Create two matrices
X = np.array([[3, 1], 
              [4, 2]])          # This definition is easier to read

Y = np.array([[1, 2], [3, 4]])  # This definition is more compact

# Display our matrices
print(f"X =\n{X}\n\nY =\n{Y}")
```

    X =
    [[3 1]
     [4 2]]
    
    Y =
    [[1 2]
     [3 4]]



```
# Calculate the dot product of X and Y
D = X @ Y

# Display the dot product
print(f"D = X . Y =\n{D}")
```

    D = X . Y =
    [[ 6 10]
     [10 16]]


## Exercises

### Exercise 1

Calculate and display $$\begin{bmatrix}3&1&4\end{bmatrix} \cdot \begin{bmatrix}1\\2\\3\end{bmatrix}$$


```
# Create the first vector

# Create the second vector

# Calculate the dot-product

# Display the dot-product

```

### Exercise 2

Calculate and display $$\begin{bmatrix}5&3\\1&2\end{bmatrix} \cdot \begin{bmatrix}7&4\\6&8\end{bmatrix}$$


```
# Create the first matrix

# Create the second matrix

# Calculate the dot-product

# Display the dot-product

```

### Exercise 3

Calculate and display $$\begin{align}x &= \begin{bmatrix}3&1&4\end{bmatrix}\\z &= \sum_{i=1}^n x_i\end{align}$$


```
# Create the vector
# x = ...

# Calculate the sum
# s = ...

# Display the dot-product
# print(f"... = {...}")
```

594347bce6c1281caddc2a57b8b61be7bb1e5a04
ipynb
Jupyter Notebook
notebooks/extras/Linear_Algebra_Basics.ipynb
paulc00/ML-Intro
ac901ec5947f29da7e94fc5badc94391235e2609
MIT
2019-10-17T01:16:47.000Z
2019-10-17T01:16:47.000Z
notebooks/extras/Linear_Algebra_Basics.ipynb
paulc00/ML-Intro
ac901ec5947f29da7e94fc5badc94391235e2609
MIT
notebooks/extras/Linear_Algebra_Basics.ipynb
paulc00/ML-Intro
ac901ec5947f29da7e94fc5badc94391235e2609
MIT
Qwen/Qwen-72B
1. YES
2. YES