A bank wants to know whether a loan applicant will default. A hospital wants to know whether a tumour is malignant. Your email app wants to know whether a message is spam. In each case the answer is not a number on a scale. It is a choice between categories, and the useful output is a probability, such as an 85 percent chance of default. Logistic regression is the classic tool for this job.
Despite its name, logistic regression is a classification algorithm. It takes the same weighted sum of inputs that linear regression uses, then passes it through a curve called the sigmoid function, which squeezes any number into a value between 0 and 1. That value is read as a probability. Because it is fast, easy to explain and gives probabilities, it is often the first model a team tries, and the baseline everything else must beat.
In this tutorial, we explain what logistic regression is, why a straight line fails for classification, and how the sigmoid function fixes it. We cover the types of logistic regression, the real assumptions behind it, and then run a complete Python example with output from an actual run, including accuracy, a comparison against a simple baseline, and the effect of changing the decision threshold. We end with strengths, limits, common mistakes and a short FAQ.
Logistic regression is a supervised learning algorithm that predicts the probability that an input belongs to a category. The inputs, called independent variables or features, can be numbers or categories. The output, called the dependent variable, is a category, such as yes or no.
Some everyday examples:
The model first calculates a score from the inputs. It then passes that score through the sigmoid function, so the final output is a probability. A common rule is that a probability of 0.5 or more means the positive class, and anything lower means the negative class.
The outcome has exactly two values, such as 0 and 1, or true and false. A classic example is whether a Titanic passenger survived or not, based on age, ticket class and Gender.

The outcome has more than two categories that have no natural order. For example, the weather could be sunny, cloudy or overcast. The model gives one probability for each category, and the probabilities add up to 1. The original page calls this multi linear logistic regression, but the correct name is multinomial.
Here is a small illustrative table of the kind of data involved. The values are made up to show the layout.
| Temperature (C) | Humidity (%) | Wind (km per hour) | Outlook |
|---|---|---|---|
| 31 | 40 | 8 | Sunny |
| 24 | 78 | 15 | Cloudy |
| 20 | 92 | 22 | Overcast |
| 33 | 35 | 6 | Sunny |

A third type applies when the categories have a natural order, such as low, medium and high. Only this type depends on order.
| Type | Outcome | Example |
|---|---|---|
| Binary | Two categories | Fraud or not fraud |
| Multinomial | Three or more, no order | Sunny, cloudy, overcast |
| Ordinal | Three or more, with order | Low, medium, high risk |
Linear regression draws a straight line, and a straight line has no limits. It can predict 1.7 or minus 0.4, which make no sense as probabilities. A few extreme points can also tilt the line and shift every prediction. We need a curve that stays between 0 and 1.

The sigmoid function takes any real number and maps it to a value between 0 and 1. It is shaped like a stretched letter S. Large positive numbers go close to 1, large negative numbers go close to 0, and zero maps exactly to 0.5. The formula is 1 divided by 1 plus e raised to the power of minus z, where z is the weighted sum of the inputs.

| Score z | Probability | What it means |
|---|---|---|
| Minus 4 | 0.018 | Almost surely the negative class |
| Minus 2 | 0.119 | Probably negative |
| 0 | 0.500 | A coin toss |
| 2 | 0.881 | Probably positive |
| 4 | 0.982 | Almost surely positive |
I checked these values by running them. Notice how a change from 0 to 2 moves the probability a lot, while a change from 2 to 4 moves it very little. The curve flattens at the ends.
Note that the sigmoid limits the model output, which is the prediction. It does not limit the cost function, as the original page says.

Probability tells you how likely something is. Odds compare the chance of it happening with the chance of it not happening. If the probability is 0.75, the odds are 0.75 to 0.25, which is 3 to 1. The log odds are the natural logarithm of the odds.
Here is the key idea. Logistic regression assumes the log odds change in a straight line with the inputs. The sigmoid then converts the log odds back into a probability. That is what makes the model easy to interpret: each coefficient tells you how much the log odds change when that input rises by one unit.
These are the real assumptions, and a few things that people often confuse with them.
Two items from the original list are not assumptions. A sample size rule such as 50 observations per input is a rule of thumb. Handling missing values is a data preparation step, done before you fit the model.
This program uses the breast cancer dataset that comes with scikit learn, with 569 patients and 30 measurements each. The label is malignant or benign. The program checks the sigmoid values, trains a model, compares it with a baseline, shows a confusion matrix, tests the effect of the threshold and ends with a three class example on the Iris dataset.
When I ran the program, it printed:
The test set has 171 patients. A lazy model that always guesses benign would be right 62.6 percent of the time, so any accuracy must be judged against that number. The original page reports 78.35 percent with no baseline and no dataset name, so a reader cannot tell whether that is good.
| Predicted malignant | Predicted benign | |
|---|---|---|
| Actually malignant | 61 correct | 3 missed |
| Actually benign | 4 false alarms | 103 correct |
Out of 64 malignant cases, 61 were found and 3 were called benign. Out of 107 benign cases, 4 were flagged as malignant. The precision and recall for each class were between 0.94 and 0.97.
The 0.5 cut off is a default, not a law. Here the model’s probability is for the benign class, and I tried raising the bar to 0.8:
Here, a false positive means a malignant case called benign, which is the costly mistake. Raising the threshold to 0.8 cut those from 3 to 2, but false negatives, which are benign cases flagged as malignant, rose from 4 to 7. You always trade one kind of error for the other. Choose the threshold based on which mistake costs more in your business, not on the default.
With three flower species, the same model gives one probability for each, and they add up to 1. For the flower shown, the model is 96.4 percent sure of the third species. The test set has 45 flowers, so 0.978 means 44 correct.
| Setting | Default | What it does |
|---|---|---|
| C | 1.0 | Inverse of regularization strength. Smaller means a simpler model |
| penalty | l2 | The type of regularization that discourages large coefficients |
| solver | lbfgs | The method that finds the coefficients |
| max_iter | 100 | Maximum steps. Raise it if you see a convergence warning |
| class_weight | None | Use balanced when one class is rare |
Logistic regression is a small idea that goes a long way. A weighted sum, a sigmoid curve and a threshold give you a model that is fast, explainable and surprisingly hard to beat. Run the program on your own data, compare it with a baseline, and choose your threshold with your costs in mind. If the pattern is curved, try a random forest next.