Logistic regression in Python
Logit models a probability; the coefficients are log odds.
import numpy as np
import statsmodels.api as sm
rng = np.random.default_rng(8)
practice = rng.uniform(0, 60, size=300)
logits = -3 + 0.09 * practice
passed = rng.binomial(1, 1 / (1 + np.exp(-logits)))
X = sm.add_constant(practice)
model = sm.Logit(passed, X).fit(disp=False)
print(model.params.round(4))
print("odds ratio per session:", round(float(np.exp(model.params[1])), 4))
print(round(model.prsquared, 4), round(model.llf, 2))
print(model.pred_table().astype(int))
How it works
np.exp(params)turns a coefficient into an odds ratio.predictreturns probabilities, not classes.pred_tableis the confusion matrix at a 0.5 threshold.
Keywords and builtins used here
asfloatintprintround
The run, in numbers
- Lines
- 15
- Characters to type
- 473
- Tokens
- 159
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 87 seconds.
Step 1 of 4 in Beyond least squares, step 9 of 19 in Statistics with statsmodels.