Logistic regression in Python predicts a yes or no outcome and, unlike most models, tells you in plain numbers how each input moves the odds. This guide fits one on 7,032 real telecom customers, reads the coefficients as odds ratios, and checks whether the predicted probabilities can be trusted.
The data is the IBM Telco Customer Churn sample: one row per customer with a yes or no churn label (26.6% churned). It is a published sample dataset, not a live company. Code was run with pandas 3.0.5, scikit-learn 1.9.1 and statsmodels 0.15. We keep to six inputs so every coefficient can be read.

Prepare the data
import pandas as pd, numpy as np
import statsmodels.api as sm
from sklearn.model_selection import train_test_split
url = "https://raw.githubusercontent.com/IBM/telco-customer-churn-on-icp4d/master/data/Telco-Customer-Churn.csv"
d = pd.read_csv(url)
d["TotalCharges"] = pd.to_numeric(d.TotalCharges, errors="coerce")
print("blank TotalCharges:", d.TotalCharges.isna().sum(), "tenure of those rows:", d[d.TotalCharges.isna()].tenure.unique())
d = d.dropna(subset=["TotalCharges"]).copy()
d["y"] = (d.Churn == "Yes").astype(int)
X = d[["tenure", "MonthlyCharges"]].copy()
X["month_to_month"] = (d.Contract == "Month-to-month").astype(int)
X["fiber"] = (d.InternetService == "Fiber optic").astype(int)
X["paperless"] = (d.PaperlessBilling == "Yes").astype(int)
X["senior"] = d.SeniorCitizen
Xtr, Xte, ytr, yte = train_test_split(X, d.y, test_size=0.25, stratify=d.y, random_state=0)
print(len(d), round(d.y.mean() * 100, 1), len(Xtr), len(Xte))Output:
blank TotalCharges: 11 tenure of those rows: [0]
7032 26.6 5274 1758Eleven rows have a blank TotalCharges, and all of them are customers with tenure 0 (brand new), so we drop them. The split is stratified; the reasons are in train test split in Python.
Logistic regression in Python: fit and read the odds ratios
Statsmodels prints the statistics a report needs. Exponentiating a coefficient gives an odds ratio: the factor by which the odds of churn are multiplied when that input rises by one unit.
m = sm.Logit(ytr, sm.add_constant(Xtr)).fit(disp=0)
out = pd.DataFrame({"coef": m.params, "odds_ratio": np.exp(m.params), "p": m.pvalues}).round(4)
print(out)
print(np.exp(m.conf_int()).round(2))
print("tenure odds ratio per 12 months:", round(float(np.exp(12 * m.params["tenure"])), 3))Output:
coef odds_ratio p
const -2.4549 0.0859 0.0
tenure -0.0370 0.9636 0.0
MonthlyCharges 0.0131 1.0131 0.0
month_to_month 1.1244 3.0783 0.0
fiber 0.7034 2.0206 0.0
paperless 0.4516 1.5708 0.0
senior 0.3968 1.4871 0.0
0 1
const 0.06 0.12
tenure 0.96 0.97
MonthlyCharges 1.01 1.02
month_to_month 2.46 3.85
fiber 1.55 2.64
paperless 1.33 1.85
senior 1.24 1.78
tenure odds ratio per 12 months: 0.641A month-to-month contract multiplies the odds of churn by 3.08 (95% interval 2.46 to 3.85) compared with a one or two year contract, holding the other inputs fixed. Fiber optic raises the odds by 2.02, paperless billing by 1.57 and being a senior citizen by 1.49. Each extra month of tenure multiplies the odds by 0.9636, which is 0.64 per year: longer customers are less likely to leave.
Odds are not probabilities. An odds ratio of 3 does not mean three times the chance. Raw churn rates make the size clear: 42.7% on month-to-month contracts, 11.3% on one year and 2.8% on two year. Also, the fiber coefficient is measured with MonthlyCharges in the model, and fiber plans cost more, so the two inputs share information. Read each coefficient as “other things equal”, not as a cause.
The same model in scikit-learn
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score, accuracy_score, confusion_matrix
lr = LogisticRegression(C=1e6, max_iter=5000).fit(Xtr, ytr)
print("max coefficient difference vs statsmodels:", round(np.abs(lr.coef_[0] - m.params.values[1:]).max(), 4))
default = LogisticRegression(max_iter=5000).fit(Xtr, ytr)
print({c: round(float(v), 4) for c, v in zip(Xtr.columns, default.coef_[0])})Output:
max coefficient difference vs statsmodels: 0.0002
{'tenure': -0.0372, 'MonthlyCharges': 0.0133, 'month_to_month': 1.1117, 'fiber': 0.6932, 'paperless': 0.4495, 'senior': 0.3956}scikit-learn applies a penalty by default (C=1). With the penalty effectively switched off (C=1e6) its coefficients match statsmodels to within 0.0002. With the default the coefficients move only a little here, for example month_to_month 1.1117 against 1.1244, because the data is large and the inputs few. Use scikit-learn when you want a prediction pipeline, statsmodels when you want the statistical table.
How good are the predictions?
p = m.predict(sm.add_constant(Xte))
print("test AUC:", round(roc_auc_score(yte, p), 3))
print("accuracy at 0.5:", round(accuracy_score(yte, p > 0.5), 3), "| always-no baseline:", round(1 - yte.mean(), 3))
print(confusion_matrix(yte, p > 0.5))Output:
test AUC: 0.835
accuracy at 0.5: 0.798 | always-no baseline: 0.734
[[1186 105]
[ 250 217]]Test ROC AUC is 0.835 on 1,758 held-out customers. Accuracy at the 0.5 cutoff is 79.8% against 73.4% for always predicting “no churn”. The matrix shows the cost: 250 of 467 churners are missed and 105 loyal customers are flagged. A threshold below 0.5 would find more churners, at the price of more false alarms.
Can you trust the probabilities?
Logistic regression outputs probabilities, so check them. Sort the test customers into ten equal groups by predicted risk and compare the average prediction with the actual churn rate.
bins = pd.qcut(p, 10, labels=False)
cal = pd.DataFrame({"predicted": p, "actual": yte.values, "decile": bins}).groupby("decile").mean().round(3)
print(cal)Output:
predicted actual
decile
0 0.014 0.006
1 0.030 0.028
2 0.057 0.040
3 0.089 0.114
4 0.150 0.182
5 0.232 0.278
6 0.313 0.303
7 0.409 0.381
8 0.559 0.568
9 0.700 0.756The agreement is close. The lowest-risk group has a predicted 1.4% against an actual 0.6%, the middle groups differ by a few points, and the highest group is predicted at 70.0% against an actual 75.6%. The model slightly under-predicts at the top end. With about 176 customers per group, differences of a few points are within noise, so this is a reasonable but not perfect calibration.
Use it on a customer
new = pd.DataFrame([{"const": 1, "tenure": 2, "MonthlyCharges": 80, "month_to_month": 1, "fiber": 1, "paperless": 1, "senior": 0}])
print("new customer:", round(float(m.predict(new)[0]), 3))
loyal = new.assign(tenure=60, month_to_month=0)
print("same customer, 60 months on a longer contract:", round(float(m.predict(loyal)[0]), 3))Output:
new customer: 0.689
same customer, 60 months on a longer contract: 0.077A two-month customer paying $80 on a monthly contract with fiber has a predicted churn probability of 68.9%. The same profile after 60 months on a longer contract is at 7.7%. These are model estimates for invented customers, not observed outcomes. To go further, try the customer churn prediction walkthrough, where tree models compete with this one, or compare against the random forest guide. If your table needs tidying first, see clean and preprocess data.
The official scikit-learn logistic regression docs and the statsmodels Logit reference list every option. Not tested here: interaction terms, regularisation tuning and multiclass models.
FAQ
What does logistic regression do in Python?
It models the probability of a yes or no outcome as a function of the inputs, and returns coefficients that can be turned into odds ratios.
How do I interpret logistic regression coefficients?
Exponentiate them. In this model the month-to-month coefficient 1.1244 becomes an odds ratio of 3.08: the odds of churn are about three times higher, other inputs fixed.
Should I use scikit-learn or statsmodels for logistic regression?
scikit-learn is built for prediction pipelines and regularisation. Statsmodels gives p-values and confidence intervals. With the penalty turned off their coefficients matched to 0.0002.
Does logistic regression need scaled features?
Not for statsmodels or for plain interpretation. scikit-learn’s default penalty is sensitive to scale, so scale inputs when you regularise.
