######################################################################
#CHIPINDU LOVEMORE
#lovemechipsy@gmail.com
#0778796212
#Machine learning vs Data Science
#######################################################################

Vector A column vector is a list of numbers stacked on top of each other

a <- c(1, 3, 2)
a
## [1] 1 3 2

Transpose of vectors

t(a)
##      [,1] [,2] [,3]
## [1,]    1    3    2

Multiplying a vector by a number

7*a
## [1]  7 21 14

Sum of vectors

a <- c(1, 3, 2)
b <- c(2, 8, 9)
w = a + b
w
## [1]  3 11 11

(Inner) product of vectors

sum(a * b)
## [1] 44

The length (norm) of a vector

sqrt(sum(a * a))
## [1] 3.741657

The 0–vector and 1–vector

rep(0, 5)
## [1] 0 0 0 0 0
rep(1, 5)
## [1] 1 1 1 1 1

Orthogonal (perpendicular) vectors Two vectors v1 and v2 are orthogonal if their inner product is zero, written v1 ? v2 , v1 · v2 = 0

v1 <- c(1, 1)
v2 <- c(-1, 1)
sum(v1 * v2)
## [1] 0

Matrices

A = [a1 : a2 : · · · : ac]

A <- matrix(c(1, 3, 2, 2, 8, 9), ncol = 3)
A
##      [,1] [,2] [,3]
## [1,]    1    2    8
## [2,]    3    2    9

Note that the numbers 1, 3, 2, 2, 8, 9 are read into the matrix column–by– column. To get the numbers read in row–by–row do

A2 <- matrix(c(1, 3, 2, 2, 8, 9), ncol = 3, byrow = T)
A2
##      [,1] [,2] [,3]
## [1,]    1    3    2
## [2,]    2    8    9

Multiplying a matrix with a number

7 * A
##      [,1] [,2] [,3]
## [1,]    7   14   56
## [2,]   21   14   63

Transpose of matrices A matrix is transposed by interchanging rows and columns and is denoted by “>”.

t(A)
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    2
## [3,]    8    9

Sum of matrices Let A and B be r × c matrices. The sum A + B is the r × c matrix obtained by adding A and B elementwise. Only matrices with the same dimensions can be added.

B <- matrix(c(5, 8, 3, 4, 2, 7), ncol = 3, byrow = T)
A + B
##      [,1] [,2] [,3]
## [1,]    6   10   11
## [2,]    7    4   16

Multiplication of a matrix and a vector Let A be an r × c matrix and let b be a c-dimensional column vector. The product Ab is the r × 1 matrix

A %*% a
##      [,1]
## [1,]   23
## [2,]   27
A * a  ## Note the difference 
##      [,1] [,2] [,3]
## [1,]    1    4   24
## [2,]    9    2   18

Multiplication of matrices

A <- matrix(c(1, 3, 2, 2, 8, 9), ncol = 2)
B <- matrix(c(5, 8, 4, 2), ncol = 2)
A %*% B
##      [,1] [,2]
## [1,]   21    8
## [2,]   79   28
## [3,]   82   26

Vectors as matrices One can regard a column vector of length r as an r × 1 matrix and a row vector of length c as a 1 × c matrix.

Some special matrices – An n × n matrix is a square matrix – A matrix A is symmetric if A = A>. – A matrix with 0 on all entries is the 0–matrix and is often written simply as 0. – A matrix consisting of 1s in all entries is of written J. – A square matrix with 0 on all off–diagonal entries and elements d1, d2, . . . , dn on the diagonal a diagonal matrix and is often written diag{d1, d2, . . . , dn} – A diagonal matrix with 1s on the diagonal is called the identity matrix and is denoted I. The identity matrix satisfies that IA = AI = A.

0-matrix and 1-matrix

matrix(0, nrow = 2, ncol = 3)
##      [,1] [,2] [,3]
## [1,]    0    0    0
## [2,]    0    0    0
matrix(1, nrow = 2, ncol = 3)
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    1    1    1

Diagonal matrix and identity matrix

diag(c(1, 2, 3))
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    2    0
## [3,]    0    0    3
diag(1, 3)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
diag(diag(c(1, 2, 3))) ## Note what happens when diag is applied to a matrix:
## [1] 1 2 3
diag(A)
## [1] 1 8

Inverse of matrices

In general, the inverse of an n × n matrix A is the matrix B (which is also n × n) which when multiplied with A gives the identity matrix I.

Some facts about inverse matrices are:

– Only square matrices can have an inverse, but not all square matrices have an inverse. – When the inverse exists, it is unique. – Finding the inverse of a large matrix A is numerically complicated (but computers do it for us).

A <- matrix(c(1, 3, 2, 4), ncol = 2, byrow = T)
A
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
B <- solve(A) ## calculating the inverse using the solve command
B
##      [,1] [,2]
## [1,]   -2  1.5
## [2,]    1 -0.5
A %*% B        ##proof  (Identity
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1

find the determinant using the det command

Solving systems of linear equations x1 + 3x2 = 7 2x1 + 4x2 = 10

3 possible cases of solutions to the system

Exactly one solution – when the lines intersect in one point No solutions – when the lines are parallel but not identical Infinitely many solutions – when the lines coincide.

A <- matrix(c(1, 2, 3, 4), ncol = 2)
b <- c(7, 10)
x <- solve(A) %*% b
x
##      [,1]
## [1,]    1
## [2,]    2

Inverting an n × n matrix

In the following we will illustrate one frequently applied methopd for matrix inversion. The method is called Gauss–Seidels method and many computer programs, including solve() use variants of the method for finding the inverse of an n × n matrix.

A <- matrix(c(2, 2, 3, 3, 5, 9, 5, 6, 7), ncol = 3)
A
##      [,1] [,2] [,3]
## [1,]    2    3    5
## [2,]    2    5    6
## [3,]    3    9    7

We want to find the matrix B = A−1. To start, we append to A the identity matrix and call the result AB:

AB <- cbind(A, diag(c(1, 1, 1)))
AB
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    2    3    5    1    0    0
## [2,]    2    5    6    0    1    0
## [3,]    3    9    7    0    0    1