Sub topic 3
Sensitive Attributes and Defining Groups
This chapter assumes you know the confusion matrix, precision, recall, and false positive rate. It also assumes basic pandas. Every fairness metric in this tutorial compares groups, so you must first decide what a group is and where its labels come from.
What a sensitive attribute is
A sensitive attribute is a characteristic of a person that you want to check for unequal treatment. Common examples are sex, race, age, disability, religion, and nationality. Many of these are protected by law in areas such as credit, hiring, housing, and education. The legal list depends on the country and the domain, so check which rules apply to your deployment.
The choice of attributes follows from the harms you identified earlier. Ask who could be hurt by this system, and which characteristics separate those people from everyone else.
Where group labels come from
You rarely get clean labels. Typical sources are:
- Self-reported fields on a form. These are the most reliable, but people may skip them or choose not to disclose.
- Administrative records, such as a date of birth. These are accurate but may be incomplete.
- Inferred values, such as a guessed ethnicity from a surname and postcode. These contain errors, and the errors may differ by group.
- Collected audit samples. You may survey or label a small sample just for testing.
Each source has a cost. Inferred labels can make a fairness audit look better or worse than reality. If you use them, report that they are estimates.
Using the attribute in two different ways
Training a model on a sensitive attribute and auditing a model with it are separate decisions.
- As a model input. Including it may be illegal or inappropriate in some domains.
- As an audit variable. You keep it out of the features and use it only to split the test results into groups.
Dropping the column from the features does not make a model fair. Other features act as proxies, meaning they correlate with the attribute. Postcode can stand in for race, and job title can stand in for sex. So you often need the attribute for the audit even when the model never sees it.
Defining groups in code
Keep the sensitive attribute in a separate array or column, aligned row by row with the labels and predictions. Check it before you compute any metric.
import pandas as pd
df = pd.DataFrame({
"y_true": [1, 0, 1, 1, 0, 0, 1, 0, 1, 0],
"y_pred": [1, 0, 0, 1, 0, 1, 1, 0, 0, 0],
"sex": ["F", "F", "F", "M", "M", "M", None, "F", "M", "F"],
})
print(df["sex"].value_counts(dropna=False))
missing = df["sex"].isna().mean()
print(f"Share with missing group label: {missing:.0%}")
df["sex"] = df["sex"].fillna("unknown")
print(df.groupby("sex").size())The output shows each group's size and how many labels are missing. Keeping an explicit "unknown" group is better than dropping those rows. Dropping them silently hides the people the system may serve worst.
Choices that change your results
- Granularity. "Asian" versus a list of specific national origins can give different gaps. A coarse category can hide a problem inside it.
- Binning continuous attributes. For age you must choose cut points, such as under 25, 25 to 59, and 60 or more. Different cut points can produce different conclusions.
- Reference group. Gaps are often reported against one group. Name it, and say why you chose it.
- Group size. Small groups give noisy rates. A later chapter covers how to judge this.
The diagram shows how the pieces connect.
Common errors
- Misaligned rows, so a group label does not match its prediction.
- Treating missing labels as a group that does not matter.
- Changing group definitions after seeing the results.
- Recording the definitions nowhere, so the audit cannot be repeated.
Exercise
A lending model is trained without a column for race. A colleague says an audit by race is therefore unnecessary. Give two reasons why they are wrong.
Show answer
First, other features such as postcode can act as proxies for race, so the model's outcomes can still differ by race. Second, you need the race attribute in the audit data to measure any gap at all. Keeping it out of the features does not stop you from using it to split the test results.