Logistic regression
A linear score passed through a sigmoid gives a probability. The loss is log loss, and the decision boundary is still a line.
From score to probability
Logistic regression computes a linear score, then squashes it through a sigmoid. The sigmoid is an S-shaped curve. It maps any real number into the range zero to one. The output can be read as a chance between zero and one. A score of zero becomes 0.5. Large positive scores approach one. Large negative scores approach zero.
The score is a log-odds. Odds compare the chance of an event with the chance against it. If the chance is p, the odds are p divided by one minus p. The log of that ratio is the linear part. So a weight of 0.7 means the log-odds rise by 0.7 for a one-unit rise in the feature. That multiplies the odds by about two. The intercept sets the baseline log-odds.
Log loss
The loss is the negative log likelihood of the observed labels. For a positive example it is minus the log of the predicted probability. For a negative one it is minus the log of one minus that probability. A confident correct prediction costs almost nothing. A confident wrong one costs a lot. That is the behaviour a probability model should have.
There is no closed form for the weights. The loss is convex, so gradient descent converges to the global optimum. The gradient has the same shape as in linear regression. It is the error times the feature, averaged over the rows. The difference is that the prediction has passed through the sigmoid. The error is therefore bounded between minus one and one.
Convexity is why the fit is repeatable. A convex loss has one bottom. Gradient descent from any reasonable start reaches the same weights, up to rounding error. That is not true for a neural net. There the same data and different seeds can land in different basins. Logistic regression is a good first classifier partly because its training is boring.
Probabilities from scikit-learn
from sklearn.linear_model import LogisticRegression
model = LogisticRegression().fit(X_train, y_train)
print(model.coef_, model.intercept_)
probabilities = model.predict_proba(X_test)[:, 1]
print(probabilities[:5])Fit a probability curve
Run the gradient steps, then raise the threshold to 0.7 and count how many predictions change without retraining the model.
Coefficients are log-odds
A positive weight means the feature raises the chance. Its size is the change in log-odds per unit. The effect on the chance is not constant. Near 0.5 the curve is steep. Near zero or one it flattens. So comparing weights across features with different scales needs care. Scaling the features makes the sizes more comparable.
Correlated features share their effect, as in linear regression. Regularisation, a penalty on large weights, is common and often on by default. An L2 penalty shrinks the weights toward zero. That stabilises the fit when columns are collinear. The penalty is a choice, not a neutral default. Tune it on validation data, not at whatever the library picked.
Regularisation also changes what the weights mean. An L2 penalty shrinks them toward zero. A weight is then no longer the pure best fit to the data. It is the effect under a budget. Comparing a penalised model with an unpenalised one by weight size is therefore wrong. Compare predictions, or refit without the penalty when you want to read the weights.
Report the probability, not only the label. A model that says 0.51 and one that says 0.99 make the same prediction at a 0.5 threshold, but they are not equally useful to a decision that has a cost.
The boundary is linear
The set of points where the score is zero is a line, a plane or a hyperplane. Everything on one side is predicted positive. Everything on the other is predicted negative, before any threshold other than 0.5. A linear boundary cannot represent an interaction, a joint effect of two features. An example is “positive when both features are high or both are low”. That is the XOR pattern.
The fix is to add features. Products, squares, splines or a learned encoding all widen the input. A logistic regression on built features is still a linear model in its weights. Its boundary in the original space can curve. A neural net does the same thing by learning the features. So the line between linear and nonlinear is about the boundary, not the name of the model.
Class weights are the other common fix. When positives are rare, the loss can weight a false negative more heavily. That shifts the boundary toward catching more positives. It changes the fitted model, not only the threshold. Check it on the same held-out set as any other choice. The goal is a rule that reflects the cost, not a model that looks balanced in the report.
Probabilities, odds and logits
Print the table, then add a weight of two to a feature and shift every logit by twice the feature value.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
What does the sigmoid do to a linear score?
