Brightline is an IT services company. Every time an employee leaves, hiring and training a replacement costs months of salary, and projects slow down.
HR has data on 320 employees from last year, including who left. They want to know which employees are likely to leave, and they want simple rules they can explain to managers, like "overtime and low satisfaction means high risk".
Goal: Predict left (Yes / No) for each employee, with rules people can read. A Decision Tree learns exactly that: a set of if-else questions.
320 employees, one row each.
| employee_id | age | department | years_at_company | monthly_income_k | overtime | last_hike_pct | satisfaction | left |
|---|---|---|---|---|---|---|---|---|
| 7001 | 34 | Support | 8 | 68 | No | 1 | 4 | No |
| 7002 | 47 | Sales | 2 | 47 | No | 6 | 4 | No |
| 7003 | 47 | HR | 18 | 117 | Yes | 6 | 4 | Yes |
| 7004 | 24 | Sales | 0 | 22 | No | 10 | 1 | Yes |
| 7005 | 27 | Engineering | 4 | 53 | No | 10 | 1 | No |
| 7006 | 36 | Engineering | 7 | 73 | No | 16 | 1 | No |
First 6 of 320 rows · realistic practice data
Press Run on each step in order, or run them all at once. You can change any code and run it again.
Starting Python…
Look at the data and how many employees left.
Before any model, compare the share of leavers across groups. A tree will find these splits by itself, but checking them first tells you what to expect.
A tree in scikit-learn needs numbers. overtime becomes 1 / 0. department has four values, so get_dummies makes one 0/1 column per department (this is called one-hot encoding).
Let the tree grow as deep as it wants and compare the score on the training data with the score on hidden data. A big gap means overfitting: the tree has memorised the training employees instead of learning general rules.
max_depth stops the tree after a few questions. Try depths from 1 to 10 and watch both lines. The best depth is where the test score is highest, before the lines split apart.
A depth-3 tree is small enough to read. Each box asks one question; follow the branches down to a leaf to see the prediction. The gini number says how mixed a box is (0 = everyone in it has the same answer).
export_text prints the same tree as if-else rules you can paste into an HR report. feature_importances_ says how much each column helped the tree split the employees.
A 26-year-old engineer, 2 years at the company, works overtime, got a 3% hike and rated satisfaction 2. Change the values and run again. The columns must match the training columns exactly.
20 questions on new data. You write the code; hints point the way without giving it.