DEV Community

shreya
shreya

Posted on

Logistic Regression

Logistic Regression Explained: A Beginner's Guide to Classification

By Data Science & Beyond (@shreya2006)

Discover how Logistic Regression predicts categories using probability
and classification techniques.

Introduction to Logistic Regression

Logistic Regression is a supervised machine learning algorithm commonly
used for classification. It estimates the probability that an input
belongs to a particular category.

Unlike Linear Regression, which predicts continuous numerical values,
Logistic Regression is used to predict categorical outcomes.

Examples include: - Whether an email is spam or not spam. - Whether a
customer is likely to purchase a product. - Whether a student is likely
to pass or fail.

Mathematical Formula

Logistic Regression uses the sigmoid function to convert a linear score
into a probability:

[ P(Y=1) = \frac{1}{1 + e^{-z}}{=tex} ]

The linear score is:

[ z = b_0 + b_1X_1 + b_2X_2 + \cdots{=tex}+ b_nX_n ]

Where:

  • P(Y=1): Estimated probability of the positive class.
  • e: Euler's mathematical constant.
  • z: Linear combination of input variables.
  • b₀: Intercept.
  • b₁, b₂, ... bₙ: Model coefficients.
  • X₁, X₂, ... Xₙ: Input features.

The sigmoid function returns a value between 0 and 1, which can be
interpreted as a probability under the model.

How Logistic Regression Works

  1. Collect and prepare the dataset.
  2. Identify input features and target categories.
  3. Calculate a linear combination of the input features.
  4. Apply the sigmoid function to the score.
  5. Estimate the probability of the positive class.
  6. Choose a decision threshold.
  7. Assign a predicted category based on that threshold.

A threshold of 0.5 is a common starting point for binary classification,
but it is not mandatory. The best threshold depends on the task and the
costs of false positives and false negatives.

Practical Example: Predicting Student Results

Suppose we want to estimate whether a student will pass or fail based on
study hours.


            Study hours  Estimated probability of Example prediction
                                          passing at 0.5 threshold
Enter fullscreen mode Exit fullscreen mode

                      1                      0.10 Fail

                      2                      0.25 Fail

                      3                      0.45 Fail

                      4                      0.65 Pass

                      5                      0.85 Pass
Enter fullscreen mode Exit fullscreen mode

If the estimated probability is 0.85, the model predicts the positive
class (Pass) using a 0.5 threshold.

These probabilities are illustrative examples, not results from a
trained model.

Types of Logistic Regression

1. Binary Logistic Regression

Used when there are two possible classes, such as Pass/Fail or Yes/No.

2. Multinomial Logistic Regression

Used when there are three or more unordered categories, such as Red,
Blue and Green.

3. Ordinal Logistic Regression

Used when target categories have a natural order, such as Low, Medium
and High.

Applications

Email spam detection

Classifies messages as spam or legitimate.

Healthcare

Estimates the probability of a particular medical outcome using relevant
data.

Banking

Helps estimate the likelihood of loan default.

Marketing

Predicts whether a customer may purchase a product.

Advantages

  • Relatively easy to understand and implement.
  • Provides probability estimates.
  • Computationally efficient for many classification tasks.
  • Works well when the relationship between features and log-odds is approximately linear.
  • Provides a useful baseline for classification problems.

Limitations

  • Assumes a linear relationship between features and log-odds.
  • May perform poorly with complex nonlinear decision boundaries.
  • Multicollinearity can affect coefficient estimates.
  • Feature engineering may be needed.
  • Outliers and influential observations can affect the model.

Linear Regression vs. Logistic Regression


Linear Regression Logistic Regression


Predicts numerical values Estimates class probabilities

Used for regression tasks Used for classification tasks

Often models a continuous outcome Models categorical outcomes

Output can be any real value Binary sigmoid output is between 0
and 1


Conclusion

Logistic Regression is an important supervised machine learning
algorithm for classification. It uses the sigmoid function to convert a
linear score into a probability. Its simplicity, efficiency and
probability estimates make it useful in healthcare, banking, marketing
and many other fields.


Continue learning: Read the next article in the Deep Learning
Explained
series to understand how Artificial Neural Networks learn
patterns from data.

machinelearning #datascience #python #ai

Top comments (0)