Logistic Regression

 

Logistic Regression

Logistic Regression is a simple yet powerful supervised machine learning algorithm mainly used for binary classification problems.

It predicts the probability of an outcome (such as success/failure or yes/no) based on input features.

Instead of giving a direct class, it outputs a probability (0 to 1), which is then used to assign a class.

It is named ‘Logistic Regression’ because its underlying technique is quite the same as Linear Regression. The term “Logistic” is taken from the Logit function that is used in this method of classification.

The logistic function, also called the sigmoid function was developed by statisticians to describe properties of population growth in ecology, rising quickly and maxing out at the carrying capacity of the environment. It’s an S-shaped curve that can take any real-valued number and map it into a value between 0 and 1, but never exactly at those limits.

๐Ÿ”ข Sigmoid (Logistic) Function

This is the heart of Logistic Regression:

ฯƒ(x)=11+ex

๐Ÿ”น What it does:

  • Takes any real number → converts it into a value between 0 and 1
  • Produces an S-shaped curve

๐Ÿ”น Interpretation:

  • Output ≈ 0 → Class 0 (Negative)
  • Output ≈ 1 → Class 1 (Positive)


๐Ÿ“ˆ The Core Idea of Logistic Regression

Logistic Regression:

  1. First applies a linear model
  2. Then passes the result through a sigmoid (logistic) function

๐Ÿงฎ Mathematical Model ( Binary )

Step 1: Linear Combination

t=ฮฒ0+ฮฒ1xt = \beta_0 + \beta_1 x

  • ฮฒ0\beta_0= intercept
  • ฮฒ1\beta_1 = coefficient
  • xx = input feature

Step 2: Apply Sigmoid Function

ฯƒ(x)=11+e(ฮฒ0+ฮฒ1x)​

๐Ÿ‘‰ This gives a probability value


๐ŸŽฏ Decision Boundary

  • A threshold (usually 0.5) is used:
    • If probability ≥ 0.5 → Class 1
    • If probability < 0.5 → Class 0

๐Ÿ“Œ Example:

  • 0.8 → “Yes”
  • 0.3 → “No”


⚖️ Why Logistic Regression Handles Outliers Better

  • Linear regression tries to fit a straight line → heavily affected by outliers
  • Logistic regression:
    • Uses sigmoid curve
    • Output is bounded (0–1)
    • Extreme values get compressed

๐Ÿ‘‰ So outliers don’t distort predictions as much


๐Ÿง  How Training Works in Logistic Regression

Training a logistic regression model means:

Finding the best parameters (weights) so that predicted probabilities match the actual class labels as closely as possible.


๐Ÿ”„ Step-by-Step Training Process

1. Start with the Model

We compute a linear combination of inputs:

z=ฮฒ0+ฮฒ1x1+ฮฒ2x2++ฮฒnxnz = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \dots + \beta_n x_n

Then apply the sigmoid function:

y^=11+ez\hat{y} = \frac{1}{1 + e^{-z}}

  • y^\hat{y}= predicted probability
  • Output is always between 0 and 1

❗ Why Not Use Mean Squared Error (MSE)?

In linear regression, we use MSE. But in logistic regression:

  • The sigmoid function makes the model non-linear
  • MSE leads to a non-convex loss function
  • This makes optimization difficult (multiple local minima)

๐Ÿ‘‰ So instead, we use Log Loss (Cross-Entropy Loss)


๐Ÿ“‰ Cost Function in Logistic Regression

๐Ÿ”น Binary Cross-Entropy (Log Loss)

J(ฮธ)=1mi=1m[y(i)log(y^(i))+(1y(i))log(1y^(i))]J(\theta) = -\frac{1}{m} \sum_{i=1}^{m} \left[ y^{(i)} \log(\hat{y}^{(i)}) + (1 - y^{(i)}) \log(1 - \hat{y}^{(i)}) \right]


๐Ÿ” Understanding the Formula

Terms:

  • mm = number of training examples
  • yy = actual label (0 or 1)
  • y^\hat{y} = predicted probability

๐Ÿ“Š Intuition Behind Log Loss

Case 1: When y=1y = 1

Loss=log(y^)\text{Loss} = -\log(\hat{y})
  • If y^=1\hat{y} = 1 → loss = 0 ✅ (perfect)
  • If y^\hat{y} is small → loss becomes very large ❌

Case 2: When y=0y = 0

Loss=log(1y^)\text{Loss} = -\log(1 - \hat{y})
  • If y^=0 → loss = 0 ✅
  • If y^\hat{y} is close to 1 → large penalty ❌






๐Ÿ’ก Key Insight

Log Loss:

  • Heavily penalizes wrong confident predictions
  • Encourages the model to output accurate probabilities

⚙️ How the Model Learns (Optimization)

Goal:

Minimize the cost function J(ฮธ)

Method:

๐Ÿ‘‰ Gradient Descent


๐Ÿ” Gradient Descent Update Rule

ฮธ=ฮธฮฑJ(ฮธ)\theta = \theta - \alpha \cdot \nabla J(\theta)
  • ฮธ\theta = model parameters
  • ฮฑ\alpha = learning rate
  • J(ฮธ)\nabla J(\theta) = gradient (direction of steepest increase)

๐Ÿ‘‰ We move in the opposite direction to reduce error


๐Ÿ”„ Training Loop

  1. Initialize weights randomly
  2. Compute predictions using sigmoid
  3. Calculate log loss
  4. Compute gradients
  5. Update weights
  6. Repeat until convergence

๐Ÿ“Š Real-Life Examples

  • ๐Ÿ“ง Spam detection → Spam / Not Spam
  • ๐Ÿฅ Tumor detection → Malignant / Benign
  • ๐Ÿ’ณ Fraud detection → Fraud / Genuine


๐Ÿ“‰ Advantages

✔ Simple and easy to implement
✔ Works well for linearly separable data
✔ Outputs probabilities (useful for decision-making)
✔ Fast and efficient


⚠️ Limitations

❌ Cannot capture complex non-linear relationships
❌ Assumes linear relationship between input and log-odds
❌ Sensitive to irrelevant features

๐Ÿ” Summary

Input FeaturesLinear EquationSigmoid FunctionProbabilityClass Label

 ๐Ÿ’กLogistic Regression is not actually “regression” in the traditional sense—it’s a classification algorithm that uses a regression-like equation + sigmoid transformation.

⚖️ Key Differences

Feature        Linear Regression    Sigmoid (Logistic)
Shape    Straight line    S-shaped curve
Output Range        −∞ to +∞    0 to 1
Use Case    Regression    Classification
Handles Probability    ❌ No    ✅ Yes


Appendix :Deriving the gradient descent update rule

๐Ÿ”น Cost Function (Log Loss)


J(ฮธ)=1mi=1m[y(i)log(hฮธ(x(i)))+(1y(i))log(1hฮธ(x(i)))]J(\theta) = -\frac{1}{m} \sum_{i=1}^{m} \left[y^{(i)} \log(h_\theta(x^{(i)})) + (1 - y^{(i)}) \log(1 - h_\theta(x^{(i)}))\right]


  • Penalizes wrong predictions heavily
  • Ensures convex optimization

๐Ÿ”น Gradient Descent Update Rule


ฮธ=ฮธฮฑ1mXT(hฮธ(x)y)\theta = \theta - \alpha \cdot \frac{1}{m} X^T (h_\theta(x) - y)

Where:

  • ฮฑ = learning rate
  • m= number of samples

Look at ONE training example

Instead of the full sum, take just one data point:

J=[ylog(h)+(1y)log(1h)]J = -\left[y \log(h) + (1-y)\log(1-h)\right]

where:

  • h=ฯƒ(z)h = \sigma(z)
  • z=ฮธTx

Think of it like layers (very important)

We don’t differentiate everything at once.

We go step by step:

ฮธ → z → h → J

So we use chain rule:

dJdฮธ=dJdhdhdzdzdฮธ\frac{dJ}{d\theta} = \frac{dJ}{dh} \cdot \frac{dh}{dz} \cdot \frac{dz}{d\theta}

Compute each part (simple pieces)

✅ 1. Derivative of cost w.r.t h

dJdh=hyh(1h)\frac{dJ}{dh} = \frac{h - y}{h(1-h)}

✅ 2. Derivative of sigmoid

dhdz=h(1h)\frac{dh}{dz} = h(1-h)

✅ 3. Derivative of z

dzdฮธ=x\frac{dz}{d\theta} = x

 Multiply them

Now multiply all three:

dJdฮธ=(hyh(1h))×(h(1h))×x\frac{dJ}{d\theta} = \left(\frac{h - y}{h(1-h)}\right) \times (h(1-h)) \times x

Simplification

The h(1h)h(1-h) cancels:

dJdฮธ=(hy)x\frac{dJ}{d\theta} = (h - y) \cdot x

For all data points

Average over all samples:

Jฮธ=1mXT(hy)\frac{\partial J}{\partial \theta} = \frac{1}{m} X^T (h - y)

Final Gradient Descent Rule

ฮธ=ฮธฮฑ1mXT(hy)

Comments

Popular posts from this blog

Machine Learning PCCST503 Semester5 KTU CS 2024 Scheme - Dr Binu V P

Introduction to Machine Learning (ML)

Distinguishing Machine Learning from Traditional Programming