Linear regression with one variable Cost function Machine Learning
Training Set Hypothesis: ‘s: Parameters How to choose ‘s ? Size in feet2 (x) Price ($) in 1000's (y) 2104 460 1416 232 1534 315 852 178 … Hypothesis: ‘s: Parameters How to choose ‘s ?
Idea: Choose so that is close to for our training examples y x Idea: Choose so that is close to for our training examples
Cost function intuition I Linear regression with one variable Cost function intuition I Machine Learning
Simplified Hypothesis: Parameters: Cost Function: Goal:
(for fixed , this is a function of x) (function of the parameter ) y x
(function of the parameter ) (for fixed , this is a function of x) y x
(function of the parameter ) (for fixed , this is a function of x) y x
Cost function intuition II Linear regression with one variable Cost function intuition II Machine Learning
Hypothesis: Parameters: Cost Function: Goal:
(for fixed , this is a function of x) (function of the parameters ) Price ($) in 1000’s Size in feet2 (x)
(for fixed , this is a function of x) (function of the parameters )
(for fixed , this is a function of x) (function of the parameters )
(for fixed , this is a function of x) (function of the parameters )
(for fixed , this is a function of x) (function of the parameters )
Linear regression with one variable Gradient descent Machine Learning
Have some function Want Outline: Start with some Keep changing to reduce until we hopefully end up at a minimum
J(0,1) 1 0
J(0,1) 1 0
Gradient descent algorithm Correct: Simultaneous update Incorrect:
Gradient descent intuition Linear regression with one variable Gradient descent intuition Machine Learning
Gradient descent algorithm
If α is too small, gradient descent can be slow. If α is too large, gradient descent can overshoot the minimum. It may fail to converge, or even diverge.
at local optima Current value of
Gradient descent can converge to a local minimum, even with the learning rate α fixed. As we approach a local minimum, gradient descent will automatically take smaller steps. So, no need to decrease α over time.
Gradient descent for linear regression Linear regression with one variable Gradient descent for linear regression Machine Learning
Gradient descent algorithm Linear Regression Model
Gradient descent algorithm update and simultaneously
Gradient descent example 𝑡ℎ𝑒𝑡𝑎1=2 theta0 = - 1 alpha = 0.01 X y h error h-y (h-y)x 1 2 3 6 5 10
J(0,1) 1 0
(for fixed , this is a function of x) (function of the parameters )
(for fixed , this is a function of x) (function of the parameters )
(for fixed , this is a function of x) (function of the parameters )
(for fixed , this is a function of x) (function of the parameters )
(for fixed , this is a function of x) (function of the parameters )
(for fixed , this is a function of x) (function of the parameters )
(for fixed , this is a function of x) (function of the parameters )
(for fixed , this is a function of x) (function of the parameters )
(for fixed , this is a function of x) (function of the parameters )
Logistic Regression Classification Machine Learning
Classification Email: Spam / Not Spam? Online Transactions: Fraudulent (Yes / No)? Tumor: Malignant / Benign ? 0: “Negative Class” (e.g., benign tumor) 1: “Positive Class” (e.g., malignant tumor)
Classification: y = 0 or 1 can be > 1 or < 0 Logistic Regression:
Hypothesis Representation Logistic Regression Hypothesis Representation Machine Learning
Sigmoid function Logistic function Logistic Regression Model Want 1 0.5 Sigmoid function Logistic function
Logistic regression z 1 Suppose predict “ “ if predict “ “ if
Logistic Regression Cost function Machine Learning
Training set: m examples How to choose parameters ?
Cost function Linear regression: “non-convex” “convex”
Logistic regression cost function If y = 1 1
Logistic regression cost function If y = 0 1
Simplified cost function and gradient descent Logistic Regression Simplified cost function and gradient descent Machine Learning
Logistic regression cost function
Logistic regression cost function To fit parameters : To make a prediction given new : Output
Gradient Descent Want : Repeat (simultaneously update all )
Algorithm looks identical to linear regression! Gradient Descent Want : Repeat (simultaneously update all ) Algorithm looks identical to linear regression!
Gradient Descent Want : Repeat (simultaneously update all )
Algorithm looks identical to linear regression! Gradient Descent Want : Repeat (simultaneously update all ) Algorithm looks identical to linear regression!
Chain rule
Derivation of logistic regression
Now Derive From
code to compute code to compute code to compute code to compute theta = function [jVal, gradient] = costFunction(theta) jVal = [ ]; code to compute gradient(1) = [ ]; code to compute gradient(2) = [ ]; code to compute gradient(n+1) = [ ]; code to compute