Linear Regression with One Variable
trainning set
x | y |
---|---|
1 | 3 |
2 | 6 |
3 | 9 |
4 | 14 |
Trainning examples is 4 .
x is features
y is output
hθ=θ0+θ1x
is Hypothesis.
θ=[θ0θ1]
X=⎡⎣⎢⎢⎢11111234⎤⎦⎥⎥⎥=[x0x1]∈Rm∗(n+1)
m=4 is the number of trainning example
n = 1 is the numbei of feature
Hypothesis:
hθ=θ0+θ1x
Parameters:
θ0,θ1
cost Function:
J(θ)=J(θ0,θ1)=12m∑mi=1(hθ(x(i))−y(i))2=12m∑mi=1((θTx(i))−y(i))2=12m(Xθ−y).2
Goal:
minimizeJ(θ0,θ1)
Use Gradient descent algorithm minimize the cost function
J(θ0,θ1)
repeat{
θ0:=θ0−α1m∑mi=1(hθ(x(i))−y(i))=θ0−α1m∑mi=1((θTx(i))−y(i))
θ1:=θ1−α1m∑mi=1(hθ(x(i))−y(i))x(i)=θ1−α1m∑mi=1((θTx(i))−y(i))x(i)=θ1−α1m((Xθ−y)TX(:,2))
}
向量形式
repeat{
θ=θ−α1m((Xθ−y)TX)
}