# Linear Algebra

## 1.1 Graph vector $\vec{a}$ 

\begin{align}
\vec{a} = \begin{bmatrix} 3 \\ 2 \end{bmatrix}
\end{align}


```python
import matplotlib.pyplot as plt

plt.arrow(0, 0, 3, 2, width=0.04)
plt.xlim(0,4)
plt.ylim(0,4);
```

## 1.2 Find $||\vec{b}||$. What does the norm of a vector represent?
\begin{align}
\vec{b} = \begin{bmatrix} 17 & -4 & -2 & 1\end{bmatrix}
\end{align}



```python
import numpy as np

norm = np.linalg.norm(np.array([17, -4, -2, 1]))
print(norm)
```

    17.60681686165901


Norm represents the magnitude/factor/distance of a vector.

## 1.3 Find $\vec{c} \cdot \vec{d}$

\begin{align}
\vec{c} = \begin{bmatrix}3 & 7 & -2 & 12\end{bmatrix}
\qquad
\vec{d} = \begin{bmatrix}9 & -7 & 4 & 6\end{bmatrix}
\end{align}


```python
c = np.array([3,  7, -2, 12])
d = np.array([9, -7,  4,  6])

print(np.dot(c, d))
```

    42


## 1.4 Find $E^{-1}$ and $E^{T}$

\begin{align}
E = 
\begin{bmatrix}
    7 & 4 & 2 \\
    1 & 3 & -1 \\
    2 & 6 & -4
\end{bmatrix}
\end{align}


```python
E = np.array([[7, 4,  2],
              [1, 3, -1],
              [2, 6, -4]])

print(np.linalg.inv(E))
print(E.T)
```

    [[ 0.17647059 -0.82352941  0.29411765]
     [-0.05882353  0.94117647 -0.26470588]
     [ 0.          1.         -0.5       ]]
    [[ 7  1  2]
     [ 4  3  6]
     [ 2 -1 -4]]


# Intermediate Linear Algebra

## 2.1 Suppose that the number of customers at a ski resort as well as the number of inches of fresh powder (snow)  was recorded for 7 days. 

### Customers: [820, 760, 1250, 990, 1080, 1450, 1600]

### Inches of new snow: [0, 1, 7, 1, 0, 6, 4 ]

## Find the mean, variance, and standard deviation for both the number of customers and inches of new snow for the week. You may use library functions, dataframes, .describe(), etc. 




```python
import pandas as pd

customers = [820, 760, 1250, 990, 1080, 1450, 1600]
snow = [0, 1, 7, 1, 0, 6, 4]

df = pd.DataFrame({'customers': customers, 'snow': snow})

df.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>customers</th>
      <th>snow</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>820</td>
      <td>0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>760</td>
      <td>1</td>
    </tr>
    <tr>
      <th>2</th>
      <td>1250</td>
      <td>7</td>
    </tr>
    <tr>
      <th>3</th>
      <td>990</td>
      <td>1</td>
    </tr>
    <tr>
      <th>4</th>
      <td>1080</td>
      <td>0</td>
    </tr>
  </tbody>
</table>
</div>




```python
df.mean()
```




    customers    1135.714286
    snow            2.714286
    dtype: float64




```python
df.std(ddof=0)
```




    customers    290.951991
    snow           2.710524
    dtype: float64




```python
df.var(ddof=0)
```




    customers    84653.061224
    snow             7.346939
    dtype: float64



## 2.2 Are the variances of the number of customers and inches of snow comparable? 
## Why or why not? 

No, customers has a much greater variance.

## 2.3 Find the variance-covariance matrix for the number of customers and inches of snow at the ski resort. 


```python
df.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>customers</th>
      <th>snow</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>customers</th>
      <td>98761.904762</td>
      <td>670.238095</td>
    </tr>
    <tr>
      <th>snow</th>
      <td>670.238095</td>
      <td>8.571429</td>
    </tr>
  </tbody>
</table>
</div>



# PCA

## 3.1 Standardize the data so that it has a mean of 0 and a standard deviation of 1. (You may use library functions)

We have included some code to get you started so that you don't get stuck on something that isn't standardizing the data or PCA.

This might be helpful:

<https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html>


```python
# Let me get you some data to start you off.
import pandas as pd

data = {"Country": ["England","Wales","Scotland","North Ireland"], 
        "Cheese": [105,103,103,66], 
        "Carcass_Meat": [245,227,242,267], 
        "Other_Meat": [685, 803, 750, 586], 
        "Fish": [147, 160, 122, 93], 
        "Fats_and_Oils": [193, 235, 184, 209], 
        "Sugars": [156, 175, 147, 139], 
        "Fresh_Potatoes": [720, 874, 566, 1033], 
        "Fresh_Veg": [253, 265, 171, 143], 
        "Other_Veg": [488, 570, 418, 355], 
        "Processed_Potatoes": [198, 203, 220, 187], 
        "Processed_Veg": [360, 365, 337, 334], 
        "Fresh_Fruit": [1102, 1137, 957, 674], 
        "Cereals": [1472, 1582, 1462, 1494], 
        "Beverages": [57,73,53,47], 
        "Soft_Drinks": [1374, 1256, 1572, 1506], 
        "Alcoholic Drinks": [375, 475, 458, 135], 
        "Confectionery": [54, 64, 62, 41]}

df = pd.DataFrame(data)

# Look at the data
df.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>Country</th>
      <th>Cheese</th>
      <th>Carcass_Meat</th>
      <th>Other_Meat</th>
      <th>Fish</th>
      <th>Fats_and_Oils</th>
      <th>Sugars</th>
      <th>Fresh_Potatoes</th>
      <th>Fresh_Veg</th>
      <th>Other_Veg</th>
      <th>Processed_Potatoes</th>
      <th>Processed_Veg</th>
      <th>Fresh_Fruit</th>
      <th>Cereals</th>
      <th>Beverages</th>
      <th>Soft_Drinks</th>
      <th>Alcoholic Drinks</th>
      <th>Confectionery</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>England</td>
      <td>105</td>
      <td>245</td>
      <td>685</td>
      <td>147</td>
      <td>193</td>
      <td>156</td>
      <td>720</td>
      <td>253</td>
      <td>488</td>
      <td>198</td>
      <td>360</td>
      <td>1102</td>
      <td>1472</td>
      <td>57</td>
      <td>1374</td>
      <td>375</td>
      <td>54</td>
    </tr>
    <tr>
      <th>1</th>
      <td>Wales</td>
      <td>103</td>
      <td>227</td>
      <td>803</td>
      <td>160</td>
      <td>235</td>
      <td>175</td>
      <td>874</td>
      <td>265</td>
      <td>570</td>
      <td>203</td>
      <td>365</td>
      <td>1137</td>
      <td>1582</td>
      <td>73</td>
      <td>1256</td>
      <td>475</td>
      <td>64</td>
    </tr>
    <tr>
      <th>2</th>
      <td>Scotland</td>
      <td>103</td>
      <td>242</td>
      <td>750</td>
      <td>122</td>
      <td>184</td>
      <td>147</td>
      <td>566</td>
      <td>171</td>
      <td>418</td>
      <td>220</td>
      <td>337</td>
      <td>957</td>
      <td>1462</td>
      <td>53</td>
      <td>1572</td>
      <td>458</td>
      <td>62</td>
    </tr>
    <tr>
      <th>3</th>
      <td>North Ireland</td>
      <td>66</td>
      <td>267</td>
      <td>586</td>
      <td>93</td>
      <td>209</td>
      <td>139</td>
      <td>1033</td>
      <td>143</td>
      <td>355</td>
      <td>187</td>
      <td>334</td>
      <td>674</td>
      <td>1494</td>
      <td>47</td>
      <td>1506</td>
      <td>135</td>
      <td>41</td>
    </tr>
  </tbody>
</table>
</div>




```python
from sklearn.preprocessing import StandardScaler

df_zstand = StandardScaler().fit_transform(df.select_dtypes('number'))
```

## 3.2 Perform PCA on the data and graph Principal Component 1 against Principal Component 2. (You may use library functions)

This might be helpful:

<https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html>


```python
from sklearn.decomposition import PCA

pca = PCA(2).fit_transform(df_zstand)

print(pca)

plt.scatter(x=[_[0] for _ in pca], y=[_[1] for _ in pca]);
```

# Clustering

## 4.1 Use K-Means to cluster the following data and then graph your results. (You may use library functions)

We have included some code to get you started so that you don't get stuck on something that isn't standardizing clustering.

Prioritize calculating the clusters over graphing them. 

Scikit-Learn K-Means Documentation:

<https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html>


```python
points = pd.read_csv('https://raw.githubusercontent.com/ryanleeallred/datasets/master/points.csv')
points.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>x</th>
      <th>y</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>-7.846803</td>
      <td>-3.421277</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-3.554323</td>
      <td>-6.884729</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-0.192822</td>
      <td>-9.671030</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-6.401456</td>
      <td>-5.223972</td>
    </tr>
    <tr>
      <th>4</th>
      <td>-0.804026</td>
      <td>-9.704457</td>
    </tr>
  </tbody>
</table>
</div>




```python
from sklearn.cluster import KMeans

sum_of_squared_distances = []
xs = range(1,15)
for x in xs:
    km = KMeans(n_clusters=x)
    km = km.fit(points)
    sum_of_squared_distances.append(km.inertia_)
```


```python
plt.plot(list(range(1,15)), sum_of_squared_distances);
```


```python
labels = KMeans(4).fit(points).labels_
```


```python
fig, ax = plt.subplots()
ax.scatter(x='x', y='y', data=points, c=labels)
ax.set_aspect('equal');
```

ff213f481f1a3ececd598237ed9df6a52e13aced
ipynb
Jupyter Notebook
05-Linear-Algebra/05_Sprint_Challenge.ipynb
shalevy1/data-science-journal
2a6beaf5bf328e257b638a695983457a9f3cd7ce
MIT
2019-03-05T04:44:48.000Z
2022-03-24T09:47:48.000Z
05-Linear-Algebra/05_Sprint_Challenge.ipynb
pesobreiro/data-science-journal
82a72b4ed5ce380988fac17b0acd97254c2b5c86
MIT
05-Linear-Algebra/05_Sprint_Challenge.ipynb
pesobreiro/data-science-journal
82a72b4ed5ce380988fac17b0acd97254c2b5c86
MIT
2019-03-07T05:08:03.000Z
2022-01-05T11:32:51.000Z
Qwen/Qwen-72B
1. YES
2. YES