Logistic Regression using gradient descent ( sample data)
Experiment:
Simple Logistic Regression🎯 Aim
To implement Logistic Regression from scratch using NumPy and apply it to a sample dataset for binary classification.
📘 Theory
🔹 What is Logistic Regression?
Logistic Regression is a supervised learning algorithm used for binary classification problems. Unlike linear regression, it predicts probabilities using a sigmoid function.
🔹 Hypothesis Function
We model the probability as:
- Output lies between 0 and 1
- Interpreted as probability of class 1
🔹 Cost Function (Log Loss)
- Penalizes wrong predictions heavily
- Ensures convex optimization
🔹 Gradient Descent Update Rule
Where:
- = learning rate
- = number of samples
Look at ONE training example
Instead of the full sum, take just one data point:
where:
Think of it like layers (very important)
We don’t differentiate everything at once.
We go step by step:
So we use chain rule:
Compute each part (simple pieces)
✅ 1. Derivative of cost w.r.t h
✅ 2. Derivative of sigmoid
✅ 3. Derivative of z
Multiply them
Now multiply all three:
Simplification
The cancels:
For all data points
Average over all samples:
Final Gradient Descent Rule
🧾 Algorithm
- Initialize weights and bias
-
Compute linear combination:
- Apply sigmoid function
- Compute cost
- Update weights using gradient descent
- Repeat until convergence
- Predict using threshold (0.5)
📊 Sample Dataset
We use a simple dataset for binary classification:
| Feature 1 | Feature 2 | Label |
|---|---|---|
| 1 | 2 | 0 |
| 2 | 3 | 0 |
| 3 | 3 | 0 |
| 5 | 6 | 1 |
| 6 | 7 | 1 |
💻 Program
📈 Output (Sample)
Result
- Logistic Regression model was successfully implemented using NumPy.
- Gradient descent optimizes parameters
- Sigmoid function enables classification
- The model correctly classified the dataset with high accuracy.

Comments
Post a Comment