Skip to content
BI & Data ScienceLogistic regression, and how to judge a classifier

Formulas for this chapter

Logit

Z = b0 + b1*x1 + b2*x2 + ... + bn*xn

The first column of every logistic-regression sheet. Unbounded, and not yet a probability.

b0
Intercept, the value of Z when every predictor is zero
bi
Change in Z per one unit of predictor i, all else equal
xi
The value of predictor i for this row

Sigmoid (logistic function)

P(y=1) = e^Z / (1 + e^Z) Excel: =EXP(Z)/(1+EXP(Z))

Immediately after Z. Turns the score into a probability. Z = 0 gives 0.5.

Z
The logit for this row
P(y=1)
Predicted probability that the row belongs to class 1

Likelihood and log-likelihood

Likelihood = IF(y = 1, P, 1 - P) Objective = SUM( LN(likelihood) ) -> maximise with GRG Nonlinear

The fitting objective. Solver changes only the coefficient cells; every other column recalculates.

y
The actual class of the row, 0 or 1
P
The row's predicted probability of class 1
SUM(LN)
Log-likelihood of the whole training set, always negative

Classification cut-off

predicted class = IF( P(y=1) >= 0.5, 1, 0 )

On the holdout rows, once the coefficients are frozen. Change the 0.5 to trade precision against recall.

0.5
The cut-off, a business decision rather than a statistical one

The five classification metrics

Accuracy = (TP + TN) / (TP + TN + FP + FN) Precision = TP / (TP + FP) Recall = TP / (TP + FN) (Sensitivity) Specificity = TN / (TN + FP) F1 = 2 x (Precision x Recall) / (Precision + Recall)

On the test rows only, from the four COUNTIFS cells. Choose which one to report from the cost of each error.

TP
Actual 1, predicted 1
FN
Actual 1, predicted 0. Type II error, loss of opportunity
FP
Actual 0, predicted 1. Type I error, loss of resources
TN
Actual 0, predicted 0
Step 1 of 30
The ideaTheory

A yes or no needs a probability

Ask a weather app whether it will rain and it does not say yes or no. It says 70 %.

That is the whole trick. The model never predicts the class directly. It predicts a probability between 0 and 1, and then a rule you choose turns that probability into a yes or a no.

Logistic regression is the machine for producing that probability. Everything else in this chapter is either how the probability is computed or how you check whether it was any good.