<a href="https://colab.research.google.com/github/Avery1493/Avery1493.github.io/blob/master/module2-intermediate-linear-algebra/QuinnWorks_132_LS_DS_132_Intermediate_Linear_Algebra_Assignment.ipynb" target="_parent"></a>

# Statistics

## 1.1 Sales for the past week was the following amounts: [3505, 2400, 3027, 2798, 3700, 3250, 2689]. Without using library functions, what is the mean, variance, and standard deviation of of sales from last week? (for extra bonus points, write your own function that can calculate these two values for any sized list)


```
import numpy as np
import pandas as pd
```


```
sales = [3505,2400,3027,2798,3700,3250,2689]
print('MEAN:',np.mean(sales))
print('VAR:',np.var(sales, ddof=1))
print('STD:',np.std(sales, ddof=1))
```

    MEAN: 3052.714285714286
    VAR: 214387.90476190473
    STD: 463.0204150595357



```
#MEAN FUNCTION
def mean(data_list):
  n = len(data_list) 
  x = 0
  for i in data_list:
    x= x+i
  return x/n

mean(sales)
```




    3052.714285714286




```
#VARIENCE FUNCTION
def var(data_list):
  n = len(data_list) 
  x = mean(data_list)
  v = []
  for i in data_list:
    v= np.append([v],[(i-x)**2])
  a = v.copy()
  b = 0
  for i in a:
    b = b+i
  return b/(n-1)

var(sales)
```




    214387.90476190473




```
#STANDARD DEVIATION FUNCTION
def stdv(data_list):
  n = len(data_list) 
  x = mean(data_list)
  v = []
  for i in data_list:
    v= np.append([v],[(i-x)**2])
  a = v.copy()
  b = 0
  for i in a:
    b = b+i
 
  return (b/(n-1))**(1/2)

stdv(sales)
```




    463.0204150595357




```
#SHOWING THE SPREAD WITH MY FUNCTIONS
print('MEAN:',mean(sales))
print('VAR:',var(sales))
print('STD:',stdv(sales))
```

    MEAN: 3052.714285714286
    VAR: 214387.90476190473
    STD: 463.0204150595357


## 1.2 Find the covariance between last week's sales numbers and the number of customers that entered the store last week: [127, 80, 105, 92, 120, 115, 93] (you may use librray functions for calculating the covariance since we didn't specifically talk about its formula)


```
customers = [127,80,105,92,120,115,93]
```


```
print('MEAN:',mean(customers))
print('VAR:',var(customers))
print('STD:',stdv(customers))
```

    MEAN: 104.57142857142857
    VAR: 290.95238095238096
    STD: 17.057326313123664



```
#COVARIANCE
data = {'sales': sales, 'customers': customers}
df = pd.DataFrame(data)
df
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>sales</th>
      <th>customers</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>3505</td>
      <td>127</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2400</td>
      <td>80</td>
    </tr>
    <tr>
      <th>2</th>
      <td>3027</td>
      <td>105</td>
    </tr>
    <tr>
      <th>3</th>
      <td>2798</td>
      <td>92</td>
    </tr>
    <tr>
      <th>4</th>
      <td>3700</td>
      <td>120</td>
    </tr>
    <tr>
      <th>5</th>
      <td>3250</td>
      <td>115</td>
    </tr>
    <tr>
      <th>6</th>
      <td>2689</td>
      <td>93</td>
    </tr>
  </tbody>
</table>
</div>




```
np.cov(sales,customers)
```




    array([[214387.9047619 ,   7604.35714286],
           [  7604.35714286,    290.95238095]])




```
var_cov = df.cov()
var_cov 
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>sales</th>
      <th>customers</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>sales</th>
      <td>214387.904762</td>
      <td>7604.357143</td>
    </tr>
    <tr>
      <th>customers</th>
      <td>7604.357143</td>
      <td>290.952381</td>
    </tr>
  </tbody>
</table>
</div>




```
print('Covariance:',var_cov.sales[1])
```

    Covariance: 7604.357142857142


## 1.3 Find the standard deviation of customers who entered the store last week. Then, use the standard deviations of both sales and customers to standardize the covariance to find the correlation coefficient that summarizes the relationship between sales and customers. (You may use library functions to check your work.)




```
print('sigma: \u03C3')
```

    sigma: σ



```
σx = stdv(sales)
σy = stdv(customers)
σxy = var_cov.sales[1]
```


```
#CORRELATION COEFFICIENT
r = σxy / (σx * σy)
print('Correlation:',r)
```

    Correlation: 0.9628339778148909


## 1.4 Use pandas to import a cleaned version of the titanic dataset from the following link: [Titanic Dataset](https://raw.githubusercontent.com/Geoyi/Cleaning-Titanic-Data/master/titanic_clean.csv)

## Calculate the variance-covariance matrix and correlation matrix for the titanic dataset's numeric columns. (you can encode some of the categorical variables and include them as a stretch goal if you finish early)


```
titanic = pd.read_csv('https://raw.githubusercontent.com/Geoyi/Cleaning-Titanic-Data/master/titanic_clean.csv')
titanic = titanic.drop('Unnamed: 0', axis=1)
titanic.head()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>pclass</th>
      <th>survived</th>
      <th>name</th>
      <th>sex</th>
      <th>age</th>
      <th>sibsp</th>
      <th>parch</th>
      <th>ticket</th>
      <th>fare</th>
      <th>cabin</th>
      <th>embarked</th>
      <th>boat</th>
      <th>body</th>
      <th>home.dest</th>
      <th>has_cabin_number</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1.0</td>
      <td>1.0</td>
      <td>Allen, Miss. Elisabeth Walton</td>
      <td>female</td>
      <td>29.0000</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>24160</td>
      <td>211.3375</td>
      <td>B5</td>
      <td>S</td>
      <td>2</td>
      <td>NaN</td>
      <td>St Louis, MO</td>
      <td>1</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1.0</td>
      <td>1.0</td>
      <td>Allison, Master. Hudson Trevor</td>
      <td>male</td>
      <td>0.9167</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>113781</td>
      <td>151.5500</td>
      <td>C22 C26</td>
      <td>S</td>
      <td>11</td>
      <td>NaN</td>
      <td>Montreal, PQ / Chesterville, ON</td>
      <td>1</td>
    </tr>
    <tr>
      <th>2</th>
      <td>1.0</td>
      <td>0.0</td>
      <td>Allison, Miss. Helen Loraine</td>
      <td>female</td>
      <td>2.0000</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>113781</td>
      <td>151.5500</td>
      <td>C22 C26</td>
      <td>S</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>Montreal, PQ / Chesterville, ON</td>
      <td>1</td>
    </tr>
    <tr>
      <th>3</th>
      <td>1.0</td>
      <td>0.0</td>
      <td>Allison, Mr. Hudson Joshua Creighton</td>
      <td>male</td>
      <td>30.0000</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>113781</td>
      <td>151.5500</td>
      <td>C22 C26</td>
      <td>S</td>
      <td>NaN</td>
      <td>135.0</td>
      <td>Montreal, PQ / Chesterville, ON</td>
      <td>1</td>
    </tr>
    <tr>
      <th>4</th>
      <td>1.0</td>
      <td>0.0</td>
      <td>Allison, Mrs. Hudson J C (Bessie Waldo Daniels)</td>
      <td>female</td>
      <td>25.0000</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>113781</td>
      <td>151.5500</td>
      <td>C22 C26</td>
      <td>S</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>Montreal, PQ / Chesterville, ON</td>
      <td>1</td>
    </tr>
  </tbody>
</table>
</div>




```
titanic.describe()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>pclass</th>
      <th>survived</th>
      <th>age</th>
      <th>sibsp</th>
      <th>parch</th>
      <th>fare</th>
      <th>body</th>
      <th>has_cabin_number</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>count</th>
      <td>1309.000000</td>
      <td>1309.000000</td>
      <td>1310.000000</td>
      <td>1309.000000</td>
      <td>1309.000000</td>
      <td>1308.000000</td>
      <td>121.000000</td>
      <td>1310.000000</td>
    </tr>
    <tr>
      <th>mean</th>
      <td>2.294882</td>
      <td>0.381971</td>
      <td>29.881135</td>
      <td>0.498854</td>
      <td>0.385027</td>
      <td>33.295479</td>
      <td>160.809917</td>
      <td>0.225191</td>
    </tr>
    <tr>
      <th>std</th>
      <td>0.837836</td>
      <td>0.486055</td>
      <td>12.878277</td>
      <td>1.041658</td>
      <td>0.865560</td>
      <td>51.758668</td>
      <td>97.696922</td>
      <td>0.417867</td>
    </tr>
    <tr>
      <th>min</th>
      <td>1.000000</td>
      <td>0.000000</td>
      <td>0.166700</td>
      <td>0.000000</td>
      <td>0.000000</td>
      <td>0.000000</td>
      <td>1.000000</td>
      <td>0.000000</td>
    </tr>
    <tr>
      <th>25%</th>
      <td>2.000000</td>
      <td>0.000000</td>
      <td>22.000000</td>
      <td>0.000000</td>
      <td>0.000000</td>
      <td>7.895800</td>
      <td>72.000000</td>
      <td>0.000000</td>
    </tr>
    <tr>
      <th>50%</th>
      <td>3.000000</td>
      <td>0.000000</td>
      <td>29.881135</td>
      <td>0.000000</td>
      <td>0.000000</td>
      <td>14.454200</td>
      <td>155.000000</td>
      <td>0.000000</td>
    </tr>
    <tr>
      <th>75%</th>
      <td>3.000000</td>
      <td>1.000000</td>
      <td>35.000000</td>
      <td>1.000000</td>
      <td>0.000000</td>
      <td>31.275000</td>
      <td>256.000000</td>
      <td>0.000000</td>
    </tr>
    <tr>
      <th>max</th>
      <td>3.000000</td>
      <td>1.000000</td>
      <td>80.000000</td>
      <td>8.000000</td>
      <td>9.000000</td>
      <td>512.329200</td>
      <td>328.000000</td>
      <td>1.000000</td>
    </tr>
  </tbody>
</table>
</div>




```
titanic2 = titanic[['pclass','survived','age','sibsp','parch','fare','body','has_cabin_number']]
titanic2
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>pclass</th>
      <th>survived</th>
      <th>age</th>
      <th>sibsp</th>
      <th>parch</th>
      <th>fare</th>
      <th>body</th>
      <th>has_cabin_number</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1.0</td>
      <td>1.0</td>
      <td>29.000000</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>211.3375</td>
      <td>NaN</td>
      <td>1</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1.0</td>
      <td>1.0</td>
      <td>0.916700</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>151.5500</td>
      <td>NaN</td>
      <td>1</td>
    </tr>
    <tr>
      <th>2</th>
      <td>1.0</td>
      <td>0.0</td>
      <td>2.000000</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>151.5500</td>
      <td>NaN</td>
      <td>1</td>
    </tr>
    <tr>
      <th>3</th>
      <td>1.0</td>
      <td>0.0</td>
      <td>30.000000</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>151.5500</td>
      <td>135.0</td>
      <td>1</td>
    </tr>
    <tr>
      <th>4</th>
      <td>1.0</td>
      <td>0.0</td>
      <td>25.000000</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>151.5500</td>
      <td>NaN</td>
      <td>1</td>
    </tr>
    <tr>
      <th>...</th>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th>1305</th>
      <td>3.0</td>
      <td>0.0</td>
      <td>29.881135</td>
      <td>1.0</td>
      <td>0.0</td>
      <td>14.4542</td>
      <td>NaN</td>
      <td>0</td>
    </tr>
    <tr>
      <th>1306</th>
      <td>3.0</td>
      <td>0.0</td>
      <td>26.500000</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>7.2250</td>
      <td>304.0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>1307</th>
      <td>3.0</td>
      <td>0.0</td>
      <td>27.000000</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>7.2250</td>
      <td>NaN</td>
      <td>0</td>
    </tr>
    <tr>
      <th>1308</th>
      <td>3.0</td>
      <td>0.0</td>
      <td>29.000000</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>7.8750</td>
      <td>NaN</td>
      <td>0</td>
    </tr>
    <tr>
      <th>1309</th>
      <td>NaN</td>
      <td>NaN</td>
      <td>29.881135</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>0</td>
    </tr>
  </tbody>
</table>
<p>1310 rows × 8 columns</p>
</div>




```
#TITANIC VARIANCE COVARIANCE MATRIX
titanic2.cov()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>pclass</th>
      <th>survived</th>
      <th>age</th>
      <th>sibsp</th>
      <th>parch</th>
      <th>fare</th>
      <th>body</th>
      <th>has_cabin_number</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>pclass</th>
      <td>0.701969</td>
      <td>-0.127248</td>
      <td>-3.954605</td>
      <td>0.053090</td>
      <td>0.013287</td>
      <td>-24.227788</td>
      <td>-2.876653</td>
      <td>-0.249992</td>
    </tr>
    <tr>
      <th>survived</th>
      <td>-0.127248</td>
      <td>0.236250</td>
      <td>-0.314343</td>
      <td>-0.014088</td>
      <td>0.034776</td>
      <td>6.146023</td>
      <td>0.000000</td>
      <td>0.061406</td>
    </tr>
    <tr>
      <th>age</th>
      <td>-3.954605</td>
      <td>-0.314343</td>
      <td>165.850021</td>
      <td>-2.559806</td>
      <td>-1.459378</td>
      <td>114.416613</td>
      <td>81.622922</td>
      <td>1.463138</td>
    </tr>
    <tr>
      <th>sibsp</th>
      <td>0.053090</td>
      <td>-0.014088</td>
      <td>-2.559806</td>
      <td>1.085052</td>
      <td>0.336833</td>
      <td>8.641768</td>
      <td>-8.708471</td>
      <td>-0.003946</td>
    </tr>
    <tr>
      <th>parch</th>
      <td>0.013287</td>
      <td>0.034776</td>
      <td>-1.459378</td>
      <td>0.336833</td>
      <td>0.749195</td>
      <td>9.928031</td>
      <td>4.237190</td>
      <td>0.013316</td>
    </tr>
    <tr>
      <th>fare</th>
      <td>-24.227788</td>
      <td>6.146023</td>
      <td>114.416613</td>
      <td>8.641768</td>
      <td>9.928031</td>
      <td>2678.959738</td>
      <td>-179.164684</td>
      <td>10.976961</td>
    </tr>
    <tr>
      <th>body</th>
      <td>-2.876653</td>
      <td>0.000000</td>
      <td>81.622922</td>
      <td>-8.708471</td>
      <td>4.237190</td>
      <td>-179.164684</td>
      <td>9544.688567</td>
      <td>3.625689</td>
    </tr>
    <tr>
      <th>has_cabin_number</th>
      <td>-0.249992</td>
      <td>0.061406</td>
      <td>1.463138</td>
      <td>-0.003946</td>
      <td>0.013316</td>
      <td>10.976961</td>
      <td>3.625689</td>
      <td>0.174613</td>
    </tr>
  </tbody>
</table>
</div>



# Orthogonality

## 2.1 Plot two vectors that are orthogonal to each other. What is a synonym for orthogonal?


```
#Perpendicular 
import matplotlib.pyplot as plt

a = [0,1]
b = [1.25,0]

plt.arrow(0,0,a[0],a[1],head_width=.05, head_length=0.05)
plt.arrow(0,0,b[0],b[1],head_width=.05, head_length=0.05)

plt.xlim(-1,2)          
plt.ylim(-1,2)
plt.show()
```


```
a = np.array([0,1])
b = np.array([1.25,0])
np.dot(a,b)
```




    0.0



## 2.2 Are the following vectors orthogonal? Why or why not?

\begin{align}
a = \begin{bmatrix} -5 \\ 3 \\ 7 \end{bmatrix}
\qquad
b = \begin{bmatrix} 6 \\ -8 \\ 2 \end{bmatrix}
\end{align}


```
a = np.array([-5,3,7])
b = np.array([6,-8,2])
```


```
# Vectors a and b are orthogonal because their dot product is zero.
np.dot(a,b)
```




    -40



## 2.3 Compute the following values: What do these quantities have in common?

## What is $||c||^2$? 

## What is $c \cdot c$? 

## What is $c^{T}c$?

\begin{align}
c = \begin{bmatrix} 2 & -15 & 6 & 20 \end{bmatrix}
\end{align}


```
#NORM SQUARED
c = np.array([2,-15,6,20])
```


```
np.linalg.norm(c)**2
```




    665.0




```
#DOT PRODUCT
np.dot(c,c)
```




    665




```
#TRANSPOSED
c.T * c
```




    array([  4, 225,  36, 400])




```
#cT . c
np.dot(c.T,c)
```




    665



# Unit Vectors

## 3.1 Using Latex, write the following vectors as a linear combination of scalars and unit vectors:

\begin{align}
d = \begin{bmatrix} 7 \\ 12 \end{bmatrix}
\qquad
e = \begin{bmatrix} 2 \\ 11 \\ -8  \end{bmatrix}
\end{align}

\begin{align}
d = \begin{bmatrix} 7 \\ 12 \end{bmatrix} = 7
\begin{bmatrix} 1 \\ 0 \end{bmatrix} + 12
\begin{bmatrix} 0\\ 1 \end{bmatrix} = 7 î + 12 ĵ
\end{align} 

\begin{align}
e = \begin{bmatrix} 2 \\ 11\\-8 \end{bmatrix} = 2
\begin{bmatrix} 1 \\ 0 \\0 \end{bmatrix} + 11
\begin{bmatrix} 0\\ 1 \\0 \end{bmatrix} + -8
\begin{bmatrix} 0 \\ 0 \\1 \end{bmatrix} = 2 î + 11 ĵ -8 k̂
\end{align} 





```
print('i\u0302')
print('j\u0302')
print('k\u0302')
print('f\u0302')
```

    î
    ĵ
    k̂
    f̂


## 3.2 Turn vector $f$ into a unit vector:

\begin{align}
f = \begin{bmatrix} 4 & 12 & 11 & 9 & 2 \end{bmatrix}
\end{align}


```
#DIVIDE EACH ELEMENT BY IT'S NORM
f = np.array([[4,12,11,9,2]])
fnorm = np.linalg.norm(f)
fnorm
```




    19.131126469708992




```
f̂ = f / fnorm
f̂
```




    array([[0.20908335, 0.62725005, 0.57497921, 0.47043754, 0.10454167]])



# Linear Independence / Dependence 

## 4.1 Plot two vectors that are linearly dependent and two vectors that are linearly independent (bonus points if done in $\mathbb{R}^3$).


```
#LINEARLY DEPENDENT 
a = [1,1.8]
b = [.5,.9]

plt.arrow(0,0,a[0],a[1],head_width=.1, head_length=0.05, color='blue')
plt.arrow(0,0,b[0],b[1],head_width=.1, head_length=0.05)

plt.xlim(-1,2)          
plt.ylim(-1,2)
plt.show()
```


```
#LINEARLY DEPENDENT 
a = [.8,.4]
b = [1.6,0]

plt.arrow(0,0,a[0],a[1],head_width=.1, head_length=0.05, color='blue')
plt.arrow(0,0,b[0],b[1],head_width=.1, head_length=0.05)

plt.xlim(-1,2)          
plt.ylim(-1,2)
plt.show()
```

# Span

## 5.1 What is the span of the following vectors?

\begin{align}
g = \begin{bmatrix} 1 & 2 \end{bmatrix}
\qquad
h = \begin{bmatrix} 4 & 8 \end{bmatrix}
\end{align}


```
#THE SPAN OF THESE VECTORS IS ONE (1) BECAUSE THE ARE LINEARLY DEPENDENT
g = [1,2]
h = [4,8]
plt.arrow(0,0,h[0],h[1],head_width=.15, head_length=0.15, color = 'pink')
plt.arrow(0,0,g[0],g[1],head_width=.15, head_length=0.15, color='blue')
plt.xlim(0,5)          
plt.ylim(0,9)
plt.show()
```

## 5.2 What is the span of $\{l, m, n\}$?

\begin{align}
l = \begin{bmatrix} 1 & 2 & 3 \end{bmatrix}
\qquad
m = \begin{bmatrix} -1 & 0 & 7 \end{bmatrix}
\qquad
n = \begin{bmatrix} 4 & 8  & 2\end{bmatrix}
\end{align}


```
#THE SPAN OF THESE VECTORS IS THREE (3)
lmn = np.array([[1,2,3],
              [-1,0,7],
              [4,8,2]])

np.linalg.matrix_rank(lmn)

```




    3



# Basis

## 6.1 Graph two vectors that form a basis for $\mathbb{R}^2$




```
a = [0,1]
b = [1,0]

plt.arrow(0,0,a[0],a[1],head_width=.05, head_length=0.05, color ='#fcba03')
plt.arrow(0,0,b[0],b[1],head_width=.05, head_length=0.05, color ='#03fca5')

plt.xlim(-.2,1.2)          
plt.ylim(-.2,1.2)
plt.show()
```

## 6.2 What does it mean to form a basis?

Basis  - every vector in the vector space is linearly independent and can fill the entire vector space.

# Rank

## 7.1 What is the Rank of P?

\begin{align}
P = \begin{bmatrix} 
1 & 2 & 3 \\
 -1 & 0 & 7 \\
4 & 8  & 2
\end{bmatrix}
\end{align}


```
lmn = np.array([[1,2,3],
              [-1,0,7],
              [4,8,2]])

np.linalg.matrix_rank(lmn)
```




    3



## 7.2 What does the rank of a matrix tell us?

The rank of this matrix tell us that demensions of the vector space.



# Linear Projections

## 8.1 Line $L$ is formed by all of the vectors that can be created by scaling vector $v$ 
\begin{align}
v = \begin{bmatrix} 1 & 3 \end{bmatrix}
\end{align}

\begin{align}
w = \begin{bmatrix} -1 & 2 \end{bmatrix}
\end{align}

## find $proj_{L}(w)$

## graph your projected vector to check your work (make sure your axis are square/even)


```
v = np.array([1,3])
w = np.array([-1,2])
#L = 

plt.arrow(0,0,v[0],v[1],head_width=.3,
          head_length=0.2, color ='#fcba03') #vector v
plt.arrow(0,0,w[0],w[1],head_width=.3,
          head_length=0.2, color ='#03fca5') #vector w

plt.xlim(-4,4)          
plt.ylim(-4,4)
plt.hlines(0,-4,4)
plt.vlines(0,-4,4)
import matplotlib.patches as mpatches
gold_patch = mpatches.Patch(color='#fcba03', label='v')
teal_patch = mpatches.Patch(color='#03fca5', label='w')
plt.legend(handles=[gold_patch,teal_patch])
plt.show()
```




```
np.linalg.norm(v)**2
```




    10.000000000000002




```
projection = (np.dot(w,v) / (np.linalg.norm(v)**2))*v
projection
```




    array([0.5, 1.5])




```
#projL(w)
plt.xlim(-4,4)          
plt.ylim(-2,4)
plt.hlines(0,-4,4)
plt.vlines(0,-4,4)

v = np.array([1,3])
w = np.array([-1,2])
x= np.linspace(-5,5)
y=3*x
plt.plot(x,y,color = 'dimgray', linestyle = '--') #Line L

plt.arrow(0,0,v[0],v[1],head_width=.3, head_length=0.2,
          color ='#fcba03', width=.05) #vector v
plt.arrow(0,0,w[0],w[1],head_width=.3, head_length=0.2,
          color ='#03fca5', width=.05) #vector w
plt.arrow(0,0,projection[0],projection[1],head_width=.3,
          head_length=0.2, color ='#e834eb',
          width=.05) #vector w projected on v (line L)
plt.plot((.5,-1),(1.5,2), linestyle='--')

gold_patch = mpatches.Patch(color='#fcba03', label='v')
teal_patch = mpatches.Patch(color='#03fca5', label='w')
purple_patch = mpatches.Patch(color='#e834eb', label='p')
pink_patch = mpatches.Patch(color='dimgray', label='L')
plt.legend(handles=[gold_patch,teal_patch,purple_patch,pink_patch])

plt.show()
```

# Stretch Goal

## For vectors that begin at the origin, the coordinates of where the vector ends can be interpreted as regular data points. (See 3Blue1Brown videos about Spans, Basis, etc.)

## Write a function that can calculate the linear projection of each point (x,y) (vector) onto the line y=x. run the function and plot the original points in blue and the new projected points on the line y=x in red. 

## For extra points plot the orthogonal vectors as a dashed line from the original blue points to the projected red points.


```
import pandas as pd
import matplotlib.pyplot as plt

# Creating a dataframe for you to work with -Feel free to not use the dataframe if you don't want to.
x_values = [1, 4, 7, 3, 9, 4, 5 ]
y_values = [4, 2, 5, 0, 8, 2, 8]

data = {"x": x_values, "y": y_values}

df = pd.DataFrame(data)

df.head()

plt.scatter(df.x, df.y)
plt.show()
```


```

```

0601ddf64aa7e0a3705128f5609fcf76b5f0f204
ipynb
Jupyter Notebook
module2-intermediate-linear-algebra/QuinnWorks_132_LS_DS_132_Intermediate_Linear_Algebra_Assignment.ipynb
Avery1493/Avery1493.github.io
f0d7205dd52d49935546a9839de24368d9cdb097
MIT
module2-intermediate-linear-algebra/QuinnWorks_132_LS_DS_132_Intermediate_Linear_Algebra_Assignment.ipynb
Avery1493/Avery1493.github.io
f0d7205dd52d49935546a9839de24368d9cdb097
MIT
module2-intermediate-linear-algebra/QuinnWorks_132_LS_DS_132_Intermediate_Linear_Algebra_Assignment.ipynb
Avery1493/Avery1493.github.io
f0d7205dd52d49935546a9839de24368d9cdb097
MIT
Qwen/Qwen-72B
1. YES
2. YES