# Introduction
Whether or not they’re well-established classifiers or state-of-the-art large fashions like giant language fashions (LLMs), constructing machine studying options usually entails a threat: algorithms would possibly silently undertake prejudices inherent within the historic coaching dataset they had been educated on. However in a high-stakes state of affairs or one the place knowledge is delicate, how can we audit whether or not a mannequin is biased with out compromising real-world info?
This hands-on article guides you in coaching a easy classification mannequin for “mortgage approval” on biased knowledge. Based mostly on this, we’ll use Mimesis, an open-source library that may assist generate a wonderfully balanced, counterfactual dataset. You can take a look at “pretend” customers with an identical monetary backgrounds however completely different demographic traits, thereby figuring out whether or not the mannequin discriminates towards sure teams or not.
# Step-by-Step Information
Begin by putting in the Mimesis library in case you are new to utilizing it, or you’re engaged on a cloud pocket book setting like Colab:
Earlier than auditing a mannequin, we truly have to get one! On this instance, we’ll synthetically generate a dataset of 1,000 financial institution clients, with simply two options: gender and revenue. These options are categorical and numerical, respectively. The information creation might be deliberately manipulated in order that the gender attribute unfairly influences the binary final result: mortgage approval. Particularly, for labeling the dataset, we’ll take into account a state of affairs wherein males are typically authorized, whereas girls are solely authorized once they have remarkably excessive revenue.
The method to create this clearly biased dataset and practice a choice tree classifier on it’s proven beneath:
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier
# 1. Simulating biased historic knowledge (1000 cases)
np.random.seed(42)
n_train = 1000
genders = np.random.selection(['Male', 'Female'], n_train)
incomes = np.random.randint(30000, 120000, n_train)
approvals = []
for gender, revenue in zip(genders, incomes):
if gender == 'Male':
# Traditionally, males are authorized
approvals.append(1)
else:
# Solely females with excessive revenue are authorized
approvals.append(1 if revenue > 80000 else 0)
train_df = pd.DataFrame({'Gender': genders, 'Revenue': incomes, 'Permitted': approvals})
# Changing classes to numbers for the machine studying mannequin
train_df['Gender_Code'] = train_df['Gender'].map({'Male': 1, 'Feminine': 0})
# 2. Coaching a Resolution Tree classifier
mannequin = DecisionTreeClassifier(max_depth=3)
mannequin.match(train_df[['Gender_Code', 'Income']], train_df['Approved'])
The subsequent step exhibits Mimesis in motion. We are going to use this library to generate a small set of take a look at topics utilizing the Generic class. This might be executed by defining three base monetary profiles that comprise random UUIDs (universally distinctive identifiers) and a average revenue ranging between 40K and 70K. Discover that these profiles is not going to have gender info included but:
from mimesis import Generic
generic = Generic('en')
# Producing 3 base monetary profiles
base_profiles = []
for _ in vary(3):
profile = {
'Applicant_ID': generic.cryptographic.uuid(),
'Revenue': generic.random.randint(40000, 70000) # Reasonable revenue
}
base_profiles.append(profile)
For instance, the three newly created profiles might look one thing like:
[{'Applicant_ID': '1f1721e1-19af-4bd1-8488-6abf01404ef9', 'Income': 44815},
{'Applicant_ID': '5c862597-7f55-43f4-9d6e-ac9cc0b9083e', 'Income': 47436},
{'Applicant_ID': '3479d4cf-0d9b-4f06-9c43-1c3b7e787830', 'Income': 58194}]
Let’s end constructing our counterfactual set of examples, which constitutes the core of our auditing course of! For every of the three base profiles, we’ll create two cloned counterfactual cases: one being male and the opposite being feminine. For every pair of take a look at clients, their software ID and revenue might be completely an identical, so the one distinction would be the gender: any distinction in how our educated resolution tree mannequin treats them will undoubtedly be proof of gender bias.
counterfactual_data = []
for profile in base_profiles:
# Model A: Male Counterfactual
counterfactual_data.append({
'Applicant_ID': profile['Applicant_ID'],
'Gender': 'Male',
'Gender_Code': 1,
'Revenue': profile['Income']
})
# Model B: Feminine Counterfactual
counterfactual_data.append({
'Applicant_ID': profile['Applicant_ID'],
'Gender': 'Feminine',
'Gender_Code': 0,
'Revenue': profile['Income']
})
audit_df = pd.DataFrame(counterfactual_data)
That is what the three pairs of consumers might appear like:
1f1721e1-19af-4bd1-8488-6abf01404ef9 Male 1 44815
1 1f1721e1-19af-4bd1-8488-6abf01404ef9 Feminine 0 44815
2 5c862597-7f55-43f4-9d6e-ac9cc0b9083e Male 1 47436
3 5c862597-7f55-43f4-9d6e-ac9cc0b9083e Feminine 0 47436
4 3479d4cf-0d9b-4f06-9c43-1c3b7e787830 Male 1 58194
5 3479d4cf-0d9b-4f06-9c43-1c3b7e787830 Feminine 0 58194
A key level to insist on right here: we’ve simply used Mimesis to immediately construct completely matched “clones” of mortgage candidates with an identical revenue however completely different genders. This underlines the library’s worth in offering whole statistical management, isolating a protected attribute.
Now it is time to probe the mannequin and see what it reveals.
# Asking the mannequin to foretell approval for our counterfactuals
audit_df['Predicted_Approval'] = mannequin.predict(audit_df[['Gender_Code', 'Income']])
# Formatting the output for readability (1 = Permitted, 0 = Denied)
audit_df['Predicted_Approval'] = audit_df['Predicted_Approval'].map({1: 'Permitted', 0: 'Denied'})
print("n--- Mannequin Audit Outcomes ---")
print(audit_df[['Applicant_ID', 'Gender', 'Income', 'Predicted_Approval']].sort_values('Applicant_ID'))
The choice-making outcomes yielded by our mannequin couldn’t be clearer:
--- Mannequin Audit Outcomes ---
Applicant_ID Gender Revenue Predicted_Approval
0 1f1721e1-19af-4bd1-8488-6abf01404ef9 Male 44815 Permitted
1 1f1721e1-19af-4bd1-8488-6abf01404ef9 Feminine 44815 Denied
4 3479d4cf-0d9b-4f06-9c43-1c3b7e787830 Male 58194 Permitted
5 3479d4cf-0d9b-4f06-9c43-1c3b7e787830 Feminine 58194 Denied
2 5c862597-7f55-43f4-9d6e-ac9cc0b9083e Male 47436 Permitted
3 5c862597-7f55-43f4-9d6e-ac9cc0b9083e Feminine 47436 Denied
Discover that for the very same Applicant_ID and Revenue, male clones are authorized for the mortgage. In the meantime, feminine clones with such average revenue are typically denied. The Mimesis functionalities we used based mostly on profiles helped us maintain all different variables fixed, thereby efficiently isolating and exposing the mannequin’s discriminatory decision-making.
# Wrapping Up
All through this hands-on article, we’ve proven how Mimesis can be utilized to generate balanced, counterfactual knowledge examples — with out privateness or delicate knowledge constraints — that may assist audit a mannequin’s conduct and establish whether or not the mannequin is behaving in a biased method or not. Subsequent steps to take in case your mannequin is biased might embody:
- Augmenting your coaching knowledge with extra balanced profiles to appropriate historic skewness or bias.
- Relying on the mannequin kind, utilizing mannequin re-weighting methods.
- Using open-source toolkits for equity — as an illustration, AI Equity 360 — that are useful for bias mitigation in machine studying pipelines.
Iván Palomares Carrascosa is a pacesetter, author, speaker, and adviser in AI, machine studying, deep studying & LLMs. He trains and guides others in harnessing AI in the true world.






