<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 mean1(numbs):
  return sum(numbs)/len(numbs)

def variance(numbs):
  mean = mean1(numbs)
  return sum([(mean-x)**2 for x in numbs])/len(numbs)

def std(numbs):
  return variance(numbs)**.5

SalesMean = mean(sales)
SalesVariance = variance(sales)
SalesSTDEV = std(sales)

print("Mean of Sales: ", SalesMean)
print("Variance of Sales: ", SalesVariance)
print("Standard Deviation of Sales: ", SalesSTDEV)<jupyter_output>Mean of Sales:  3052.714285714286
Variance of Sales:  183761.06122448976
Standard Deviation of Sales:  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>import math

sales = [3505, 2400, 3027, 2798, 3700, 3250, 2689]
customers = [127, 80, 105, 92, 120, 115, 93]

df = pd.DataFrame({'LWsales': sales, 'LWcustomers':customers})

print(np.cov(df['LWsales'], df['LWcustomers']))
covariance = df.cov()['LWsales']['LWcustomers']

print(covariance)<jupyter_output>[[214387.9047619    7604.35714286]
 [  7604.35714286    290.95238095]]
7604.357142857142<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>last_weekstd = std(customers)
print(last_weekstd)

co_co = covariance / (df['LWsales'].std() * df['LWcustomers'].std())
print(co_co)<jupyter_output>15.792015549069118
0.9628339778148909<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>df = pd.read_csv('https://raw.githubusercontent.com/Geoyi/Cleaning-Titanic-Data/master/titanic_clean.csv')
df.head(3)
df.cov()
df.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
# A synonym for orthogonal is perpendicular

v1 = np.array([10,0])
v2 = np.array([0,10])

plt.arrow(0,0, *v1, head_width=1, head_length=1, color='purple')
plt.arrow(0,0, *v2, head_width=1, head_length=1, color='blue')
plt.xlim(-1, 15)
plt.ylim(-1, 15)
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>#We can calculate the dot product to find out:

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

np.dot(a, b)

# Not orthogonal bc the dot product is -40, not zero+<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])

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

two = (np.dot(c, c))

three = (np.matmul(c.T, c))
print (one, two, three)

#All the same number<jupyter_output>665.0 665 665<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} Your text here  3.2 Turn vector $f$ into a unit vector:\begin{align}f = \begin{bmatrix} 4 & 12 & 11 & 9 & 2 \end{bmatrix}\end{align}<jupyter_code>f = [4, 12, 11, 9, 2]

unitvec = f/np.linalg.norm(f)
print(unitvec)<jupyter_output>[0.20908335 0.62725005 0.57497921 0.47043754 0.10454167]<jupyter_text>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><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>g = np.array([1, 2])
h = np.array([4, 8])

span = np.linalg.matrix_rank(g, h)
print(span)<jupyter_output>1<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>l = np.array([1,2,3])
m = np.array([-1,0,7])
n = np.array([4, 8, 2])

span2 = np.linalg.matrix_rank(l, m, n)
print(span2)
#that didnt work.....

np.linalg.matrix_rank(np.array([[1, 2, 3], [-1, 0, 7], [4, 8, 2]]))<jupyter_output>1<jupyter_text>Basis  6.1 Graph two vectors that form a basis for $\mathbb{R}^2$<jupyter_code># Could use my orthogonal vectors from 2.1 but I'll make a new one

v1 = np.array([20,0])
v2 = np.array([0,20])

plt.arrow(0,0, *v1, head_width=1, head_length=1, color='purple')
plt.arrow(0,0, *v2, head_width=1, head_length=1, color='pink')
plt.xlim(-5, 25)
plt.ylim(-5, 25)
plt.show()<jupyter_output><empty_output><jupyter_text>6.2 What does it mean to form a basis? Vectors in a space that are linearly independent and span the whole 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}<jupyter_code># Exactly the same as 5.2 but this comined it to one big matrix. Can solve it the same way
RoP = np.linalg.matrix_rank(np.array([[1, 2, 3], [-1, 0, 7], [4, 8, 2]]))
RoP<jupyter_output><empty_output><jupyter_text>7.2 What does the rank of a matrix tell us? It can tell us how many independent columns and rows are in a given matrix and the different ways the matrix may be legally transformed. And other things too.  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>v = np.array([1, 3])
w = np.array([-1, 2])

sca_v = np.dot(w, v)/(np.dot(v, v))
proj= sca_v * v
proj
#Checking

plt.arrow(0, 0, *v, head_width=.3, head_length=.3, color='purple')
plt.arrow(0, 0, *w, head_width=.3, head_length=.3, color='pink')
plt.arrow(0, 0, *proj, head_width=.3, head_length=.3, color='blue')
plt.xlim(-2, 5)
plt.ylim(-1, 5)
plt.show()<jupyter_output><empty_output><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.13629842180774748, 'fraction_numerical': 0.06719273075083693, 'mean_word_length': 5.022318214542836, 'pattern_counts': {'":': 2, '<': 54, '<?xml version=': 0, '>': 54, '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': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '511293', 'license': 'MIT', 'n_tokens_mistral': 3475, 'n_tokens_neox': 3002, 'n_words': 964, 'path': 'Valerie_Intermediate_Linear_Algebra_Assignment.ipynb', 'repo_name': 'ValerieLangat/DS-Unit-1-Sprint-4-Statistical-Tests-and-Experiments', 'size': '8364'}