DEV Community

Cover image for Logistic Regression Algorithm
likhitha manikonda
likhitha manikonda

Posted on

Logistic Regression Algorithm

What Is Logistic Regression?

Logistic Regression is a supervised machine learning algorithm used for classification, not regression.
It predicts categories (like yes/no, spam/not spam, pass/fail), not numbers.
Example: Will a house sell above a certain price? Is an email spam?

How Is It Different from Linear Regression?
Linear Regression: Predicts a continuous value (e.g., house price).
Logistic Regression: Predicts a probability that something belongs to a class (e.g., probability of passing an exam).

How Does Logistic Regression Work?
It uses a mathematical function called the sigmoid to turn predictions into probabilities between 0 and 1.
If the probability is above a threshold (usually 0.5), it predicts one class; otherwise, it predicts the other. Probability is considered usually with 0.5 because the predictions are always between 0 and 1.

Real-World Example of Logistic Regression
Scenario:
Suppose you work for a bank and want to predict whether a customer will default on a loan (yes/no) based on features like income, age, and loan amount.

Features: Income, Age, Loan Amount
Target: Default (1 = Yes, 0 = No)

Logistic regression helps you predict the probability that a customer will default. If the probability is above 0.5, you predict “Yes”; otherwise, “No”. The probability is above 0.5 because the output/target is always 0 or 1.

Other common examples:

  • Predicting if an email is spam or not spam.
  • Predicting if a patient has a disease (yes/no) based on medical test results.
  • Predicting if a student will pass or fail an exam.

Why Can’t We Use Linear Regression for Problems Meant for Logistic Regression?
1. Type of Prediction
Linear Regression: Predicts a continuous number (e.g., house price, temperature).
Logistic Regression: Predicts a category/class (e.g., yes/no, spam/not spam, pass/fail).

2. Output Range
Linear Regression: Can output any number, from negative infinity to positive infinity.
Logistic Regression: Outputs a probability between 0 and 1 (using the sigmoid function), which is then used to classify into categories.

3. Example Problem
Suppose you want to predict if a student will pass or fail an exam (yes/no):
Linear Regression might give you predictions like 1.2, -0.3, 0.7, which don’t make sense for categories.
Logistic Regression gives you probabilities (e.g., 0.8 means likely to pass, 0.2 means likely to fail), and you can set a threshold (like 0.5) to decide the class.

4. Interpretation
Linear Regression: Not designed for classification; its predictions can be outside the valid range for categories.
Logistic Regression: Designed for classification; its predictions are always valid probabilities.

5. Mathematical Reason
Linear Regression: Fits a straight line.
Logistic Regression: Fits an S-shaped curve (sigmoid) that maps any input to a value between 0 and 1.

Key Points to Remember

  1. Use logistic regression when your target is a category/class.
  2. Evaluate with accuracy, precision, recall, F1-score—not RMSE or R².
  3. Visualize results to see how well your model is classifying.

🧩 If this piece fit perfectly into your brain puzzle, the next one might just complete the picture. Slide on over! 🧠🧵https://dev.to/codeneuron/how-to-check-if-logistic-regression-works-for-your-dataset-1853

Top comments (0)