HumanForYou
predicting employee attrition
A textbook attrition case: bring together the HR data of a fictional company, derive indicators from it, then compare models under identical conditions.
- 28
training runs compared
With and without rebalancing, on two pipelines, all scored on the same test set.
- 5
sources merged
HR records, two satisfaction surveys, badge-in and badge-out logs, joined per employee.
- 71%
of leavers caught
3 times more leavers caught thanks to rebalancing: 22% without it, with logistic regression.
ChapterProblemContents
Problem
Who will leave, and why
HumanForYou is a fictional pharmaceutical company with 4,000 employees that considers its attrition too high. Management wants two things: to know who is likely to leave, and which levers to pull.
These are two different questions. A model that predicts well but cannot be read tells nobody what to do. It is the direct equivalent of a churn problem, applied to employees.
Constraints
A rare class, a raw log
- Sources that look nothing alike. HR records, satisfaction surveys and time logs, to be joined on a single identifier.
- A log that is useless as is. One column per working day, 261 working days over the year, for arrivals as for departures: it has to be summarized per employee.
- A rare class. 16.12% of employees leave. A model answering "nobody leaves" would be right more than eight times out of ten, and useless.
- A test set that must learn nothing. No statistic from the test set may enter data preparation.
Pipeline
From raw log to model choice
Two pipelines, identical except for one point: when the test set is set aside. Follow the one that was kept.
The test set is set aside before any preparation.
The pipeline kept.
Sourcespandas
HR data, two satisfaction surveys and a year of badge-in and badge-out logs.
Indicatorspandas
Attendance hours summarized as indicators, then the tables joined on the employee ID.
Splitscikit-learn
The test set is set aside first. Audit done afterwards: the same employee, in several copies, lands on both sides.
Example written for the illustration.
Preparescikit-learn
Medians and modes are computed on the training set alone, then applied unchanged to the test set.
Example written for the illustration.
Preparescikit-learn
Medians and modes are computed on the whole dataset: the test set weighs on its own preparation.
Example written for the illustration.
Splitscikit-learn
The test set is set aside afterwards. Same audit: the same employee lands on both sides.
Example written for the illustration.
RebalanceSMOTE, imbalanced-learn
Synthetic leavers are created between two real leavers, in the training set only; the test set keeps its proportion.
Example written for the illustration.
Trainscikit-learn, XGBoost, LightGBM
Seven algorithms, each with and without rebalancing, scored on the same test set.
Decisions
Recall first, and its price
| Decision | Option ruled out | What settled it |
|---|---|---|
| Recall first | Accuracy | A missed leaver costs more than a false alarm. Accuracy, on the other hand, rewards the model that flags nobody. |
| SMOTE, on the training set only | Duplicating leavers; dropping stayers | Duplicating pushes toward overfitting, dropping loses information. The test set keeps the real attrition rate. |
| Two pipelines kept side by side | Fixing the first one silently | Keeping both makes it possible to measure what the precaution changes, instead of asserting it: an average gap of 0.96 F1 points, in favor of the kept pipeline. |
| Seven algorithms scored on the same test set | A model chosen in advance | From the most readable, logistic regression, to gradient boosting: the choice is defended by comparison. |
| XGBoost, as kept by the notebook | Random forest, recommended by the documentation | Best F1 of the seven on the kept pipeline. The random forest, easier to explain to HR, is the model that was fine-tuned. |
Results
What the comparison showed
Notebook scores, on its test set. How far they can be trusted is discussed in the next chapter.
Rebalancing shifts the trade-off between recall and precision; it does not remove it. The chart shows it on logistic regression. It is the model least exposed to the flaw described in the next chapter: a linear model cannot memorize an individual.
Leavers caught: 71% (With SMOTE) vs. 22% (Without SMOTE), 3.3 times more, among the test set's leavers.
Correct alerts: 37% (With SMOTE) vs. 54% (Without SMOTE), 1.5 times fewer, the price of rebalancing.
F1 score: 49% (With SMOTE) vs. 31% (Without SMOTE), 1.6 times higher, the balance of both.
- The test set. 882 employees, 142 of them leavers, held out at their natural proportion. Stratified 80/20 split, same seed for both pipelines.
- Rebalancing. SMOTE on the training set only: the 569 leavers brought up to the 2,959 stayers.
- The statistical tests. Chi-squared on categorical variables, Student's t-test on continuous ones, before any modeling.
- Tuning. Random forest, 216 combinations tested by five-fold cross-validation on the training set.
- The limitation. The split is random, row by row. The next chapter explains what that changes.
Limitations and lessons learned
Count the people before splitting
After the project, I counted the people again. The file holds 4,410 rows, numbered as if each were an employee. It only describes 1,573 people: most appear three times, identical on every HR attribute and both surveys, target included. The replication comes from the original public dataset.
Our random split therefore scattered the copies. On average, 93.42% of test rows described someone already seen in training. The kept pipeline protected data preparation, not that boundary. The notebook's scores are inflated.
A control model measures the gap. Same HR and survey attributes, same random forest, same seed. Only the split changes: 0.9996 AUC with a random split, 0.7332 AUC once each person is kept on one side. Faced with a leaver and a stayer, the model picks the right one seven times out of ten. The diagnosis can be rerun with a single command.
AUC of the control model: 0.7332. The probability that the model ranks a leaver above a stayer. Higher is better. On a scale from 0.5 to 1: people kept apart, 0.7332; random split, 0.9996.
The dataset is fictional, derived from a public case: nothing carries over as is to a real HR department.
If I started again
- Split off the test set before any transformation, and by person. The test set must stay what the model will see in production: data no computation has touched, and people it has never seen.
- Read logistic regression as a detector. A linear model cannot memorize an individual. The huge gap between it and the tree-based models was the symptom, visible from the very first results table.
- Price a missed departure with the business. The whole recall versus precision trade-off rests on that cost, assumed and never measured.