Build This Project · Portfolio Guide
HR Attrition Analytics — Complete Project Guide
In the IBM HR Analytics attrition dataset, 237 of 1,470 employees left: an attrition rate of 16.1%. That one number is where most portfolio projects stop. This walk-through goes further: it finds where attrition is concentrated, builds a model that predicts who might leave, and shows the trap that catches many beginners when they read the model's output. Every number below comes from running the code on the dataset.
About the dataset
The data was created by IBM data scientists for teaching and is fictional; say this in your project. It has 1,470 rows (one per employee) and 35 columns, including age, department, job role, monthly income, overtime, satisfaction scores (1 to 4), years at the company and the target column Attrition (Yes/No). It is available on Kaggle as "IBM HR Analytics Employee Attrition & Performance" and in IBM's public GitHub repositories. Four columns carry no information (EmployeeCount, Over18 and StandardHours are constant, and EmployeeNumber is just an ID), so drop them before modelling.
Step 1: Where is attrition concentrated?
import pandas as pd
df = pd.read_csv("emp_attrition.csv", encoding="utf-8-sig")
df["left"] = (df["Attrition"] == "Yes").astype(int)
print("employees:", len(df), "| left:", df["left"].sum(),
"| attrition rate:", f"{df['left'].mean():.1%}")
def rate(col):
return (df.groupby(col)["left"].agg(employees="size", rate="mean")
.assign(rate=lambda t: (t["rate"] * 100).round(1))
.sort_values("rate", ascending=False))
print(rate("Department"))
print(rate("OverTime"))
df["tenure_band"] = pd.cut(df["YearsAtCompany"], bins=[-1, 1, 3, 5, 10, 40],
labels=["0-1", "2-3", "4-5", "6-10", "11+"])
print(rate("tenure_band").sort_index())Output:
employees: 1470 | left: 237 | attrition rate: 16.1%
employees rate
Department
Sales 446 20.6
Human Resources 63 19.0
Research & Development 961 13.8
employees rate
OverTime
Yes 416 30.5
No 1054 10.4
employees rate
tenure_band
0-1 215 34.9
2-3 255 18.4
4-5 306 13.1
6-10 448 12.3
11+ 246 8.1Three findings stand out:
- Overtime: employees who work overtime leave at about three times the rate of those who do not (30.5% vs 10.4%).
- Early tenure: more than a third of people in their first two years (0–1 years at the company) leave. Onboarding and the first manager relationship deserve attention.
- Department: Sales is highest, but Human Resources has only 63 people, so its 19.0% rests on 12 leavers. Always show the group size next to a rate.
The encoding="utf-8-sig" argument matters for some copies of this file, which start with a byte-order mark; without it the first column can be read as Age.
Step 2: KPIs for an HR dashboard
| KPI | Formula | Note |
|---|---|---|
| Attrition rate | Leavers in period / average headcount in period | This dataset is a snapshot, so we use leavers / total rows |
| Early attrition | Leavers with under 1 year of tenure / joiners in that year | Points to hiring and onboarding issues |
| Voluntary vs involuntary split | Resignations vs terminations | Not in this dataset; real HR systems record it |
| Attrition by segment | Rate by department, role, overtime, manager, location | Show headcount beside every rate |
| Regretted attrition | Leavers rated high performers / all leavers | Often the number leaders care about most |
Step 3: A model that flags who may leave
Logistic regression is a good first model here: it is fast, and its output is a probability that HR can rank. Scaling the features puts coefficients on a comparable scale, and class_weight="balanced" compensates for the fact that only 16% of rows are leavers.
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.metrics import roc_auc_score, recall_score, precision_score
df = pd.read_csv("emp_attrition.csv", encoding="utf-8-sig")
y = (df["Attrition"] == "Yes").astype(int)
X = df.drop(columns=["Attrition", "EmployeeCount", "EmployeeNumber",
"Over18", "StandardHours"])
X = pd.get_dummies(X, drop_first=True, dtype=int)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.25,
stratify=y, random_state=1)
model = make_pipeline(StandardScaler(),
LogisticRegression(max_iter=2000, class_weight="balanced"))
model.fit(X_tr, y_tr)
proba = model.predict_proba(X_te)[:, 1]
pred = (proba >= 0.5).astype(int)
print(f"ROC AUC: {roc_auc_score(y_te, proba):.3f}")
print(f"recall (leavers caught): {recall_score(y_te, pred):.2f}")
print(f"precision: {precision_score(y_te, pred):.2f}")
coef = pd.Series(model[-1].coef_[0], index=X.columns).sort_values()
print("pushes towards leaving:\n", coef.tail(4).round(2).to_string())
print("pushes towards staying:\n", coef.head(4).round(2).to_string())Output (scikit-learn 1.9.1):
ROC AUC: 0.826
recall (leavers caught): 0.75
precision: 0.33
pushes towards leaving:
OverTime_Yes 0.70
MaritalStatus_Single 0.76
Department_Sales 0.86
YearsAtCompany 0.89
pushes towards staying:
YearsWithCurrManager -0.58
YearsInCurrentRole -0.57
EducationField_Life Sciences -0.56
EnvironmentSatisfaction -0.56The model catches 75% of leavers in the test set, and one in three of the people it flags actually left. For a retention team, that is a useful shortlist for conversations, not a list of people to treat differently.
The trap: reading coefficients as causes
Look at YearsAtCompany. In Step 1, longer tenure clearly meant lower attrition. Yet in the model its coefficient is the largest positive one. Both are true. Tenure moves together with YearsInCurrentRole, YearsWithCurrManager and TotalWorkingYears. With all of them in the model, each coefficient means "the effect of this feature while the others are held fixed", and that can flip sign. Read this way, more years at the company without a change of role or manager is associated with leaving, which hints at stagnation. It is a hypothesis to test, not a finding to present as fact.
Similarly, EducationField_Life Sciences is negative only relative to the dropped reference category (Human Resources as a field of education). Dummy coefficients are always comparisons with a baseline. In your write-up, base claims on the simple group rates from Step 1 and use the model for ranking.
Step 4: Dashboard and recommendations
- Page 1: headcount, attrition rate, early attrition and overtime share as KPI cards; attrition by department and tenure band as bars with headcounts.
- Page 2: a drill-down by job role and manager, with a filter for overtime.
- Page 3: the model's ranked risk list with the main contributing features, visible only to authorised HR users.
Turn findings into actions you can defend: review overtime load in Sales, strengthen first-year onboarding and check-ins, and look at role movement for employees who have stayed in one role for many years. State clearly that these are suggestions from a fictional dataset.
For a similar project with a different business problem, see the insurance claims analytics project, the healthcare analytics dashboard or the sales analytics dashboard. For portfolio presentation, read how to build a job-ready data analytics portfolio.
FAQs
Is the IBM HR attrition dataset real?
No. It is a fictional dataset created by IBM for teaching. It is fine for a portfolio project if you say so.
Should I use accuracy to evaluate an attrition model?
No. With 16% leavers, predicting "stays" for everyone gives about 84% accuracy. Use recall, precision and ROC AUC, and report how many people the model flags.
Can I use age or marital status as features?
Technically yes, but think about fairness and law before acting on them. Many teams exclude personal attributes from decisions and use them only to check that the model treats groups fairly.