<jupyter_start><jupyter_text>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)<jupyter_code>import pandas as pd
import numpy as np
sales = [3505, 2400, 3027, 2798, 3700, 3250, 2689]


def calc_mvs(somelist):
  
  total = sum(somelist)
  mean = total/len(somelist)
  distances = []
  
  for item in somelist:
    distance = item - mean
    distances.append(distance**2)
    
  variance = sum(distances)/ len(distances)
  
  std_dev = variance**.5
  
  
    
  
    
  return mean, variance, std_dev
  
  
print(np.std(sales))  
calc_mvs(sales)<jupyter_output>428.67360686714756<jupyter_text>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)<jupyter_code>customer_tally = [127, 80, 105, 92, 120, 115, 93]

df_0 = pd.DataFrame({1: sales, 2: customer_tally})
df.columns = ['sales', 'customer_tally']

df.cov()<jupyter_output><empty_output><jupyter_text>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.)<jupyter_code>print(calc_mvs(sales)[2])
calc_mvs(customer_tally)[2]


def calc_corr_coef(list1, list2):
  x = np.mean(list1)
  y = np.mean(list2)
  
  sx = calc_mvs(list1)[2]
  sy = calc_mvs(list2)[2]
  
  df = pd.DataFrame({1:list1, 2: list2})
  df.columns = ['list_1', 'list_2']
  
  df['x_less_mean'] = df['list_1'] - x
  df['y_less_mean'] = df['list_2'] - y
  df['numerator_val'] = df['x_less_mean']*df['y_less_mean']
  
  numer_totals = df.numerator_val.sum()
  
  by_std_dev = numer_totals/(sx*sy)
  
  r = by_std_dev/ len(list1)
  
  print(f"r = {r.round(5)}")


calc_corr_coef(sales, customer_tally)
  
   
df_0.corr()<jupyter_output><empty_output><jupyter_text>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)<jupyter_code>titanic_clean = pd.read_csv('https://raw.githubusercontent.com/Geoyi/Cleaning-Titanic-Data/master/titanic_clean.csv')
titanic_clean.cov()
titanic_clean.corr()<jupyter_output><empty_output><jupyter_text>Orthogonality  2.1 Plot two vectors that are orthogonal to each other. What is a synonym for orthogonal?<jupyter_code>import matplotlib.pyplot as plt

vector_1 = [0, 5]
vector_2 = [5, 0]

plt.arrow(0,0, vector_1[0], vector_1[1],head_width=.05, head_length=0.05, color ='red')
plt.arrow(0,0, vector_2[0], vector_2[1],head_width=.05, head_length=0.05, color ='green')
plt.xlim(-10,10)          
plt.ylim(-10,10)
plt.title("Orthogonal Vectors")
plt.show()<jupyter_output><empty_output><jupyter_text>2.2 Are the following vectors orthogonal? Why or why not?\begin{align}a = \begin{bmatrix} -5 \\ 3 \\ 7 \end{bmatrix}\qquadb = \begin{bmatrix} 6 \\ -8 \\ 2 \end{bmatrix}\end{align}<jupyter_code>a = np.array([-5, 3, 7])
b = np.array([6, -8, 2])

a.dot(b)

# They are not orthogonal because the dot product of the two vectors does not equal 0<jupyter_output><empty_output><jupyter_text>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}<jupyter_code>c = np.array([2, -15, 6, 20])

norm_sq = np.linalg.norm(c)**2

cc = c.dot(c)

ctc = c.T.dot(c)


norm_sq
cc
ctc<jupyter_output><empty_output><jupyter_text>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}\qquade = \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\hat{i} + 12\hat{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\hat{i} + 11\hat{j} - 8\hat{k}\end{align}  3.2 Turn vector $f$ into a unit vector:\begin{align}f = \begin{bmatrix} 4 & 12 & 11 & 9 & 2 \end{bmatrix}\end{align} \begin{align} ||f|| = \sqrt{4^2 + 12^2 + 11^2 + 9^2 +4^2 } = \sqrt{16 + 144 + 121 +81 +4} = \sqrt{366}\end{align}  \begin{align} \hat{f} = \frac{1}{||f||}f = \frac{1}{\sqrt 366}\begin{bmatrix} 4 \\ 12 \\ 11 \\ 9 \\ 2 \end{bmatrix} = \begin{bmatrix} \frac{4}{\sqrt 366} \\ \frac{12}{\sqrt 366} \\  \frac{11}{\sqrt 366} \\ \frac{9}{\sqrt 366} \\ \frac {2}{\sqrt 366} \end{bmatrix} = \begin{bmatrix} \frac{2 \sqrt 2}{\sqrt 183} \\ \frac{2 \sqrt 6}{\sqrt 61} \\ \frac {11}{\sqrt 366} \\ \frac{3 \sqrt 3}{\sqrt 122} \\ \sqrt \frac{2}{183} \end{bmatrix}\end{align}  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$).<jupyter_code># Axis Bounds
plt.xlim(0,10)          
plt.ylim(0,10)

# Original Vector
v = [3,6] 

# Scaled Vector
v2 = np.multiply(3, v)



plt.arrow(0,0, v2[0], v2[1], linewidth=6, head_width=.05, head_length=0.05, color ='r')
plt.arrow(0,0, v[0], v[1], linewidth=3, head_width=.05, head_length=0.05, color ='b')


plt.title("Linearly Dependent Vectors")
plt.show()
plt.xlim(-20,20)          
plt.ylim(-20,20)


a = [-7,-10] 
b = [-5, 13]


plt.arrow(0,0, a[0], a[1], linewidth=3, head_width=.05, head_length=0.05, color ='b')
plt.arrow(0,0, b[0], b[1], linewidth=3, head_width=.05, head_length=0.05, color ='r')

plt.title("Linearly Independent Vectors")
plt.show()<jupyter_output><empty_output><jupyter_text>Span  5.1 What is the span of the following vectors?\begin{align}g = \begin{bmatrix} 1 & 2 \end{bmatrix}\qquadh = \begin{bmatrix} 4 & 8 \end{bmatrix}\end{align}<jupyter_code># Vectors are linearly dependent so their span is 1<jupyter_output><empty_output><jupyter_text>5.2 What is the span of $\{l, m, n\}$?\begin{align}l = \begin{bmatrix} 1 & 2 & 3 \end{bmatrix}\qquadm = \begin{bmatrix} -1 & 0 & 7 \end{bmatrix}\qquadn = \begin{bmatrix} 4 & 8  & 2\end{bmatrix}\end{align}<jupyter_code># Independent vectors span 3<jupyter_output><empty_output><jupyter_text>Basis  6.1 Graph two vectors that form a basis for $\mathbb{R}^2$<jupyter_code>a = [2, 8]
b = [1, 3]
plt.grid()
plt.arrow(0,0, a[0], a[1], linewidth=3, head_width=0.5, head_length=0.5)
plt.arrow(0,0,b[0],  b[1], color = 'r', linewidth=3, head_width=0.5, head_length=0.5)

plt.xlim(-10, 10)
plt.ylim(-10, 10)
plt.show()<jupyter_output><empty_output><jupyter_text>6.2 What does it mean to form a basis? A basis is n linearly independent vectors n being the dimensions of the 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}<jupyter_code>p = np.array([[1,2,3], [-1,0,7], [4,8,2]])
np.linalg.matrix_rank(p)<jupyter_output><empty_output><jupyter_text>7.2 What does the rank of a matrix tell us? The rank of a matrix is the number of dimensions it can span in 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)<jupyter_code># Bounds
plt.xlim(-2, 2)
plt.ylim(-1, 4)

axes = plt.gca()
plt.axes().set_aspect('equal')

v = np.array([1, 3])
w = np.array([-1, 2])
proj = np.multiply((np.vdot(w, v)/np.vdot(v,v)), v)

plt.grid()

plt.arrow(0,0, 
          v[0], v[1], 
          linewidth=8, 
          head_width=0.05,
          head_length=0.05, 
          color = 'g')
plt.arrow(0,0,
          w[0], w[1], 
          color = 'r', 
          linewidth=8, 
          head_width=0.05, 
          head_length=0.05)
plt.arrow(0,0, 
          proj[0], 
          proj[1],
          linewidth=8, 
          head_width=0.05,
          head_length=0.05)<jupyter_output>/usr/local/lib/python3.6/dist-packages/matplotlib/figure.py:98: MatplotlibDeprecationWarning: 
Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
  "Adding an axes using the same arguments as a previous axes "<jupyter_text>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.<jupyter_code>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()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14837758112094396, 'fraction_numerical': 0.054965585054080626, 'mean_word_length': 4.095691382765531, 'pattern_counts': {'":': 2, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '638373', 'license': 'MIT', 'n_tokens_mistral': 4205, 'n_tokens_neox': 3714, 'n_words': 1083, 'path': 'curriculum/unit-1-statistics-fundamentals/sprint-3-linear-algebra/module2-intermediate-linear-algebra/LS_DS_132_Intermediate_Linear_Algebra_Assignment.ipynb', 'repo_name': 'BrianThomasRoss/lambda-school', 'size': '10170'}