Artificial Intelligence 8 min read

How to Interpret Logistic Regression Parameters and Odds Ratios with Python

This article explains the concepts of odds and odds ratios, shows how logistic regression estimates them, and demonstrates practical model fitting and prediction using Python's statsmodels and scikit‑learn libraries with real‑world examples.

Model Perspective
Model Perspective
Model Perspective
How to Interpret Logistic Regression Parameters and Odds Ratios with Python

Parameter Interpretation of Logistic Model

In epidemiology, researchers often need to compare the likelihood of a disease occurring versus not occurring; the odds measure is commonly used for this purpose.

Definition 1 : The odds of a random event is the ratio of its probability to the probability of it not occurring, denoted as odds.

If the probability of an event is p , then the probability of it not occurring is 1‑p , and the odds are p/(1‑p) . For example, an odds of 3 means the event occurs three times as often as it does not, i.e., 3 : 1.

When comparing two groups, the odds ratio (OR) is used. The odds ratio is the ratio of the odds for one group to the odds for another group. For instance, if drivers have odds O₁ of filing a claim and teachers have odds O₂ , the OR = O₁/O₂ indicates how many times larger the risk is for drivers.

Example: A real‑estate developer wants to estimate, for a household with an annual income of 90,000 CNY, the odds of finally buying a house after expressing interest, and compare it to a household with an income of 80,000 CNY.

By substituting the income values into the fitted logistic model, we obtain the odds ratio between the two income groups, showing how much more likely the higher‑income household is to purchase a house.

Solving with the statsmodels Library

Example: A financial institution evaluates loan applications. The outcome is 0 (bankrupt in two years, loan denied) or 1 (solvent in two years, loan approved). Given the first 20 companies' three evaluation metrics and outcomes, build a model to predict the next two companies.

Using maximum‑likelihood estimation, we fit a logistic regression model with the data. The resulting model achieves 100 % accuracy on the known data and correctly predicts that company 21 should be denied a loan while company 22 can receive a loan.

<code>import numpy as np
import statsmodels.api as sm

a = np.loadtxt("data/loan.txt")
n = a.shape[1]  # number of columns
x = a[:, :n-1]
y = a[:, n-1]
md = sm.Logit(y, x)
md = md.fit(method="bfgs")  # bfgs required; default Newton method fails
print(md.params, '\n----------\n')
print(md.summary2())
print(md.predict([[-49.2, -17.2, 0.3], [40.6, 26.4, 1.8]]))  # predictions for two new firms
</code>

Solving with the sklearn Library

Example: Use scikit‑learn to solve the same loan‑evaluation problem.

The fitted logistic regression model again yields 100 % accuracy on the training set and predicts the same outcomes for the two new companies.

<code>import numpy as np
from sklearn.linear_model import LogisticRegression

a = np.loadtxt("data/loan.txt")
n = a.shape[1]
 x = a[:, :n-1]
 y = a[:, n-1]
md = LogisticRegression(solver='lbfgs')
md = md.fit(x, y)
print(md.intercept_, md.coef_)
print(md.predict(x))  # model validation
print(md.predict([[-49.2, -17.2, 0.3], [40.6, 26.4, 1.8]]))  # predictions for two new firms
</code>

References

Si Shou‑kui, Sun Xi‑jing. Python Mathematics Experiments and Modeling .

machine learningPythonlogistic regressionscikit-learnstatsmodelsodds ratio
Model Perspective
Written by

Model Perspective

Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.