Lecture Notes Lecture 2 Basic Linear Algebra Matlab
Lecture Notes Lecture 2 Basic Linear Algebra Matlab
CS4442/9542b
Artificial Intelligence II
Prof. Olga Veksler
Lecture 2
Introduction to ML
Basic Linear Algebra
Matlab
Some slides on Linear Algebra are from Patrick Nichols
Downloaded by Marisnelvys Cabreja (930mcc@gmail.com)
lOMoARcPSD|17897360
Outline
fis
h sp
fis ec
h im ie s salmon
ag
e
sorting
chamber
sea bass
Classifier design
• Notice salmon tends to be shorter than sea bass
• Use fish length as the discriminating feature
• Count number of bass and salmon of each length
2 4 8 10 12 14
bass 0 1 3 8 10 5
salmon 2 5 10 5 1 0
12
10
8
Count
salmon
6
sea bass
4
2
0
2 4 8 10 12 14
Length
• 1 sea bass 2 4 8 10 12 14
bass 0 1 3 8 10 5
• 16 salmon
salmon 2 5 10 5 1 0
salmon
6
sea bass
4
2
0
2 4 8 10 12 14
Length
Next Step
• Lesson learned:
• Length is a poor feature alone!
• What to do?
• Try another feature
• Salmon tends to be lighter
• Try average fish lightness
14
12
10
Count
8 salmon
6 sea bass
4
2
0
1 2 3 4 5
Lightness
ba decision
ss
lightness
boundary
decision regions
sa
lm
on
length
• Classification error 4%
lightness
length
lightness
length
Generalization
training data test data
Underfitting
Underfitting Overfitting
Training Phase
• Find the weights w s.t. f(xi,w) = yi “as much as
possible” for training samples (xi, yi)
• “as much as possible” needs to be defined
• How do we find parameters w to ensure
f(xi,w) = yi for most training samples (xi,yi) ?
• This step is usually done by optimization, can be
quite time consuming
Testing Phase
• The goal is to design machine which performs
well on unseen examples
• Evaluate the performance of the trained
machine f(x,w) on the test samples (unseen
labeled samples)
• Testing the machine on unseen labeled examples
lets us approximate how well it will perform in
practice
• If testing results are poor, may have to go back
to the training phase and redesign f(x,w)
[l1, w1 , c1 ] example 1 l1 w1 c1
[l2 , w2 , c2 ] example 2 l2 w2 c2
[l3 , w3 , c3 ] example 3
l3 w3 c3
matrix
What is a Matrix?
• A matrix is a set of elements, organized into
rows and columns
rows
2 7 6 10 example 1
columns 1 4 4 9 example 2
6 4 9 feature 2 6 example 3
feature 3
feature 4
feature 1
a b e f a+e b+ f
+ = add elements
c d g h c+g d +h
a b e f a−e b− f
− = subtract elements
c d g h c−g d −h
α ⋅ a α ⋅b
a b
α⋅ = multiply every entry
c d α ⋅c α ⋅d
Matrix Transpose
T
• n by m matrix A and its m by n transpose A
Vectors
• Vector: N x 1 matrix
x1
v=
x2
• dot product and magnitude defined on vectors only
x2 x2 x2
v a a+b a a-b
b b
x1 x1 x1
vector addition vector subtraction
More on Vectors
• n-dimensional row vector x = [x1 x2 xn ]
x1
T
x2
• Transpose of row vector is column vector x =
xn
• Vector product (or inner or dot product)
x, y = x ⋅ y = xT y = x1 y1 + x2 y2 + + xn yn = xi yi
i=1 n
More on Vectors
• Euclidian norm or length x = x, x = xi2
i =1 n
More on Vectors
x x-y
x− y = (xi − yi ) 2
i =1 n
y
Orthonormal Basis
• x, y,…, z form an orthonormal basis
x = [1 0 0] x⋅ y = 0
T
y = [0 1 0]
T x⋅z = 0
z = [0 0 1]
T y⋅z = 0
Matrix Product
b11 b1m
a11 a12 a13 a1d b21 b2m
AB = b31 b3m = cij
an1 an2 an3 and
bd1 bdm cij = ai, bj
ai is row i of A
bj is column j of B
• # of columns of A = # of rows of B
• even if defined, in general AB BA
Matrices
• Rank of a matrix is the number of linearly
independent rows (or equivalently columns)
• A square matrix is non-singular if its rank equal
to the number of rows. If its rank is less than
number of rows it is singular.
1 0 0
0 1 0
• Identity matrix I=
0 0
AI=IA=A 1 2 9 5
0 0 1
T
2 7 4 8
• Matrix A is symmetric if A=A
9 4 3 6
5 8 6 4
Downloaded by Marisnelvys Cabreja (930mcc@gmail.com)
lOMoARcPSD|17897360
Matrices
-1
• Inverse of a square matrix A is matrix A s.t.
-1
AA = I
• If A is singular or not square, inverse does not
exist
T
• Pseudo-inverse A is defined whenever A A is
not singular (it is square)
T -1 T
A = (A A) A
T -1 T
AA =(A A) AA=I
MATLAB