Logistic Regression

Desprite the name it's a classification algorithm

Model function (Hypothesis)

We want a model function that is 0≤hθ(x)≤10 \leq h_\theta(x) \leq 1

hθ(x)=g(θTx)=11+e−θTxh_\theta(x) = g(\theta^Tx) = \frac{1}{1 + e^{-\theta^Tx}}

  • g is the Sigmoid function or Logistic function

    g(z)=11+e−zg(z) = \frac{1}{1+e^{-z}}

    Sigmoid Function

  • This value is the estimated probablity that y=1y = 1 on the given x

    hθ(x)=P(y=1∣x;θ)h_\theta(x) = P(y = 1|x;\theta)

    • "The probability that y=1y = 1, given x, parametrized by θ\theta"
  • We say y=1 when hθ(x)≥0.5h_\theta(x) \geq 0.5, i.e. z=θTx≥0z = \theta^Tx \geq 0

    • This basically defines a dividing line/plane/hyperplane between the two classes - The Decision Boundary

      Decision Boundary

Cost Function

We cannot use the same cost function as with Linear regression, since it will not be convex (i.e. it will have local minima)

So we use:

J(θ)=1m∑i=1mCost(hθ(x(i)),y(i))J(\theta) = \frac{1}{m} \sum_{i=1}^{m} Cost(h_\theta(x^{(i)}), y^{(i)})

With:

  • Cost(hθ(x),y)=−log(hθ(x))Cost(h_\theta(x), y) = -log(h_\theta(x)) if y = 1

    Logistic Regression Cost Function

  • Cost(hθ(x),y)=−log(1−hθ(x))Cost(h_\theta(x), y) = -log(1 - h_\theta(x)) if y = 0

    Logistic Regression Cost Function

Combined into one function that is:

Cost(hθ(x),y)=−y∗log(hθ(x))−(1−y)∗log(1−hθ(x))Cost(h_\theta(x), y) = -y * log(h_\theta(x)) - (1 - y) * log(1 - h_\theta(x))

This becomes:

J(θ)=−1m∑i=1m(y(i)∗log(hθ(x(i)))+(1−y(i))∗log(1−hθ(x(i))))J(\theta) = - \frac{1}{m} \sum_{i=1}^{m} (y^{(i)} * log(h_\theta(x^{(i)})) + (1 - y^{(i)}) * log(1 - h_\theta(x^{(i)})))

(Batch) Gradient Descent Algorithm

θj:=θj−α1m∑i=1m(hθ(x(i))−y(i))∗xj(i)\theta_j := \theta_j - \alpha \frac{1}{m}\sum_{i=1}^{m}(h_\theta(x^{(i)}) - y^{(i)}) * x^{(i)}_j

So

∂∂θjJ(θ)=1m∑i=1m(hθ(x(i))−y(i))∗xj(i)\frac{\partial}{\partial\theta_j}J(\theta) = \frac{1}{m}\sum_{i=1}^{m}(h_\theta(x^{(i)}) - y^{(i)}) * x^{(i)}_j