Python para Data Science e Analytics
Python para Data Science e Analytics › Guides › What Is Logistic Regression?

What Is Logistic Regression?

Logistic regression is a classification model: it estimates the probability that an example belongs to a class and returns a value between 0 and 1. A decision threshold then turns that probability into “class A” or “class B.” Linear regression answers a different question — how much the target is worth — so it does not replace logistic regression for binary classification.

Topics: regressão logística, classificação, machine learning

What does logistic regression do that linear regression cannot?

Logistic regression solves classification problems. It takes the input variables, computes a linear combination of them, and applies the sigmoid function to that result. The sigmoid compresses any number into the interval between 0 and 1, and the resulting value is read as the estimated probability of the positive class.

The model does not return “yes” or “no” directly. It returns a continuous score, and the conversion to a class happens in a later step, when you choose a threshold. That separation between probability and decision is why logistic regression is often preferred when one type of error costs more than the other.

On imbalanced data — few positive cases, many negative ones — this matters even more. A linear regression trained on 0/1 labels would produce predictions outside the probability range and would assume a linear relationship between the input and the chance of occurrence.

How do odds and the logit work in logistic regression?

Odds are the ratio between the probability that an event happens and the probability that it does not, that is, p divided by (1 minus p). If the estimated probability is 0.8, the odds are 4: the event is four times more likely to occur than not to occur. At probability 0.5, the odds are 1 — a balance point.

Logistic regression models the natural logarithm of the odds, called the logit, as a linear function of the input variables. That is why the model coefficients come out on a log-odds scale and need to be transformed before they can be read as an effect on probability.

In practical reading, a positive coefficient pushes probability up and a negative coefficient pushes it down. The magnitude, however, depends on the scale of the variable: comparing coefficients of variables measured in different units without standardizing them leads to a wrong conclusion about which one weighs more.

What is a decision threshold, and why does it change the result?

The decision threshold is the cut that converts probability into a class. The most common default is 0.5: above it, the model predicts the positive class; below it, the negative class. That number is an analyst’s choice, not a property of the model.

Moving the threshold changes the balance between false positives and false negatives. If letting a positive case pass is expensive — in fraud detection, for example — it makes sense to lower the threshold and live with more false alarms. If the cost is in the false alarm, the path is to raise the threshold and accept more missed cases.

For that reason, evaluating the model only by accuracy at a 0.5 threshold misleads on imbalanced data: a model that always predicts the majority class can look good. Confusion matrix, precision, recall, F1, and ROC curve with AUC show behavior across several thresholds. The final threshold should come from the cost of error in the application context.

What is the difference between logistic regression and linear regression?

The central difference is the type of question. Linear regression estimates a continuous value — price, quantity, time — and is fitted by least squares. Logistic regression estimates class probability and is fitted by maximum likelihood. They are models for different problems, not versions of each other.

The assumptions also diverge. Linear regression assumes a linear relationship between inputs and the expected value of the target, and it treats errors as normally distributed for inference. Logistic regression assumes that the logit of the probability is linear in the inputs and that observations are independent; it does not assume normally distributed errors in the same way.

Both produce coefficients that are easy to inspect, and both are common baselines. The choice comes from the target: a continuous measurement calls for linear regression; a binary label calls for logistic regression. Using a linear model on a binary target can rank examples, but its outputs are not calibrated probabilities.

Where can you practice logistic regression in Python?

The Python for Data Science and Analytics course covers logistic regression inside its machine learning module, alongside linear regression, K-Means, SVM, Naive Bayes, decision tree, Random Forest, Gradient Boosting, XGBoost, LightGBM, feature selection, and cross-validation. The course runs from zero to advanced in three main modules — Basic, Intermediate, and Advanced — plus bonus modules.

Before machine learning, it covers IDE and Jupyter Notebook setup, basic functions, NumPy and Pandas, user-defined functions, loops and conditionals, data visualization, data cleaning and preparation, and outlier detection. The final project is an end-to-end exploratory analysis and machine learning project. Lessons are recorded in high quality and watched at the student’s pace; exercises are guided, and real market projects help build a portfolio.

You do not need to know how to program before you start. The course includes a certificate of completion, an exclusive community for networking, and unlimited access with no expiration date. The price is R$ 470, or 12 installments of R$ 20.37 on a credit card. Payment goes through the Hotmart checkout, with credit card or boleto. You can see the course at python.paanalytics.net and start at the checkout link.

In short, it is a Python for Data Science and Analytics course, from zero to advanced, with a machine learning project, exercises, certificate, and permanent access.

Start with the machine learning module

Secure payment via Hotmart, up to 12 installments.

Enroll now Product site

Frequently asked questions

Is logistic regression a regression or a classification algorithm?

It is used for classification. The name comes from the logistic function and from the fact that the model is fitted like a regression, but the output is a class probability, not a continuous target.

Do I need to understand odds to use logistic regression?

No. You can fit and evaluate the model without computing odds by hand. Odds and the logit help explain why coefficients are on a log-odds scale and why the effect on probability changes with the starting point.

Should I always use 0.5 as the decision threshold?

No. 0.5 is a default, not a rule. The threshold should reflect the cost of false positives versus false negatives in the application. Evaluate precision, recall, F1, and the ROC curve before choosing it.

Can logistic regression handle more than two classes?

Binary logistic regression handles two classes. Multiclass extensions exist, such as multinomial logistic regression and one-vs-rest, but the standard binary form is what the page describes.

Does the course require prior programming experience?

No. The course starts from zero and covers IDE and Jupyter Notebook setup, basic functions, NumPy and Pandas, loops, conditionals, and user-defined functions before machine learning. It also covers logistic regression in the machine learning module.

Ler em português →