scikit-learn is a Python machine-learning library that fits logistic regression models when the outcome is a yes-or-no value. It handles coefficient fitting so you can focus on preparing data and interpreting predictions. section 8.3
A small scikit-learn classification workflow looks like this:
- Reshape one input variable into a single column with reshape(-1, 1). section 8.3
- Create LogisticRegression, optionally choosing a penalty such as None, l1, or l2. section 8.3
- Fit the model with fit(x, y), then inspect coef_ and intercept_ for the fitted parameters. section 8.3
- Use predict_proba to estimate the probability of a new outcome. section 8.3
Predicting a toddler meltdown
Given hours without a nap as x and meltdown status as y, fit LogisticRegression(penalty=None). For 4.78 hours without a nap, predict_proba returns a probability of about 0.9701. Treat that number as a model estimate, not an automatic truth machine. section 8.3
import numpy as np
from sklearn.linear_model import LogisticRegression
x = np.array([0.1, 0.5, 0.7, 1.2, 1.2, 1.5, 1.6, 1.9, 2.5, 2.5,
2.7, 3.2, 3.3, 3.7, 3.8, 4.1, 4.5, 4.5, 4.5, 5.0,
5.1, 5.6, 5.7, 6.2, 6.3]) \
.reshape(-1, 1) #1
y = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0]) #2
model = LogisticRegression(penalty=None) #3
model.fit(x, y) #3
b1 = model.coef_.flatten()[0] #4
b0 = model.intercept_[0] #4
print(f"β₀={b0:.4f}, β₁={b1:.4f}") #5
Browse the complete NumPy and scikit-learn listing for fitting the model and printing β₀ and β₁. section 8.3
# Make a prediction using formula
# Prints "Predicted probability of 4.78: 0.9701"
x_predict = 4.78
y_predict = 1.0 / (1.0 + np.exp(-(b0 + b1 * x_predict)))
print(f"Predicted probability of 4.78: {y_predict:.4f}")
# Or just use the model to make a prediction
# Prints "Predicted probability of 4.78: 0.9701"
y_predict = model.predict_proba([[x_predict]])
print(f"Predicted probability of 4.78: {y_predict[0][1]:.4f}")
Browse the comparison between calculating the logistic probability manually and calling predict_proba. section 8.3




