Back to notes
sklearn · ~1 month
Logistic regression vs a forest on one split
On sklearn’s breast-cancer set, how do logistic regression and a random forest compare under a fixed stratified 80/20 split?
- Data
- sklearn.datasets.load_breast_cancer — 569 rows × 30 features, malignant vs benign.
- Setup
- Stratified 80/20 split, random_state=42, same 114-row test set for both. LogisticRegression(max_iter=1000) vs RandomForestClassifier(n_estimators=100). Accuracy and macro-F1.
- Approach
- pandas / numpy for the table
- sklearn for the split, both models, accuracy, and macro-F1
- matplotlib / seaborn for the confusion matrices
- Numbers
- Logistic regression96.5% accuracy · 0.962 macro-F1Random forest95.6% accuracy · 0.953 macro-F1
- Figures

Logistic regression on the 114-row test set: 40 / 2 / 2 / 70. Four mistakes. 
Random forest on the same test set: 39 / 3 / 2 / 70. Five mistakes, one more false positive than logistic regression. - What I take from it
- Logistic regression won this one. The table’s clean enough that the forest had nothing extra to find.
- Reproduce
pip install -r requirements.txt && python scripts/train.py
The other note: A PyTorch train/val loop on toy moons