Supervised Machine Learning
Supervised Machine Learning
1.1 Notations
m = Number of training examples
x’s = ”input” variable / features
y’s = ”output” variable / ”target” variable
(x,y) = single training example i.e. a single row in the table below
(x(i) , y(i) ) = ith training example
1
1.3 Basics
1.3.1 What is a model?
The algorithm you use for machine learning is called a model. It is sometimes also referred
to as a learning model.
hθ (x) = θ0 + θ1 x (1.1)
where θ0 and θ1 are the parameters of the model.
x(input) y(output)
0 4
1 7
2 7
3 8
Now we can make a random guess about our hθ function: θ0 = 2 and θ1 = 2. The
hypothesis function becomes hθ (x) = 2 + 2x.
2
m 2
1 X
J(θ0 , θ1 ) = hθ (x(i) ) − y (i) (1.2)
2m i=1
To break it apart, it is 21 x̄ where x̄ is the mean of the squares of hθ (x(i) ) − y (i) , or the
difference between the predicted value and the actual value.
This function is otherwise called the ”Squared error function”, or Mean squared error.
The mean is halved 21 m as a convenience for the computation of the gradient descent, as
the derivative term of the square function will cancel out the 12 term. (We will be seeing
this soon)
Now we are able to concretely measure the accuracy of our predictor function against the
correct results we have so that we can predict new results we don’t have.
Imagine that we graph our hypothesis function based on its fields θ0 and θ1 (actually we
are graphing the cost function for the combinations of parameters). We are not graphing x
and y itself, but the guesses of our hypothesis function.
We put θ0 on the x axis and θ1 on the z axis, with the cost function on the vertical y
axis. The points on our graph will be the result of the cost function using our hypothesis
with those specific theta parameters.
We will know that we have succeeded when our cost function is at the very bottom of the
pits in our graph, i.e. when its value is the minimum.
3
The way we do this is by taking the derivative (the line tangent to a function) of our
cost function. The slope of the tangent is the derivative at that point and it will give us a
direction to move towards. We make steps down that derivative by the parameter α, called
the learning rate.
Algorithm outline
1. Start with some θ0 , θ1
2. Keep changing θ0 , θ1 to reduce J(θ0 , θ1 )
3. repeat step 2 until we reach minimum
4
This could also be thought of as:
5
Thus, the partial derivatives work like this:
m 2 m
∂ ∂ 1 X 1 X ∂
g(θ0 , θ1 ) = f (θ0 , θ1 )(i) = 2 × f (θ0 , θ1 )2−1 θ0 =
∂θ0 ∂θ0 2m i=1 2m i=1 ∂θ0
m
1 X
f (θ0 , θ1 )(i) (1.8)
m i=1
∂ ∂
f (θ0 , θ1 )(i) = θ0 + θ1 x(i) − y (i) = 1 (1.9)
∂θ0 ∂θ0
Using chain rule,
∂ ∂ ∂
g(f (θ0 , θ1 )(i) ) = g(θ0 , θ1 ) f (θ0 , θ1 )(i) (1.10)
∂θ0 ∂θ0 ∂θ0
m m
1 X ∂ 1 X
f (θ0 , θ1 )(i) f (θ0 , θ1 )(i) = θ0 + θ1 x(i) − y (i) × 1 =
m i=1 ∂θ0 m i=1
m
1 X
θ0 + θ1 x(i) − y (i) (1.11)
m i=1
Similarly for θ1 . Our term g(θ0 , θ1 ) is identical, so we just need to take the derivative of
f (θ0 , θ1 )(i) , this time treating θ1 as the variable and the other terms as ”just a number.”
That goes like this:
∂ ∂
f (θ0 , θ1 )(i) = θ0 + θ1 x(i) − y (i) (1.12)
∂θ1 ∂θ1
∂ ∂
f (θ0 , θ1 )(i) = [a number] + θ1 [a number, x(i) ] − [a number] (1.13)
∂θ1 ∂θ1
∂ (1−1=0) (i)
f (θ0 , θ1 )(i) = 0 + (θ1 )1 x(i) − 0 = 1 × θ1 x = 1 × 1 × x(i) = x(i) (1.14)
∂θ1
∂ ∂ ∂
g(f (θ0 , θ1 )(i) ) = g(θ0 , θ1 ) f (θ0 , θ1 )(i) =
∂θ1 ∂θ1 ∂θ1
m m
1 X ∂ 1 X
f (θ0 , θ1 )(i) f (θ0 , θ1 )(i) = θ0 + θ1 x(i) − y (i) x(i) (1.15)
m i=1 ∂θ1 m i=1
6
So our final algorithm will be as follows
where m is the size of the training set, θ0 a constant that will be changing simultaneously
with θ1 and x(i) ,y (i) are values of the given training set (data).
Note that we have separated out the two cases for θ0 and that for θ1 we are multiplying
x(i) at the end due to the derivative.
The point of all this is that if we start with a guess for our hypothesis and then repeatedly
apply these gradient descent equations, our hypothesis will become more and more accurate.
1.4 Quiz
Quiz 1