Logistic regression is a topic that most machine learning courses cover. None of these courses will explain to you why the L2 regularization is being applied to your model automatically behind the scenes, why your “97% accurate” fraud detection model is totally useless, or why the 0.5 decision threshold that was recommended by all beginner guides is probably robbing some people of their money.
This guide will explain everything from the beginning to the end, from the Sigmoid function to production pipeline. You can skip to the specific section via the table of contents below.
What Will I Learn?
What Is Logistic Regression?
Logistic regression is a supervised machine learning technique which provides the probability whether the given input will belong to a certain class or not. For a given set of inputs, logistic regression gives out a value between 0 and 1 (probability) which you can use for classification.
The term logistic regression is somewhat confusing since although the word ‘regression’ has been used, it deals with classification and not regression.
The most concise answer is: you have emails that can either be spam or not; you have loan applications that will either be defaulted upon or not; and you have tumors where their scan result will either indicate malignancy or not. The logistic regression will learn a boundary between those two classes and will give you the probability that an unknown input belongs to the “yes” class.
Statistical wise, it was developed way back in the 1950s when British statistician David Cox introduced it as a classification technique. Even today after many decades, logistic regression remains a popular choice of model for credit scoring, clinical trials, and in situations where a regulatory agency needs to know the exact mechanics of the model.
Logistic Regression vs. Linear Regression — The Key Difference
In both cases, you get a linear function mapping your input variables to the output variable. But what differs is the usage of that function.
Linear regression provides a numeric prediction. Input is your square feet and location, and output is your house price ($0 – $10M).
Logistic regression gives you a probability. Input is your financial characteristics, and output is a number between 0 and 1. If the number is above some threshold, the algorithm returns yes; otherwise, it returns no.
But you cannot apply linear regression for classification because its output is not bound to be within [0, 1]. A model telling you that there is a probability of 1.7 that a person will default on his loan is useless.
| Linear Regression | Logistic Regression | |
| Output type | Any real number | Probability between 0 and 1 |
| Problem type | Regression | Classification |
| Fitting method | Ordinary least squares | Maximum likelihood estimation |
| Output shape | Straight line | S-shaped curve |
| Use when | Predicting continuous values | Predicting a category or probability |
How Logistic Regression Works — Step by Step
That’s how the algorithm works under the hood, without all the notation at the start.
Step 1: Compute a linear score. The model takes your input features, multiplies each feature with a corresponding weight that it has learned. Adds a bias term. It gives a scalar; let’s name it z. It’s precisely what linear regression does. At this point, z can take any value: -300, 0, 47, etc.
Step 2: Squeeze z into a probability. The model applies the sigmoid function to z, thus restricting it between 0 and 1. The more positive z is, the closer its output is to 1. The more negative z is, the closer its output is to 0.
Step 3: Apply a threshold. Set a threshold. If the probability is greater than your threshold, make a prediction for class 1. Otherwise, make a prediction for class 0. By default, the threshold equals 0.5, but you will soon find out how dangerous this choice is.
Now let us look at an example for spam filtering. Your feature set may comprise: word count, link count, presence of ‘free’ in the email’s subject line, sender reputation rating. A value for every one is assigned. High values of word count and link count from an unknown sender give you a high value of z which is converted by the sigmoid function to 0.94. This is more than 0.5 – spam.
The Sigmoid Function — Why It Exists
Logistic regression uses sigmoid functions. The formula for this function looks like this:
Where e = 2.718 (Euler’s number).
A few important facts about this function:
- If z = 0, then σ(z) = 0.5
- If z is high (for example, +10), then σ(z) tends to 1 but doesn’t become 1
- If z is low (for example, −10), then σ(z) tends to 0 but doesn’t become 0
- This function is fully derivable, which is important in gradient descent algorithm
And the main feature is the S-shape. It automatically “saturates” on both sides, which means the result can’t be out of [0, 1]. And that is what you need for probability.
Log-Odds, Odds, and Logit — Explained Without the Math Anxiety
This section has been provided because it seems that many papers use these concepts without justifying their relevance. In order to properly evaluate the coefficients in your model, it is crucial that you understand the concepts of odds and log-odds, which is extremely important in the fields of medicine and finance.
Odds can be used to compare the probability of an occurrence to its nonoccurrence. For example, if a credit scoring model estimates that there is a probability of 0.75 of default, then the odds of default is 3:1 (0.75/0.25 = 3). In other words, if the odds of an event are greater than 1, then the probability of its occurrence is higher than the probability of its nonoccurrence.
Unfortunately, the concept of odds is asymmetrical because odds of 2 (“twice as likely”) and odds of 0.5 (“half as likely”) are opposites.
Log-odds, also known as the logit, solve this problem. The natural logarithm of the odds has a symmetric scale ranging from negative infinity to positive infinity. Positive log-odds indicate that an event is more probable than not; negative log-odds suggest otherwise.
The reason why this is important is because logistic regression models the log-odds as a linear function of your predictors. In other words, each coefficient is equal to the increment of the log-odds for one-unit change in that particular predictor. Once you exponentiate a coefficient, you obtain the odds ratio, which is a multiplicative factor showing how the odds change. For example, a coefficient of 0.8 would yield an odds ratio of 2.2: a one-unit increase in the feature would multiply the odds by 2.2.
In regulated industries, auditors ask for exactly this interpretation. “How does income affect the probability of loan approval?” needs an answer in odds ratios, not just “the model said so.”
Types of Logistic Regression
These come in three types, and depending on your target variable, you would need one of the three.
Binary Logistic Regression. Either spam or not. Either fraud or not. Either churned or not. And by far, this is the most common type that people talk about in tutorials and is the one that sklearn’s LogisticRegression handles by default.
Multinomial logistic regression for multiclass classification problems where the classes are unordered, there is. Classification of support tickets into billing, technical, or account category. Classification of patients into any of the five disease categories. Behind the scenes, the sigmoid function gets replaced by the softmax function which guarantees that the sum of probabilities across all classes adds up to one.
Ordinal Logistic Regression is used for ordered categories where A is “greater” than B. Customer satisfaction scale (very dissatisfied → neutral → very satisfied). Staging of cancer patients (Stage I → Stage II → Stage III). Regular sklearn does not have an implementation of ordinal logistic regression; you will need statsmodels or mord for that.
Assumptions of Logistic Regression
Violation of these assumptions does not necessarily imply that your model is inaccurate; it implies that you need to verify whether there is an influence of error magnitude.
1. Independent observations. Your data points in rows should be independent. This assumption is violated in case of time series data (stock prices are dependent on each other in consecutive days), clustered data (patients from one hospital), or in case of repeated measurements of the same patient. In such cases, even though your logistic regression model will work fine, the calculation of standard errors will be incorrect.
2. A linear relationship between features and log-odds. You don’t have a linear relationship between your features and outcome variables. However, you need to have a linear relationship between your features and log-odds of your outcome variable. You can test this by making a scatterplot of each continuous feature vs. log-odds of the target.
3. No extreme multicollinearity. In cases when two of your predictors are highly correlated (e.g., income and loan amount in the credit risk model), logistic regression cannot estimate their independent effect. You’ll receive unsteady coefficients which won’t be interpretable anymore. How to check: calculate variance inflation factors (VIFs). The value of the VIF should not exceed 5 or 10.
4. No extreme outliers. One single outlier will affect your decision boundary in a strong way. Logistic regression is much more resistant to outliers compared to linear regression but it still can be affected by them.
5. A reasonably large sample size. Rule of thumb used by practitioners: at least 10 events per predictor. So, for example, if you have 15 predictors and 150 positive samples in a binary classification task, you are on the verge. Less than that and you can consider your estimations of the coefficients to be biased.
The Math Behind Logistic Regression (For Those Who Want It)
You will be able to apply logistic regression effectively without having read this section. Just jump ahead directly to the implementation part in Python code if you do not care about understanding why this algorithm works in certain ways (under-performs for class imbalance problem, fails to converge, what does the C parameter actually regulate).
The Logistic Regression Equation
Starting from a standard linear combination:
Where w is the weight vector, X is your feature matrix, and b is the bias term. We then apply the sigmoid:
This gives the probability that the observation belongs to class 1.
Maximum Likelihood Estimation — The Intuition
The maximum likelihood method (MLE) is what the algorithm uses to determine its parameters. This is how it works intuitively: “what values of w and b will give us the highest chance to see the observed data?”
Imagine it as finding a lock for a door. You have a number of doors (each point from your dataset) which have locks that can either be labeled as “0” or “1”. MLE tests different keys (various w and b) until it gets a key that will open as many doors as possible – that is, it will maximize the chance of having these labels on your doors.
Intuitively speaking, MLE solves the problem of maximizing the following log-likelihood function:
This is also called cross-entropy loss or log loss, just negated. When you minimize log loss, you’re maximizing log-likelihood. Same thing, different sign.
Gradient Descent Updates the Weights
It is impossible to obtain a solution for the maximum likelihood estimate using analytical methods. Rather, it relies on an optimization process which usually takes the form of some form of gradient descent to update the weight to maximize the log-likelihood function.
The gradient of the log-likelihood function with respect to the weight j is:
The algorithm calculates the gradient, takes a step in that direction, and iterates until the weights stabilize. This is called convergence. When you get an error message from sklearn saying that the model “did not converge,” this means that the algorithm reached the maximum number of iterations without the weights stabilizing.
How to Implement Logistic Regression in Python
Below is a complete, runnable pipeline — not just fit and predict. This is closer to how you’d actually build it in practice.
python
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.metrics import (
accuracy_score, precision_score, recall_score,
f1_score, roc_auc_score, confusion_matrix,
classification_report
)
# Load data
X, y = load_breast_cancer(return_X_y=True)
# Stratified split -- keeps class proportions in train and test
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
# Pipeline: scale features first, then fit the model
# StandardScaler is not optional -- logistic regression is sensitive to feature scale
pipeline = Pipeline([
('scaler', StandardScaler()),
('model', LogisticRegression(
solver='lbfgs', # Good default; see solver section below
penalty='l2', # L2 regularization is on by default
C=1.0, # C = 1/lambda; smaller = stronger regularization
max_iter=1000, # Increase if you see convergence warnings
random_state=42
))
])
pipeline.fit(X_train, y_train)
# Predictions
y_pred = pipeline.predict(X_test)
y_prob = pipeline.predict_proba(X_test)[:, 1] # Probabilities for class 1
# Evaluation
print("Accuracy: ", accuracy_score(y_test, y_pred))
print("Precision:", precision_score(y_test, y_pred))
print("Recall: ", recall_score(y_test, y_pred))
print("F1 Score: ", f1_score(y_test, y_pred))
print("AUC-ROC: ", roc_auc_score(y_test, y_prob))
print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("\nFull Report:\n", classification_report(y_test, y_pred))
A few things worth flagging about this code:
The stratify=y argument in the train/test split is non-negotiable for imbalanced datasets. Without it, your test set might have a different class ratio than your training set, which skews every metric.
The Pipeline object isn’t just stylistic. It prevents data leakage. If you fit the scaler on the full dataset before splitting, you’ve leaked information from the test set into training. The pipeline fits the scaler only on training data and applies the same transformation to test data. Always use it.
And the C=1.0 default? That’s worth understanding before you leave it untouched. More on that next.
Choosing the Right Solver in scikit-learn
This section exists on no other logistic regression guide on the first page of Google. And it matters, because the wrong solver either runs slowly, fails to converge, or doesn’t support the regularization type you want.
Scikit-learn offers five solvers. Here’s what you need to know about each:
| Solver | Best for | Supports L1? | Supports L2? | Notes |
| lbfgs | Small-to-medium datasets, L2 penalty | No | Yes | The default since sklearn 0.22. Good general choice. |
| liblinear | Small datasets, L1 or L2 | Yes | Yes | Only one vs. rest for multiclass. Fast on small datasets. |
| saga | Large datasets, any penalty | Yes | Yes | Slowest per iteration but supports all penalty types including Elastic Net. |
| newton-cg | Medium datasets, L2 | No | Yes | Similar to lbfgs but slower on large datasets. |
| newton-cholesky | Medium datasets with many samples, L2 | No | Yes | Added in sklearn 1.2; faster than newton-cg for n_samples >> n_features. |
The short answer: start with lbfgs. If you need L1 regularization (which does feature selection), switch to liblinear for small datasets or saga for large ones.
If you see a ConvergenceWarning, don’t just increase max_iter blindly. First check: are your features scaled? Logistic regression converges much faster when features are on similar scales. Scaling is often all it takes.
Regularization in Logistic Regression
Here’s something no beginner tutorial tells you: when you call LogisticRegression() in scikit-learn without any arguments, regularization is already turned on. L2 regularization, specifically. You can see it in the default: penalty=’l2′, C=1.0.
This matters because regularization changes what your model learns. Without it, the model would fit the training data as tightly as possible, which usually means overfitting on high-dimensional datasets.
There are three types of regularization available:
L2 regularization (Ridge) adds a penalty proportional to the sum of squared weights. It shrinks all coefficients toward zero but doesn’t set any of them to exactly zero. Features stay in the model; they just get smaller weights. Use this when you think all your features are probably relevant and you just want to prevent overfitting.
L1 regularization (Lasso) adds a penalty proportional to the sum of absolute weights. It can shrink some weights all the way to zero: automatic feature selection. Features with zero weight are effectively removed from the model. Use this when you have many features and suspect most of them are irrelevant. The solver must be liblinear or saga.
Elastic Net combines L1 and L2. It selects features like L1 but handles correlated features more gracefully, distributing weight across a group rather than arbitrarily picking one. Requires solver=’saga’ and an additional l1_ratio parameter (0 = pure L2, 1 = pure L1).
The C parameter is the one everyone gets backwards.
C is the inverse of regularization strength. Smaller C means stronger regularization: more penalty, smaller coefficients, simpler model. Larger C means weaker regularization, so the model can fit training data more closely.
If you’re overfitting (training accuracy much higher than validation accuracy), try lowering C. If you’re underfitting (both metrics are low), try raising it.
A common workflow: cross-validate across a range like [0.001, 0.01, 0.1, 1, 10, 100] and pick the C that gives the best validation score.
python
from sklearn.model_selection import GridSearchCV
param_grid = {'model__C': [0.001, 0.01, 0.1, 1, 10, 100]}
grid_search = GridSearchCV(pipeline, param_grid, cv=5, scoring='roc_auc')
grid_search.fit(X_train, y_train)
print("Best C:", grid_search.best_params_)
Evaluating Your Logistic Regression Model
Accuracy is the metric everyone reports and the one you should trust least.
Here’s why. Imagine a fraud detection model where 99% of transactions are legitimate. A model that predicts “not fraud” for everything achieves 99% accuracy. It’s also completely useless. This is called the accuracy paradox, and it’s where a lot of beginner ML projects quietly go wrong.
The metrics that actually matter depend on what kind of mistakes cost more.
The Confusion Matrix — Read It Like a Business Decision
A confusion matrix breaks your predictions into four buckets:
| Predicted: Positive | Predicted: Negative | |
| Actually: Positive | True Positive (TP) | False Negative (FN) |
| Actually: Negative | False Positive (FP) | True Negative (TN) |
In plain language:
- True Positive: model correctly flagged a fraud case
- False Negative: actual fraud, but model said “legitimate” — a miss
- False Positive: legitimate transaction, but model flagged it as fraud — annoying but less damaging
- True Negative: legitimate transaction correctly cleared
The ratio of FN to FP costs depends on the problem. In fraud detection, a false negative (missed fraud) might cost $5,000. A false positive (blocking a real transaction) might cost one frustrated customer phone call. The business decision is asymmetric, and your threshold should reflect that — not default to 0.5.
The Metrics That Tell the Real Story
Precision = TP / (TP + FP). Of all the cases the model flagged as positive, how many actually were? High precision means few false alarms.
Recall (sensitivity) = TP / (TP + FN). Of all actual positive cases, how many did the model catch? High recall means few misses.
These two metrics pull against each other. Raising the threshold (making the model more conservative) increases precision but decreases recall. Lowering it does the opposite.
F1 score = 2 × (Precision × Recall) / (Precision + Recall). The harmonic mean of both. Useful when you want a single number that balances precision and recall. Less useful when one matters far more than the other. In that case, use precision@recall or just look at both separately.
AUC-ROC measures the model’s ability to rank positive cases above negative ones, across all possible thresholds. An AUC of 1.0 means perfect ranking. An AUC of 0.5 means the model is no better than random guessing. AUC is threshold-independent, which makes it a cleaner measure of model quality than any threshold-dependent metric.
For severely imbalanced datasets (like fraud, where positives might be 0.1% of all cases), AUC-ROC can still look good even on a bad model. In those cases, check AUC-PR (area under the precision-recall curve) instead, because it’s more sensitive to performance on the minority class.
The Decision Threshold — Why 0.5 Is Rarely the Right Answer
Every rulebook says you should use 0.5 as your cut-off point. In my opinion, this way of thinking is wrong…
It is important to understand that the choice of a cut-off point is a business decision rather than a characteristic of the model. Also, the cut-off point should depend on the cost of each type of error made.
Imagine that you build a model which helps diagnose cancer. The logistic regression predicts probabilities. When you make a false negative prediction (the patient has cancer but you predict it with a low probability), it means that you do not start treatment of the disease. On the other hand, when you make a false positive prediction, a person will have to suffer from a stressful procedure but will live. In this case, you should lower the cut-off point to something like 0.25 and predict cancer whenever you get the value which is larger than 0.25.
For the problem of fraud detection, when reviewing false positives is costly, the threshold can be increased up to 0.7 or even higher.
This is how you can play around the tradeoff in Python:
import matplotlib.pyplot as plt
from sklearn.metrics import precision_recall_curve
precision, recall, thresholds = precision_recall_curve(y_test, y_prob)
plt.plot(thresholds, precision[:-1], label='Precision')
plt.plot(thresholds, recall[:-1], label='Recall')
plt.xlabel('Threshold')
plt.ylabel('Score')
plt.title('Precision and Recall vs. Decision Threshold')
plt.legend()
plt.grid(True)
plt.show()
# Apply a custom threshold
custom_threshold = 0.3
y_pred_custom = (y_prob >= custom_threshold).astype(int)
print(classification_report(y_test, y_pred_custom))
Pick the threshold that gives you the best trade-off for your specific error costs. Not 0.5. The threshold that fits your problem.
Common Problems and How to Fix Them
These are the four things that go wrong most often, along with the specific fix for each.
| Problem | Symptom | Fix |
| Model won’t converge | ConvergenceWarning from sklearn | First, apply StandardScaler. If still failing, increase max_iter to 2000–5000. Last resort: switch solver (try saga). |
| Class imbalance | Accuracy looks great but recall is terrible on the minority class | Add class_weight=’balanced’ to LogisticRegression(). Or oversample the minority class with SMOTE before fitting. Always evaluate with recall and AUC-PR, not accuracy. |
| Features on different scales | Slow convergence, unstable coefficients | Apply StandardScaler (or MinMaxScaler) before fitting. Never skip this step — it’s not optional. |
| Multicollinearity | Coefficients change dramatically when you add/remove a related feature | Calculate VIF for each feature. Remove or combine features with VIF > 10. Alternatively, apply L1 or Elastic Net regularization to let the model handle it. |
A quick note on class imbalance: class_weight=’balanced’ tells sklearn to weight the minority class more heavily during training, effectively penalizing mistakes on the rare class more than mistakes on the common class. It’s one line of code and it often makes a large difference. Try it first before reaching for SMOTE or resampling techniques.
python
# Class imbalance -- one-line fix
model = LogisticRegression(class_weight='balanced', solver='lbfgs', max_iter=1000)
Real-World Use Cases of Logistic Regression
Credit Risk Scoring
Credit scoring involves using logistic regression to predict the chance of default of a borrower. Input variables are income level, debt-to-income ratio, years of credit history, and payment record.
Strangely enough, the reason why logistic regression has been dominant in credit scoring hasn’t been its accuracy but regulation. In many countries, lenders have to be able to explain the decision to the borrower. “Our gradient boosting model gave you a score of 412” is not an explanation. “Your chance of default was 34%, and that’s mainly because of your debt-to-income ratio and two late payments in the last year” is.
Medical Diagnosis and Clinical Research
Clinical researchers apply logistic regression to predict the likelihood of a diagnosis based on patients’ features. It is used in medical experiments all the time. Is the presence or absence of treatment a predictor for a binary result?
In this case, interpretability becomes important as well. A doctor needs to know the predictors that were used to make the diagnosis.
Email Spam Classification
One of the clearest examples. The characteristics involved are word frequencies (“free,” “click here,” “unsubscribe”), sender reputation, number of links, and attachments. The logistic regression assigns appropriate weight to each feature. If there is high weight assigned to the term “free” in the subject line, it increases the probability score.
Moreover, the model works well in conjunction. It can be seen that many spam filters use logistic regression as an initial filter and run complex models only for borderline cases.
Customer Churn Prediction
Logistic regression is used in subscription companies to forecast churn probabilities for customers who are about to cancel their services. Features such as days since last login, tickets opened in the previous 30 days, plan type, and activity level may be taken into consideration.
The probability value that this algorithm outputs becomes especially valuable in this case. For example, there would be different actions to take if the probability was 0.91 compared to 0.52. Chances are, the customer will get a phone call from an account manager in the first case but an automated email in the other.
When to Use Logistic Regression — and When to Choose Something Else
Logistic regression is always described by other papers as being “a basic algorithm,” but that is not very helpful. Instead, you should be aware of how and when to use this method.
Use logistic regression when:
- You require the output to be a probability rather than just an outcome – particularly in risk evaluation
- You work on a problem in a regulated industry where you should justify the process of prediction
- You want to get a quick, reliable starting point before moving to more sophisticated methods
- The data set is close to being linearly separable (or linearly separable itself)
- You have a small to medium data set that does not allow the high cost of training for deep learning models
- You expect your features to contribute mostly to the result (L2 regularization will be okay then)
Avoid logistic regression when:
- Your decision boundary is definitely non-linear and adding polynomial features has not helped
- Your data contains many complicated feature interactions which cannot be captured manually
- You require top predictive performance and interpretation is of no concern
- You have a very large number of features (embeddings, text) and have not done dimensionality reduction
How it measures up against other popular methods:
| Logistic Regression | Random Forest | XGBoost | Neural Network | |
| Interpretability | High (coefficients) | Medium (feature importance) | Medium (SHAP values needed) | Low |
| Training speed | Very fast | Moderate | Moderate | Slow |
| Predictive accuracy | Good baseline | Strong | Often best on tabular data | Excellent on large datasets |
| Handles non-linearity | No (needs feature engineering) | Yes | Yes | Yes |
| Works on small datasets | Yes | Yes | Yes | No |
| Calibrated probabilities | Yes (well-calibrated by default) | Needs calibration | Needs calibration | Needs calibration |
| Regulatory compliance | Easy | Hard | Hard | Very hard |
I consider the calibration row to be the most underrated thing in the table above. The probability estimates of logistic regression are actually well-calibrated: if there is 70 percent probability, then it really happens in 70 percent of cases. Random forests and gradient boosted models have a tendency to shift probabilities towards boundaries. In case when your decision depends on the value of the probability rather than its class, logistic regression wins.
Logistic Regression in Production ML Pipelines
A few words on what production really entails.
In a proper machine learning pipeline, you do not train your logistic regression model, test it, and deploy it. You version it, you watch it, and you retrain it if the distribution changes. What goes into a minimal production system?
python
import joblib
from sklearn.calibration import CalibratedClassifierCV
# Option 1: Use logistic regression directly (already well-calibrated)
pipeline.fit(X_train, y_train)
# Option 2: If you need better calibration for edge cases
calibrated_model = CalibratedClassifierCV(
LogisticRegression(solver='lbfgs', max_iter=1000),
method='isotonic',
cv=5
)
calibrated_model.fit(X_train, y_train)
# Save the trained pipeline
joblib.dump(pipeline, 'logistic_regression_model.pkl')
# Load it later for inference
loaded_model = joblib.load('logistic_regression_model.pkl')
new_prediction = loaded_model.predict_proba(X_new)[:, 1]
Two things worth knowing about the CalibratedClassifierCV class: first, you typically don’t need it with logistic regression because the probabilities are already good. Second, you do need it if you’re using logistic regression inside an ensemble where the raw probabilities feed into another model’s features.
The Pipeline object is worth using even in production. When you save the full pipeline with joblib.dump, you save the scaler and the model together. At inference time, you just call predict_proba on the loaded pipeline and it scales the features automatically.
One less thing to forget.
Frequently Asked Questions
Q1. Is logistic regression a classification or regression algorithm?
Ans. Classification. Even though it sounds like it, classification is really about predicting categories, namely, the likelihood that something belongs to a class. “Regression” is just an old name due to its origins in statistics. It is really more like “logistic classification.”
Q2. Does logistic regression require feature scaling?
Ans. Yes, absolutely. Logistic Regression makes use of gradient descent for training, and gradient descent converges much more quickly if all of the features are on a similar scale. Failure to scale leads to one feature, which has values between 0 and 1,000,000, overpowering others that lie between 0 and 1. Apply Standard Scaler before fitting. Always.
Q3. What does the C parameter in scikit-learn’s LogisticRegression mean?
Ans. C is the reciprocal of the strength of regularization. Small values of C imply a large regularization term; hence, the model becomes simpler. Large values of C imply a smaller regularization term; hence, the model fits the training data very well. The default value of C is 1.0. If you are overfitting, decrease C. If underfitting, increase C.
Q4. Why does my logistic regression model fail to converge?
Ans. Three likely causes, in order of how often they’re the culprit: (1) features aren’t scaled — apply StandardScaler; (2) max_iter is too low for your dataset size — increase to 1000 or 2000; (3) your data has severe multicollinearity or is nearly linearly separable, which can cause the optimization to struggle — try L1 regularization or remove correlated features.
Q5. Can logistic regression handle multiclass problems?
Ans. Yes. Pass multi_class=’multinomial’ (or let sklearn choose automatically based on the solver) and it switches from the sigmoid to the softmax function internally. The lbfgs, newton-cg, and saga solvers all support multiclass. liblinear only supports one-vs-rest.
Q6. When should I use logistic regression instead of a decision tree?
Ans. Where coefficient interpretation is necessary (“coefficient = income raises the probability of default by a factor of 1.4”). Where there is linearity between the features and the outcome. Where well-calibrated probabilities are needed out of the box. Decision trees are superior where interaction effects between the features are important and no feature engineering is necessary.
Q7. What’s the difference between logistic regression and a neural network?
Ans. Logistic Regression is a neural network; it is a single layer neural network that uses the sigmoid activation function. The difference between a normal neural network and logistic regression is that the former has a layer or more of hidden layers between input and output while the latter does not.
The Bottom Line
Every other logistic regression guide will teach you to call model.fit(X_train, y_train) and report accuracy. That’s the beginning of the story, not the end.
The bits that truly matter in application: realizing that regularization is enabled by default, setting your threshold using error costs rather than norms, that feature scaling must be done, and that a confusion matrix is as much a business report as anything.
If there’s just one thing you take from all of this — learning to read the confusion matrix should come first. Understand the types of errors being made by your algorithm, whether they are acceptable, before moving on to anything else.