Intermediate level · Module 3: Identifying Bias in Datasets and AI Predictions · Lesson 2 of 3
Comparing Predicted and Actual Outcomes by Group
Learners compute predicted and actual pass rates and error rates for each group and identify where the model systematically under-predicts success.
In this lesson you will
- Compute predicted vs actual rates by group
- Identify systematic under- or over-prediction
Key concepts
predicted rateactual rateunder-predictionconfusion matrix by groupdisaggregated evaluation
The lesson
For each group, compare the actual pass rate with the rate the model predicts. When the predicted rate is far below the actual rate for one group, the model is under-predicting that group's success. If the model is used to decide who receives extra support or opportunities, this can be harmful.
A confusion matrix for each group shows where the errors are: false negatives (passers predicted to fail) and false positives (failers predicted to pass). Two groups can have similar accuracy but very different kinds of error.
In code, add the predictions to the test DataFrame, then group by the sensitive columns and compute the mean of the actual and predicted labels. Use sklearn.metrics.confusion_matrix on each group's subset.
Step by step
- Add a 'pred' column with the baseline model's test predictions.
- Compute actual and predicted pass rates with groupby(['county_type', 'gender'])[['passed', 'pred']].mean().
- Compute a confusion matrix for each group.
- Write which group is most under-predicted and what that would mean in practice.
Actual and predicted pass rates by group in a fictional school dataset
A pass/fail model looks accurate overall but under-predicts the success of rural girls far more than of any other group.
| Group | Students | Actual pass rate (%) | Predicted pass rate (%) |
|---|---|---|---|
| Urban girls | 180 | 68 | 66 |
| Urban boys | 200 | 70 | 71 |
| Rural girls | 140 | 52 | 38 |
| Rural boys | 160 | 55 | 50 |
What the data shows
For urban students the predicted pass rate is within 2 points of the actual rate, but for rural girls it is 14 points lower (38% predicted against 52% actual), and for rural boys 5 points lower.
If the model were used to choose who is entered for a scholarship examination, many rural girls who would pass would be left out. If it were used to offer extra tutoring, they would be wrongly flagged as at risk, which could lower teachers' expectations of them.
With 140 rural girls in the test set, a 14-point gap is unlikely to be random variation alone, but a bootstrap interval should be reported before drawing conclusions.
Recommendation
Investigate the inputs behind the gap (for example, attendance recorded differently in rural schools, or distance to school acting as a proxy), report true and false positive rates by group, and do not use the model for selection decisions until the gap is explained and reduced.
Practice activity
Create a by-group table of actual and predicted pass rates and confusion matrices for the project model.
Expected result: A correct table showing the largest gap and a clear explanation of its practical meaning.
Check your understanding
-
What does it mean if a group's predicted pass rate is far below its actual pass rate?
Show answer
The model under-predicts that group's success.
-
Can two groups have the same accuracy but different harms?
Show answer
Yes. Their errors can be different kinds (false positives vs false negatives).
Further learning
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.