Ethical AI · Intermediate level

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

  1. Add a 'pred' column with the baseline model's test predictions.
  2. Compute actual and predicted pass rates with groupby(['county_type', 'gender'])[['passed', 'pred']].mean().
  3. Compute a confusion matrix for each group.
  4. Write which group is most under-predicted and what that would mean in practice.
Data and chart

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.

Test-set results for a fictional student-performance model, 680 students in four groups (illustrative data)
GroupStudentsActual pass rate (%)Predicted pass rate (%)
Urban girls1806866
Urban boys2007071
Rural girls1405238
Rural boys1605550
Actual vs predicted pass rate by group (%) Actual pass rate (%): Urban girls 68, Urban boys 70, Rural girls 52, Rural boys 55. Predicted pass rate (%): Urban girls 66, Urban boys 71, Rural girls 38, Rural boys 50 020406080Urban girlsUrban boysRural girlsRural boysActual pass rate (%)Predicted pass rate (%)

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

  1. 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.

  2. 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.