Linear Algebra For Data Science (DataCamp)
Linear Algebra For Data Science (DataCamp)
Creating Vectors in R
# Creating three 3's and four 4's, respectively
rep(3, 3)
## [1] 3 3 3
rep(4, 4)
## [1] 4 4 4 4
# Creating a vector with the first three even numbers and the first three odd numbers
seq(2, 6, by = 2)
## [1] 2 4 6
seq(1, 5, by = 2)
## [1] 1 3 5
## [1] 3 3 3
c(4, 4, 4, 4)
## [1] 4 4 4 4
c(2, 4, 6)
## [1] 2 4 6
c(1, 3, 5)
## [1] 1 3 5
## [1] 3 6 9 12 15 18 21
## [1] 2 2 4
## [1] 2 8 18 32 50 72 98
https://rstudio-pubs-static.s3.amazonaws.com/597406_cae47e2d352642c7b64449b4fee456c7.html 1/13
5/20/2021 Linear Algebra for Data Science (DataCamp)
## [1] 2 3 5 5 6 8 8
Creating Matrices in R
# Create a matrix of all 1's and all 2's that are 2 by 3 and 3 by 2, respectively
matrix(1, nrow = 2, ncol = 3)
## [,1] [,2]
## [1,] 2 2
## [2,] 2 2
## [3,] 2 2
## [,1] [,2]
## [1,] 2 3
## [2,] 4 3
Matrix-Vector Operations
[Video]
Matrix-Vector Compatibility
Consider the matrix A created by the R code:
## [,1]
## [1,] 4
## [2,] 1
# Multiply B by b
B%*%b
## [,1]
## [1,] 0.000000
## [2,] 1.666667
Reflections
# Multiply A by b
A%*%b
## [,1]
## [1,] -2
## [2,] 1
https://rstudio-pubs-static.s3.amazonaws.com/597406_cae47e2d352642c7b64449b4fee456c7.html 2/13
5/20/2021 Linear Algebra for Data Science (DataCamp)
# Multiply B by b
B%*%b
## [,1]
## [1,] 2
## [2,] -1
# Multiply C by b
C%*%b
## [,1]
## [1,] -8
## [2,] -2
Matrix-Matrix Calculations
[Video]
Often times these collections of weights are applied iteratively using successive applications of matrix multiplication.
Are A and B compatible in any way in terms of matrix multiplication? Use A%*%B and B%*%A in the console to check. What are the dimensions of
the resulting matrix?
## [,1] [,2]
## [1,] 0.7071068 0.7071068
## [2,] 0.7071068 -0.7071068
## [,1] [,2]
## [1,] 0.7071068 -0.7071068
## [2,] -0.7071068 -0.7071068
## [,1]
## [1,] 1.414214
## [2,] 0.000000
## [,1]
## [1,] 0.000000
## [2,] -1.414214
## [,1] [,2]
## [1,] 1 0
## [2,] 0 1
https://rstudio-pubs-static.s3.amazonaws.com/597406_cae47e2d352642c7b64449b4fee456c7.html 3/13
5/20/2021 Linear Algebra for Data Science (DataCamp)
# Multiply A inverse by A
Ainv%*%A
## [,1] [,2]
## [1,] 1 0
## [2,] 0 1
## [,1] [,2]
## [1,] 1 0
## [2,] 0 1
The Meaning of Ax = b
A great deal of applied mathematics and statistics, as well as data science, ends in a matrix-vector equation of the form:
Ax = b
Which of the following is the most correct way to describe what solving this equation for x is trying to accomplish?
https://rstudio-pubs-static.s3.amazonaws.com/597406_cae47e2d352642c7b64449b4fee456c7.html 4/13
5/20/2021 Linear Algebra for Data Science (DataCamp)
## Differential
## 1 -135
## 2 -171
## 3 152
## 4 -104
## 5 -308
## 6 292
## 7 420
## 8 83
## 9 -4
## 10 -213
## 11 -5
## 12 -7
## 13 0
## [1] 0
## [1] 0
` > print(M)
1 33 -4 -2 -3 -3 -3 -3 -3 -3 -3 -3 -3 2 -4 33 -3 -3 -3 -3 -2 -3 -3 -3 -3 -3 3 -2 -3 34 -3 -3 -3 -3 -4 -4 -3 -3 -3 4 -3 -3 -3 34 -3 -4 -3 -3 -2 -3 -3 -4 5 -3 -3 -3
-3 33 -3 -3 -3 -3 -3 -2 -4 6 -3 -3 -3 -4 -3 41 -8 -3 -6 -3 -2 -3 7 -3 -2 -3 -3 -3 -8 41 -3 -4 -3 -3 -6 8 -3 -3 -4 -3 -3 -3 -3 34 -3 -2 -3 -4 9 -3 -3 -4 -2 -3 -6 -4
-3 38 -3 -4 -3 10 -3 -3 -3 -3 -3 -3 -3 -2 -3 32 -4 -2 11 -3 -3 -3 -3 -2 -2 -3 -3 -4 -4 33 -3 12 -3 -3 -3 -4 -4 -3 -6 -4 -3 -2 -3 38
solve(M) Error in solve.default(M) : system is computationally singular: reciprocal condition number = 3.06615e-
17 `
Which of the following three graphs is that of a linear system of two equations with two unknowns that has no solutions?
usually does not (computationally) have an inverse, as shown by the error produced from running solve(M) in a previous exercise.
One way we can change this is to add a row of 1 ’s on the bottom of the matrix M, a column of -1 ’s to the far right of M, and a 0 to the bottom of
the vector of point differentials f .
What does that row of 1 ’s represent in the setting of rating teams? In other words, what does the final equation stipulate?
https://rstudio-pubs-static.s3.amazonaws.com/597406_cae47e2d352642c7b64449b4fee456c7.html 5/13
5/20/2021 Linear Algebra for Data Science (DataCamp)
# Print M_3
print(M_3)
https://rstudio-pubs-static.s3.amazonaws.com/597406_cae47e2d352642c7b64449b4fee456c7.html 6/13
5/20/2021 Linear Algebra for Data Science (DataCamp)
If A−1 doesn’t exist, this does not work. The equivalent analogy for linear equations would be a situation in which the coefficient in front of the x
were 0, which is the only real number that does not have an inverse. Which of the following does NOT analogize in this situation?
Dividing by zero is illegal, and is analogous to trying to invert a matrix with a zero determinant.
[*] All of the elements of a matrix must be zero for it to fail to have an inverse.
The equation 0x=b has zero solutions (if b≠0).
The equation 0x=b has infinitely many solutions (if b=0).
# Print r
print(r)
## Rating
## Atlanta -4.012938e+00
## Chicago -5.156260e+00
## Connecticut 4.309525e+00
## Dallas -2.608129e+00
## Indiana -8.532958e+00
## Los.Angeles 7.850327e+00
## Minnesota 1.061241e+01
## New.York 2.541565e+00
## Phoenix 8.979110e-01
## San.Antonio -6.181574e+00
## Seattle -2.666953e-01
## Washington 5.468121e-01
## WNBA 1.043610e-14
https://rstudio-pubs-static.s3.amazonaws.com/597406_cae47e2d352642c7b64449b4fee456c7.html 7/13
5/20/2021 Linear Algebra for Data Science (DataCamp)
In the previous exercise, you rated the teams at the end of the 2017 WNBA season using the solution to a matrix-vector equation.
arrange(r, -Rating)
we can see which team was the best in the WNBA in 2017 (using the negative (“-”) sign in front of the ordering variable (“Rating”) puts the values
in descending order, as opposes to ascending order if just “Rating” is used).
# arrange(r, -Rating)
San Antonio
[*] Minnesota
Los Angeles
Phoenix
https://rstudio-pubs-static.s3.amazonaws.com/597406_cae47e2d352642c7b64449b4fee456c7.html 8/13
5/20/2021 Linear Algebra for Data Science (DataCamp)
## Rating
## Atlanta -4.012938e+00
## Chicago -5.156260e+00
## Connecticut 4.309525e+00
## Dallas -2.608129e+00
## Indiana -8.532958e+00
## Los.Angeles 7.850327e+00
## Minnesota 1.061241e+01
## New.York 2.541565e+00
## Phoenix 8.979110e-01
## San.Antonio -6.181574e+00
## Seattle -2.666953e-01
## Washington 5.468121e-01
## WNBA 1.043610e-14
## Rating
## [1,] -4.012938e+00
## [2,] -5.156260e+00
## [3,] 4.309525e+00
## [4,] -2.608129e+00
## [5,] -8.532958e+00
## [6,] 7.850327e+00
## [7,] 1.061241e+01
## [8,] 2.541565e+00
## [9,] 8.979110e-01
## [10,] -6.181574e+00
## [11,] -2.666953e-01
## [12,] 5.468121e-01
## [13,] 5.773160e-14
Matrix-Vector Multiplications
Rotations
Reflections
Dilations
Contradictions
Projections
Every imaginable combinations of these
Scalar Multiplication
https://rstudio-pubs-static.s3.amazonaws.com/597406_cae47e2d352642c7b64449b4fee456c7.html 9/13
5/20/2021 Linear Algebra for Data Science (DataCamp)
Eigenvalue Ordering
Markov Models for Allele Frequencies
Finding Redundancies
# Print the first 6 observations of the dataset
head(combine)
## [1] 0.8315171
## [1] 0.8163375
Given the results of the previous parts of the exercise, what can you say about the dataset combine at this point?
Covariance Explored
If the covariance between two columns of a matrix is positive and large, what can we say?
Variance/Covariance Calculations
# Create matrix B from equation in instructions
B <- t(A)%*%A/(nrow(A) - 1)
# Compare 1st element of the 1st column of B to the variance of the first column of A
B[1,1]
## [1] 7.159794
var(A[, 1])
## [1] 7.159794
# Compare 1st element of 2nd column of B to the 1st element of the 2nd row of B to the covariance between the first two colu
mns of A
B[1, 2]
## [1] 90.78808
B[2, 1]
## [1] 90.78808
## [1] 90.78808
# Print eigenvalues
V$values
Roughly how much of the variability in the dataset can be explained by the first principal component?
About 15 percent.
About 50 percent.
About 75 percent.
[*] About 95 percent.
Performing PCA in R
[Video]
https://rstudio-pubs-static.s3.amazonaws.com/597406_cae47e2d352642c7b64449b4fee456c7.html 11/13
5/20/2021 Linear Algebra for Data Science (DataCamp)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6 PC7
## Standard deviation 2.3679 0.9228 0.78904 0.61348 0.46811 0.37178 0.34834
## Proportion of Variance 0.7009 0.1064 0.07782 0.04704 0.02739 0.01728 0.01517
## Cumulative Proportion 0.7009 0.8073 0.88514 0.93218 0.95957 0.97685 0.99202
## PC8
## Standard deviation 0.25266
## Proportion of Variance 0.00798
## Cumulative Proportion 1.00000
Summarizing PCA in R
# Subset combine only to "WR"
combine_WR <- subset(combine, position == "WR")
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6 PC7
## Standard deviation 1.5425 1.4255 1.0509 0.9603 0.77542 0.63867 0.59792
## Proportion of Variance 0.2974 0.2540 0.1380 0.1153 0.07516 0.05099 0.04469
## Cumulative Proportion 0.2974 0.5514 0.6894 0.8047 0.87987 0.93085 0.97554
## PC8
## Standard deviation 0.44235
## Proportion of Variance 0.02446
## Cumulative Proportion 1.00000
https://rstudio-pubs-static.s3.amazonaws.com/597406_cae47e2d352642c7b64449b4fee456c7.html 12/13
5/20/2021 Linear Algebra for Data Science (DataCamp)
With less data, the first PC of the subset data explains more of the variability in the dataset.
The first PC explains similar amounts of variability for both datasets.
[*] It takes the first 3 PCs of the subset data to explain the same amount of variability as the first PC of the whole dataset.
Wrap-Up
[Video]
Michael earned his BS in Computer Science from New York Institute of Technology and his MBA from the University of Maryland, College Park. He
is also a candidate to receive his MS in Applied Analytics from Columbia University.
https://rstudio-pubs-static.s3.amazonaws.com/597406_cae47e2d352642c7b64449b4fee456c7.html 13/13