<a href="https://colab.research.google.com/github/jazzathoth/AB-Demo/blob/master/Linear_Algebra_Sprint_Challenge.ipynb" target="_parent"></a>


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

#Linear Algebra

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

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


```
# define a 
a = [3,2]

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

plt.xlim(0,5)          
plt.ylim(0,5)
```

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


- The norm of a vector  (||vector||) is its length.


```
# Define b
b = [17, -4, -2, 1]

# Find the length of b
norm_b = sum([x**2 for x in b])**.5
norm_b
```




    17.60681686165901



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


```
# Define c and d
c = [3, 7, -2, 12]
d = [9, -7, 4, 6]

# Do manual dot product of c and d
c_dot_d = [(c[0]*d[0] + c[1]*d[1] + c[2]*d[2] + c[3]*d[3])]

# Print results and check with builtin
print(c_dot_d, '\nCheck:\n', np.dot(c,d))
```

    [42] 
    Check:
     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}


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

# E transpose manually
E_Trans = np.array([[7,1,2],[4,3,6],[2,-1,-4]])

# Determinant manually
E_det = (7 *((3*-4) - (-1*6)) + 4*((-1*2) - (1*-4)) + 2*((1*6) - (3*2)))

E_inv = np.linalg.inv(E)

print(E_inv,'\n\n', E_Trans,'\n\n', E_det, '\n\nCheck Det:', np.linalg.det(E))
```

    [[ 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]] 
    
     -34 
    
    Check Det: -34.00000000000001


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




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




```
df.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>customers</th>
      <th>snow</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>count</th>
      <td>7.000000</td>
      <td>7.000000</td>
    </tr>
    <tr>
      <th>mean</th>
      <td>1135.714286</td>
      <td>2.714286</td>
    </tr>
    <tr>
      <th>std</th>
      <td>314.264069</td>
      <td>2.927700</td>
    </tr>
    <tr>
      <th>min</th>
      <td>760.000000</td>
      <td>0.000000</td>
    </tr>
    <tr>
      <th>25%</th>
      <td>905.000000</td>
      <td>0.500000</td>
    </tr>
    <tr>
      <th>50%</th>
      <td>1080.000000</td>
      <td>1.000000</td>
    </tr>
    <tr>
      <th>75%</th>
      <td>1350.000000</td>
      <td>5.000000</td>
    </tr>
    <tr>
      <th>max</th>
      <td>1600.000000</td>
      <td>7.000000</td>
    </tr>
  </tbody>
</table>
</div>



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

Varaince between snow and customers are not comparable because they are measuring completely different things. We could as easily change snow units to milimeters and the variance would increase because the measurements are in larger numbers, but the number of customers would remain the same even if they are causally linked because the measurement does not effect the actual amount of snow.


```
df.var()
```




    customers    98761.904762
    snow             8.571429
    dtype: float64



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


```
#making sure this is behaving like I expected
df.T.values
```




    array([[ 820,  760, 1250,  990, 1080, 1450, 1600],
           [   0,    1,    7,    1,    0,    6,    4]])




```
# Use pandas variance covariance matrix function
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>




```
# Numpy's var/cov matrix function. has more controll, sometimes one of these
# does not behave like it should, aways good to check them against eachother
np.cov(df.T)
```




    array([[9.87619048e+04, 6.70238095e+02],
           [6.70238095e+02, 8.57142857e+00]])




```
# Show correlation
df.corr()
```




<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>1.000000</td>
      <td>0.728463</td>
    </tr>
    <tr>
      <th>snow</th>
      <td>0.728463</td>
      <td>1.000000</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>


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

df1 = pd.DataFrame(data)

# Look at the data
df1.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>




```
df2 = df1.drop('Country', axis=1)
```


```
df2means = df2.mean()
df2std = df2.std()
df2means
```




    Cheese                  94.25
    Carcass_Meat           245.25
    Other_Meat             706.00
    Fish                   130.50
    Fats_and_Oils          205.25
    Sugars                 154.25
    Fresh_Potatoes         798.25
    Fresh_Veg              208.00
    Other_Veg              457.75
    Processed_Potatoes     202.00
    Processed_Veg          349.00
    Fresh_Fruit            967.50
    Cereals               1502.50
    Beverages               57.50
    Soft_Drinks           1427.00
    Alcoholic Drinks       360.75
    Confectionery           55.25
    dtype: float64




```
cendf = (df2 - df2means.values) / df2std
covdf = cendf.T.cov()
```


```
covdf
```




<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>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0.199633</td>
      <td>0.058023</td>
      <td>-0.031143</td>
      <td>-0.226513</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0.058023</td>
      <td>0.625020</td>
      <td>-0.273208</td>
      <td>-0.409836</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-0.031143</td>
      <td>-0.273208</td>
      <td>0.520300</td>
      <td>-0.215948</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-0.226513</td>
      <td>-0.409836</td>
      <td>-0.215948</td>
      <td>0.852297</td>
    </tr>
  </tbody>
</table>
</div>




```
cendf
```




<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>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>0.570083</td>
      <td>-0.015152</td>
      <td>-0.224774</td>
      <td>0.558227</td>
      <td>-0.547700</td>
      <td>0.113060</td>
      <td>-0.389777</td>
      <td>0.747650</td>
      <td>0.327129</td>
      <td>-0.291214</td>
      <td>0.697564</td>
      <td>0.638613</td>
      <td>-0.557999</td>
      <td>-0.044962</td>
      <td>-0.376921</td>
      <td>0.090931</td>
      <td>-0.119774</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0.464021</td>
      <td>-1.106061</td>
      <td>1.038240</td>
      <td>0.998043</td>
      <td>1.330128</td>
      <td>1.340571</td>
      <td>0.377324</td>
      <td>0.947023</td>
      <td>1.213892</td>
      <td>0.072804</td>
      <td>1.014638</td>
      <td>0.804795</td>
      <td>1.454458</td>
      <td>1.393816</td>
      <td>-1.216104</td>
      <td>0.729040</td>
      <td>0.838419</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0.464021</td>
      <td>-0.196970</td>
      <td>0.470954</td>
      <td>-0.287572</td>
      <td>-0.950091</td>
      <td>-0.468392</td>
      <td>-1.156878</td>
      <td>-0.614734</td>
      <td>-0.429864</td>
      <td>1.310463</td>
      <td>-0.760979</td>
      <td>-0.049855</td>
      <td>-0.740950</td>
      <td>-0.404656</td>
      <td>1.031200</td>
      <td>0.620562</td>
      <td>0.646780</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-1.498124</td>
      <td>1.318182</td>
      <td>-1.284421</td>
      <td>-1.268698</td>
      <td>0.167663</td>
      <td>-0.985239</td>
      <td>1.169331</td>
      <td>-1.079939</td>
      <td>-1.111157</td>
      <td>-1.092053</td>
      <td>-0.951223</td>
      <td>-1.393554</td>
      <td>-0.155508</td>
      <td>-0.944198</td>
      <td>0.561826</td>
      <td>-1.440533</td>
      <td>-1.365425</td>
    </tr>
  </tbody>
</table>
</div>




```
from sklearn.preprocessing import StandardScaler
```


```
scale = StandardScaler()
```


```
x = scale.fit(df2)
```

    /usr/local/lib/python3.6/dist-packages/sklearn/preprocessing/data.py:645: DataConversionWarning: Data with input dtype int64 were all converted to float64 by StandardScaler.
      return self.partial_fit(X, y)





    StandardScaler(copy=True, with_mean=True, with_std=True)




```
print(scale.fit_transform(df2))
```

    [[ 0.65827466 -0.01749546 -0.25954622  0.64458535 -0.63242919  0.13055067
      -0.45007561  0.86331183  0.37773603 -0.33626508  0.80547723  0.73740709
      -0.64432226 -0.05191741 -0.43523112  0.10499766 -0.13830319]
     [ 0.53580495 -1.27716878  1.19885635  1.15244047  1.53589947  1.54795798
       0.43569619  1.09352832  1.40168163  0.08406627  1.17160324  0.9292974
       1.67946294  1.60943981 -1.40423624  0.84182336  0.96812236]
     [ 0.53580495 -0.22744102  0.54381113 -0.33205912 -1.09707105 -0.54085279
      -1.33584741 -0.70983418 -0.49636387  1.51319287 -0.87870243 -0.0575671
      -0.85557546 -0.46725672  1.19072664  0.71656299  0.74683725]
     [-1.72988456  1.52210526 -1.48312126 -1.4649667   0.19360077 -1.13765587
       1.35022682 -1.24700598 -1.28305378 -1.26099406 -1.09837804 -1.60913739
      -0.17956522 -1.09026568  0.64874072 -1.66338402 -1.57665641]]


    /usr/local/lib/python3.6/dist-packages/sklearn/preprocessing/data.py:645: DataConversionWarning: Data with input dtype int64 were all converted to float64 by StandardScaler.
      return self.partial_fit(X, y)
    /usr/local/lib/python3.6/dist-packages/sklearn/base.py:464: DataConversionWarning: Data with input dtype int64 were all converted to float64 by StandardScaler.
      return self.fit(X, **fit_params).transform(X)



```
scale.mean_
```




    array([  94.25,  245.25,  706.  ,  130.5 ,  205.25,  154.25,  798.25,
            208.  ,  457.75,  202.  ,  349.  ,  967.5 , 1502.5 ,   57.5 ,
           1427.  ,  360.75,   55.25])




```

```

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


```
from sklearn.decomposition import PCA
```


```
pca = PCA(2)
pca.fit(df2.T)

df_evec = pd.DataFrame(pca.components_)
```


```
df_evec
```




<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>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0.490157</td>
      <td>0.498170</td>
      <td>0.504236</td>
      <td>0.507266</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0.280842</td>
      <td>0.347597</td>
      <td>0.248220</td>
      <td>-0.859471</td>
    </tr>
  </tbody>
</table>
</div>




```
plt.scatter(df_evec.loc[0], df_evec.loc[1])
```

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


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




```
# Look at data to see if clusters number can be guessed.
# I am calling it 4, although if ther's time, I will try 5
# That bottom one could be either one or two

plt.scatter(points['x'], points['y'])
```


```
from sklearn.cluster import KMeans
```


```
#check using elbow method:

sum_of_squared_distances = []
K = range(1,8)
for k in K:
    km = KMeans(n_clusters=k)
    km = km.fit(points)
    sum_of_squared_distances.append(km.inertia_)
```


```
plt.plot(K, sum_of_squared_distances, 'bx-')
plt.xlabel('k')
plt.ylabel('Sum squared distances')
plt.title('Find good K')
plt.show()
```


```
# Use scikit to make cluster lables for point pairs
kmeans = KMeans(n_clusters=4)
kmeans.fit(points)
labels = kmeans.labels_
print(labels)
```

    [3 1 1 3 1 1 3 3 2 1 1 1 1 1 3 1 3 1 1 0 3 0 0 0 1 1 1 2 1 1 1 1 1 0 1 1 1
     1 2 1 1 0 2 2 2 1 1 1 3 1 0 3 3 2 0 1 1 2 2 3 0 1 3 2 0 2 0 2 3 2 1 1 1 1
     0 0 1 1 2 0 2 1 1 1 2 1 2 1 1 0 2 1 0 1 1 1 1 1 1 1 1 1 3 1 1 0 3 3 3 0 1
     0 1 3 1 1 2 2 0 2 0 1 1 3 1 3 1 0 2 3 1 3 1 3 2 2 1 0 3 1 2 1 1 0 1 2 1 2
     3 1 1 1 3 0 1 2 1 0 1 3 1 1 2 3 0 0 1 2 1 1 3 1 2 0 1 1 1 1 1 3 1 1 1 1 1
     1 3 0 2 1 1 3 0 3 1 2 0 0 1 1]



```
# Add cluster lables to our points dataframe
points['cluster_labels'] = pd.Series(labels)
```


```
# Make sure it worked
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>
      <th>cluster_labels</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>-7.846803</td>
      <td>-3.421277</td>
      <td>3</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-3.554323</td>
      <td>-6.884729</td>
      <td>1</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-0.192822</td>
      <td>-9.671030</td>
      <td>1</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-6.401456</td>
      <td>-5.223972</td>
      <td>3</td>
    </tr>
    <tr>
      <th>4</th>
      <td>-0.804026</td>
      <td>-9.704457</td>
      <td>1</td>
    </tr>
  </tbody>
</table>
</div>




```
#get_centr

centroid = points.groupby('cluster_labels').mean()

centroid
```




<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>
    <tr>
      <th>cluster_labels</th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>6.249793</td>
      <td>3.844572</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-2.733593</td>
      <td>-8.204353</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-3.597407</td>
      <td>4.133441</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-8.366530</td>
      <td>-3.612381</td>
    </tr>
  </tbody>
</table>
</div>




```
colors = {0:'red', 1:'green', 2:'yellow', 3:'#84b3ff'}
fig, ax = plt.subplots()
ax.plot(centroid.iloc[0].x, centroid.iloc[0].y, "ok")
ax.plot(centroid.iloc[1].x, centroid.iloc[1].y, "ok")
ax.plot(centroid.iloc[2].x, centroid.iloc[2].y, "ok")
ax.plot(centroid.iloc[3].x, centroid.iloc[3].y, "ok")

groups = points.groupby('cluster_labels')


for key, group in groups:
  group.plot(ax=ax, kind='scatter', x='x', y='y', label=key, color=colors[key])
plt.show()

```


```

```

ca0e0e72eff8f3ce90e97e80ac5a63fe895662db
ipynb
Jupyter Notebook
Linear_Algebra_Sprint_Challenge.ipynb
jazzathoth/AB-Demo
bd007033794c11f04f452d2c700d8d77dc15dafb
MIT
Linear_Algebra_Sprint_Challenge.ipynb
jazzathoth/AB-Demo
bd007033794c11f04f452d2c700d8d77dc15dafb
MIT
Linear_Algebra_Sprint_Challenge.ipynb
jazzathoth/AB-Demo
bd007033794c11f04f452d2c700d8d77dc15dafb
MIT
Qwen/Qwen-72B
1. YES
2. YES