๐ Machine Learning Series โ: Understanding Classification & Logistic Regression
In our previous post, we discussed Supervised Learning and understood how machines learn from labelled data.
Now, let's take the next step!
Have you ever wondered how Gmail identifies whether an email is Spam or Not Spam? Or how a banking system identifies whether a transaction is Fraudulent or Genuine?
Well, one of the techniques behind these predictions is called Classification.
Let's understand it from the basics. ๐
๐ 1. What is Classification?
Classification is a Supervised Machine Learning technique that helps a model predict which category or class a particular data point belongs to.
In simple words, we train a machine using examples that already have labels. After learning the patterns, it can use that knowledge to classify new, unseen data.
For example:
- Email โ Spam or Not Spam
- Medical Diagnosis โ Benign or Malignant
- Banking โ Fraudulent or Genuine
- Student Performance โ Pass or Fail
Imagine Gmail receiving a new email. It checks patterns such as suspicious words, links, and sender information and predicts whether the email belongs to the Spam or Not Spam category.
That's classification in action!
๐ 2. Types of Classification
Classification is generally divided into three major types:
1. Binary Classification
When a model has to choose between only two possible categories.
Examples:
- Yes / No
- Spam / Not Spam
- Fraud / Genuine
2. Multiclass Classification
When a model has to choose exactly one category from more than two available classes.
Examples:
- Animal Classification โ Cat, Dog, Bird, Horse
- Grade Prediction โ A, B, C, D
3. Multilabel Classification
Sometimes, one data point can belong to multiple categories at the same time.
For example, a movie can belong to:
- Action
- Comedy
- Adventure
Unlike multiclass classification, here the model can predict multiple labels for one input.
๐ค 3. Popular Classification Algorithms
Now that we understand classification and its types, let's look at some popular algorithms used to solve classification problems.
- Logistic Regression
- K-Nearest Neighbors (KNN)
- Decision Tree Classifier
- Random Forest Classifier
- Support Vector Machine (SVM)
- Naive Bayes Classifier
- Gradient Boosting
- AdaBoost
- XGBoost
- Neural Networks
Each algorithm follows a different approach to identify patterns and classify data.
Today, let's start with our first algorithm!
๐ง 4. Logistic Regression โ Our First Classification Algorithm
Despite having the word "Regression" in its name, Logistic Regression is mainly used for classification problems.
It helps us predict the probability of an input belonging to a particular class.
Let's understand this with a simple example.
Imagine we want to predict whether a student will pass or fail based on their study hours.
Our model receives the student's study hours and predicts:
- Probability = 0.90 โ 90% chance of passing.
- Probability = 0.20 โ 20% chance of passing.
But how does the algorithm calculate these probabilities?
Let's break down its working.
โ๏ธ 5. How Does Logistic Regression Work?
Step 1: Input Features
First, the model takes input features from the dataset.
For our student example:
- Study hours
- Attendance
- Previous examination marks
These features help the model understand the student's performance patterns.
Step 2: Linear Combination
The algorithm calculates a weighted sum of the input features.
Formula:
z = bโ + bโxโ + bโxโ + ... + bโxโ
Here:
- z = Calculated output
- x = Input features
- b = Weights or coefficients
- bโ = Bias or intercept
But there's a small problem!
The output of this equation can be any value, from negative infinity to positive infinity.
However, a probability must always be between 0 and 1.
So, how do we solve this?
That's where the Sigmoid Function comes into the picture!
๐ 6. What is the Sigmoid Function, and Why Do We Use It?
The Sigmoid Function is a mathematical function that converts any real-valued number into a value between 0 and 1.
In simple words, it takes the output of the linear equation and converts it into a probability-like score that we can use for classification.
Its graph looks like an English letter 'S', which is why it is also called the S-shaped curve.
Mathematical Formula:
ฯ(z) = 1 / (1 + eโปแถป)
Where:
- ฯ(z) = Output of the Sigmoid Function
- z = Input from the linear equation
- e = Euler's constant (approximately 2.718)
Let's understand with an example:
| Input (z) | Sigmoid Output |
|---|---|
| -2 | 0.12 |
| 0 | 0.50 |
| 2 | 0.88 |
Notice something interesting?
- When the input is highly negative, the output moves closer to 0.
- When the input is 0, the output is 0.5.
- When the input is highly positive, the output moves closer to 1.
Why is this useful?
Because the model can now use these outputs as estimated probabilities to decide which class an input belongs to.
๐ฏ 7. What is a Decision Threshold?
Now we have a probability, but how does the model decide whether the student will pass or fail?
This is where the Decision Threshold comes in.
A decision threshold is a value used to convert the predicted probability into a final class.
The commonly used default threshold is 0.5.
For example:
- Probability โฅ 0.5 โ Pass (Class 1)
- Probability < 0.5 โ Fail (Class 0)
So, if our model predicts 0.85, it classifies the student as Pass.
If it predicts 0.25, it classifies the student as Fail.
Remember, the threshold is not always fixed at 0.5. We can adjust it depending on the requirements of our problem.
๐ 8. How Does Logistic Regression Learn?
You might be wondering:
How does the model know which weights are correct?
Initially, the model has some weights and makes predictions. It then compares its predictions with the actual answers.
To measure how wrong its predictions are, it uses a loss function called Log Loss (Binary Cross-Entropy).
The objective is to minimize this loss so that the predicted probabilities become closer to the actual labels.
Logistic Regression commonly uses Maximum Likelihood Estimation (MLE) to estimate its coefficients.
Optimization methods such as Gradient Descent can be used to find the weights that minimize the loss.
In simple words, the model keeps improving its parameters during training so that it can make better predictions.
โ 9. Advantages of Logistic Regression
- Easy to understand and implement.
- Computationally efficient.
- Provides probability estimates.
- Works well for many binary classification problems.
- Its coefficients help us understand the relationship between features and predictions.
โ ๏ธ 10. Limitations of Logistic Regression
- It struggles with complex nonlinear relationships unless features are transformed or engineered.
- It can be sensitive to outliers.
- It may not perform well when important features are missing.
- It assumes a linear relationship between input features and the log-odds of the outcome.
๐ 11. Real-World Applications
We can find Logistic Regression in many practical applications:
- Healthcare โ Predicting disease risk.
- Banking โ Credit risk classification.
- Cybersecurity โ Spam and threat detection.
- Marketing โ Customer churn prediction.
- Finance โ Fraud detection.
๐ก Final Takeaway
So, today we explored Classification, its three major types, and our first classification algorithm: Logistic Regression.
The most important thing to remember is:
Logistic Regression first calculates a probability using the Sigmoid Function and then uses a decision threshold to predict the final class.
We'll continue this journey by exploring each classification algorithm one by one, understanding its working, mathematical intuition, and practical implementation using Python.
๐ฌ Let's Discuss!
Here's a question for everyone:
Why can't we simply use Linear Regression for classification? Why do we need the Sigmoid Function?
Share your thoughts in the comments. Let's learn and grow together! ๐
MachineLearning #Classification #LogisticRegression #SupervisedLearning #Python #DataScience #ArtificialIntelligence
Top comments (0)