<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># Fuction to find the mean of a list of values:
def findMean(num_list):
    '''
    Return the mean out of any list of numbers.
        
        - Params:
        num_list: a list of numeric values
    '''    
    return round(sum(num_list)/len(num_list),2)

# Function to find th variance from a list of numbers:
def findVariance(num_list):
    '''
    Return the variance from a list of numbers.

        - Params:
        num_list: a list of numeric values
    '''
    l = []
    a = sum(num_list)/len(num_list)
    for i in num_list:
        b = i-a
        c = b**2
        l.append(c)
    d = len(l)
    e = sum(l)/d
    return round(e,2)

# Function to find the standard deviation from a list of numbers:
def findStd(num_list):
    '''
    Return the standard deviation from a list of numbers.

        - Params:
        num_list: a list of numeric values
    '''    
    l = []
    a = sum(num_list)/len(num_list)
    for i in num_list:
        b = i-a
        c = b**2
        l.append(c)
    d = len(l)
    e = sum(l)/d
    return round(e**(.5),2)
# Sale numbers of the past week:
sales_amounts = [3505,2400,3027,2798,3700,3250,2689]

print('Mean: ',findMean(sales_amounts))
print('Variance: ',findVariance(sales_amounts))
print('Standard Deviation: ',findStd(sales_amounts))<jupyter_output>Mean:  3052.71
Variance:  183761.06
Standard Deviation:  428.67<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 numpy as np
# Sale numbers of the past week:
sales_amounts = [3505,2400,3027,2798,3700,3250,2689]
# Number of customers from the past week:
num_of_cust = [127,80,105,92,120,115,93]

# Covariance between sales and customers:
cov = round(np.cov(sales_amounts,num_of_cust)[0][1],2)
print('Covariance: ',cov)<jupyter_output>Covariance:  7604.36<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># Standard deviation of the number of customers that entered the store in the past week:
cust_std = findStd(num_of_cust)
# Standard deviation of the sales from the past week:
sales_std = findStd(sales_amounts)
# Find the covariance:
cov = np.cov(np.array([sales_amounts],[num_of_cust])
# Denominator for covariance standardization:
denominator = sales_std*cust_std
# Standardize the covariance to find the correlation coefficient:
corr = cov/denominator
print('By hand Correlation Coeficient: \n',corr)

print('Correlation Coef. useing Numpy: ',np.corrcoef(sales_amounts,num_of_cust)[0][1])

# Find the Correlation Coefficient using pandas:
import pandas as pd
# Create a dataframe:
df = pd.DataFrame({'sales':sales_amounts,'customers':num_of_cust})
# Find the correlation coefficient:
df['sales'].corr(df['customers'])
print('Correlation Coef. using Pandas: ',np.array(df.corr())[0][1])

np.linalg.det(corr)<jupyter_output>By hand Correlation Coeficient: 
 [[31.67342724  1.12345915]
 [ 1.12345915  0.04298498]]
Correlation Coef. useing Numpy:  0.9628339778148909
Correlation Coef. using Pandas:  0.9628339778148908<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>import pandas as pd
# Read in the cleaned up titanic dataset:
titanic = pd.read_csv('https://raw.githubusercontent.com/Geoyi/Cleaning-Titanic-Data/master/titanic_clean.csv', index_col='Unnamed: 0')
# Get the variance-covariance matrix:
cov = titanic.cov()
# Get the correlation matrix:
corr = titanic.corr()

print("Variance-CoVariance Matrix:")
display(cov)
print('\n\n',"Correlation Matrix:")
display(corr)
print('\n\n')

import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots(figsize=(7,6))
sns.heatmap(corr)
ax.set_title('Titanic Data Correlation Matrix',fontsize=20)
plt.show();<jupyter_output>Variance-CoVariance Matrix:<jupyter_text>Orthogonality  2.1 Plot two vectors that are orthogonal to each other. What is a synonym for orthogonal? **Answer:**> A synonym for orthogonal is perpendicular.<jupyter_code># Creat orthogonal vectors:
vec_a = [.2,.5]
vec_b = [.5,-.2]

# Plot the orthogonal vectors:
fig, ax = plt.subplots()
ax.grid()
plt.xlim(-.4,1.0)
plt.ylim(-.4,1.0)
plt.arrow(0,0,vec_a[0],vec_a[1],
          width=.03,head_width=.1,head_length=.1,length_includes_head=True)
plt.arrow(0,0,vec_b[0],vec_b[1],
          width=.03,head_width=.1,head_length=.1,length_includes_head=True,
          color='green',ec='k');

np.dot(vec_a,vec_b)<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} **Answer:**> These vectors are not orthogonal, because the are not perpendicular and the dot product is not equal to zero.<jupyter_code># Create vectors:
vec_c,vec_d = [-.5,.3,.7],[.6,-.8,.2]

# Check orthoganality by finding the dot product of the 2 vectors:
print(np.dot(vec_c,vec_d))

# Check orthoganality by plotting these vectors:
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(-.10,1)
ax.set_ylim(-.10,1)
ax.set_zlim(-.10,1)
ax.quiver(0,0,0,vec_c[0],vec_c[1],vec_c[2])
ax.quiver(0,0,0,vec_d[0],vec_d[1],vec_d[2])<jupyter_output>-0.4<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># Create vector:
c = [2,-15,6,20]

# Find the length of the vector:
norm_c = np.linalg.norm(c)**2
# Find the dot product of the vector:
dot_c = np.dot(c,c)
# Calculate the vector.Transpose by itself:
transpose_c = np.array(c).T*np.array(c)

print(norm_c)
print(dot_c)
print(sum(transpose_c))

# All of these calculations compute the same thing.<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} **Answer:**>$d = 7\hat{i}+12\hat{j}$ >$e = 2\hat{i}+11\hat{j}+-8\hat{k}$  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># Create Vector:
f = [4,12,11,9,2]
# Convert f into a numpy array:
f = np.array(f)
# Turn vector into a unit vector
f = f/np.linalg.norm(f)
f<jupyter_output><empty_output><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>import numpy as np
import pandas as pd
# Create matrix of linearly dependent vectors:
dep_vec_matrix = pd.DataFrame({'vec_a':[.2,.2,.2],'vec_b':[.3,.3,.3]})
print('Rank ',np.linalg.matrix_rank(dep_vec_matrix))
# Create matrix of linearly independent vectors:
ind_vec_matrix = pd.DataFrame({'vec_a':[.3,.6,.2],'vec_b':[.1,.4,.5]})
print('Rank ',np.linalg.matrix_rank(ind_vec_matrix))
# Plot vectors:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(0,1)
ax.set_ylim(0,1)
ax.set_zlim(0,1)
ax.quiver(0,0,0,dep_vec_matrix['vec_a'][0],dep_vec_matrix['vec_a'][1],dep_vec_matrix['vec_a'][2],color='red')
ax.quiver(0,0,0,dep_vec_matrix['vec_b'][0],dep_vec_matrix['vec_b'][1],dep_vec_matrix['vec_b'][2],color='black')
ax.quiver(0,0,0,ind_vec_matrix['vec_a'][0],ind_vec_matrix['vec_a'][1],ind_vec_matrix['vec_a'][2],color='green')
ax.quiver(0,0,0,ind_vec_matrix['vec_b'][0],ind_vec_matrix['vec_b'][1],ind_vec_matrix['vec_b'][2],color='green');<jupyter_output>Rank  1
Rank  2<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># Create vectors:
g = np.array([.1,.2])
h = np.array([.4,.8])

# Find the span:
print('Span: Rank ',np.linalg.matrix_rank([g,h]),'\n\n')

# Plot the span of the vectors to show its 1-d:
fig, ax = plt.subplots()
ax.grid()
plt.xlim(-.4,1)
plt.ylim(-.4,1)
plt.arrow(0,0,h[0],h[1],color='red',head_width=.06,width=.04)
plt.arrow(0,0,g[0],g[1],head_width=.05,width=.02);<jupyter_output>Span: Rank  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># Create Vectors:
l = np.array([1,2,3])
m = np.array([-1,0,7])
n = np.array([4,8,2])
# Find span of vectors:
print('Span: Rank',np.linalg.matrix_rank([l,m,n]))<jupyter_output>Span: Rank 3<jupyter_text>Basis  6.1 Graph two vectors that form a basis for $\mathbb{R}^2$<jupyter_code># Creat vectors:
vec_one,vec_two = [-.2,.3],[.2,.5]

# Plot vectors that form a basis for rank2:
fig, ax = plt.subplots()
ax.grid()
plt.xlim(-.3,.6)
plt.ylim(-.3,.6)
plt.arrow(0,0,vec_one[0],vec_one[1],
          head_width=.03, width=.01, color='green')
plt.arrow(0,0,vec_two[0],vec_two[1],
          head_width=.03, width=.01, color='red')<jupyter_output><empty_output><jupyter_text>6.2 What does it mean to form a basis?   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># Create matrix:
p = np.array([[1,-1,4],
             [2,0,8],
             [3,7,2]])
# Find Rank:
np.linalg.matrix_rank(p.T)<jupyter_output><empty_output><jupyter_text>7.2 What does the rank of a matrix tell us? **Answer:**>It shows us the maximum number of linearly independent column vectors in the matrix or the maximum number of linearly independent row vectors in the matrix.  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])

v_w = np.dot(v,w)
w_w = np.dot(w,w)

frac = v_w/w_w

projection = np.multiply(frac,w)

fig, ax = plt.subplots()
ax.grid()
plt.xlim(-2,10)
plt.ylim(-2,10)
ax.arrow(0,0,projection[0],projection[1])<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.1432247687977483, 'fraction_numerical': 0.04551668677121029, 'mean_word_length': 4.866037735849057, 'pattern_counts': {'":': 2, '<': 52, '<?xml version=': 0, '>': 57, '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': '464758', 'license': 'MIT', 'n_tokens_mistral': 4952, 'n_tokens_neox': 4537, 'n_words': 1250, 'path': 'DataScience/DS-Unit-1-Sprint-3-Linear-Algebra/module2-intermediate-linear-algebra/LS_DS_132_Intermediate_Linear_Algebra_Assignment.ipynb', 'repo_name': 'dustin-py/Education', 'size': '12435'}