Audit model bias with balanced datasets using Mimesis

Machine Learning


Audit model bias with balanced datasets using Mimesis

# introduction

Building machine learning solutions, whether they’re established classifiers or cutting-edge large-scale models like large-scale language models (LLMs), often involves risk. Algorithms can silently adopt biases inherent in the historical training datasets used for training. But what do you do in high-stakes or data-sensitive scenarios? Audit whether the model is biased Without compromising real-world information?

This practical article shows how to train a simple classification model for “loan approval” based on biased data. Based on this, use: mimesisan open-source library that helps you generate something completely balanced. counterfactual Dataset. It will now be possible to test “fake” users with the same economic background but different demographic characteristics, thereby determining whether the model discriminates against certain groups.

# step by step guide

If you’re using the Mimesis library for the first time, or working in a cloud notebook environment like Colab, start by installing the Mimesis library.

Before you can audit a model, you need to actually obtain it. This example synthetically generates a dataset of 1,000 bank customers with only two characteristics: gender and income. These features are categorical and numerical features, respectively. Data production is intentionally manipulated to ensure that gender attributes have a disproportionate influence on the dichotomous outcome of loan approval. Specifically, we consider a scenario in which men are usually approved for labeling a dataset, and women are only approved if they have significantly higher incomes.

Below is the process of creating this clearly biased dataset and training a decision tree classifier on it.

import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier

# 1. Simulating biased historical data (1000 instances)
np.random.seed(42)
n_train = 1000
genders = np.random.choice(['Male', 'Female'], n_train)
incomes = np.random.randint(30000, 120000, n_train)

approvals = []
for gender, income in zip(genders, incomes):
    if gender == 'Male':
        # Historically, males are approved
        approvals.append(1)
    else:
        # Only females with high income are approved
        approvals.append(1 if income > 80000 else 0)

train_df = pd.DataFrame({'Gender': genders, 'Income': incomes, 'Approved': approvals})

# Converting categories to numbers for the machine learning model
train_df['Gender_Code'] = train_df['Gender'].map({'Male': 1, 'Female': 0})

# 2. Training a Decision Tree classifier
model = DecisionTreeClassifier(max_depth=3)
model.fit(train_df[['Gender_Code', 'Income']], train_df['Approved'])

The next step shows mimesis in action. Use this library to generate a small set of things to test. Generic class. This is done by defining three basic financial profiles that include a random UUID (Universally Unique Identifier) ​​and a moderate income ranging from 40,000 to 70,000. Note that these profiles do not yet incorporate gender information.

from mimesis import Generic

generic = Generic('en')

# Generating 3 base financial profiles
base_profiles = []
for _ in range(3):
    profile = {
        'Applicant_ID': generic.cryptographic.uuid(),
        'Income': generic.random.randint(40000, 70000) # Moderate income
    }
    base_profiles.append(profile)

For example, the three newly created profiles look like this:

[{'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 complete the construction of a set of counterfactual examples that will form the core of the audit process. For each of the three base profiles, we create two replicated counterfactual instances, one male and one female. For each pair of test customers, the application ID and income will be exactly the same, the only difference being their gender. Any difference in how a trained decision tree model treats a customer is definitely evidence of gender bias.

counterfactual_data = []

for profile in base_profiles:
    # Version A: Male Counterfactual
    counterfactual_data.append({
        'Applicant_ID': profile['Applicant_ID'], 
        'Gender': 'Male', 
        'Gender_Code': 1, 
        'Income': profile['Income']
    })
    
    # Version B: Female Counterfactual
    counterfactual_data.append({
        'Applicant_ID': profile['Applicant_ID'], 
        'Gender': 'Female', 
        'Gender_Code': 0, 
        'Income': profile['Income']
    })

audit_df = pd.DataFrame(counterfactual_data)

The three sets of customers are:

1f1721e1-19af-4bd1-8488-6abf01404ef9	Male	1	44815
1	1f1721e1-19af-4bd1-8488-6abf01404ef9	Female	0	44815
2	5c862597-7f55-43f4-9d6e-ac9cc0b9083e	Male	1	47436
3	5c862597-7f55-43f4-9d6e-ac9cc0b9083e	Female	0	47436
4	3479d4cf-0d9b-4f06-9c43-1c3b7e787830	Male	1	58194
5	3479d4cf-0d9b-4f06-9c43-1c3b7e787830	Female	0	58194

The important point to make here is: We used Mimesis to instantly build perfectly matched “clones” of loan applicants with the same income but different gender. This highlights the value of libraries that isolate protected attributes and provide comprehensive statistical control.

Next, examine the model and see what it reveals.

# Asking the model to predict approval for our counterfactuals
audit_df['Predicted_Approval'] = model.predict(audit_df[['Gender_Code', 'Income']])

# Formatting the output for readability (1 = Approved, 0 = Denied)
audit_df['Predicted_Approval'] = audit_df['Predicted_Approval'].map({1: 'Approved', 0: 'Denied'})

print("\n--- Model Audit Results ---")
print(audit_df[['Applicant_ID', 'Gender', 'Income', 'Predicted_Approval']].sort_values('Applicant_ID'))

The decision results obtained by our model could not be more clear.

--- Model Audit Results ---
                           Applicant_ID  Gender  Income Predicted_Approval
0  1f1721e1-19af-4bd1-8488-6abf01404ef9    Male   44815           Approved
1  1f1721e1-19af-4bd1-8488-6abf01404ef9  Female   44815             Denied
4  3479d4cf-0d9b-4f06-9c43-1c3b7e787830    Male   58194           Approved
5  3479d4cf-0d9b-4f06-9c43-1c3b7e787830  Female   58194             Denied
2  5c862597-7f55-43f4-9d6e-ac9cc0b9083e    Male   47436           Approved
3  5c862597-7f55-43f4-9d6e-ac9cc0b9083e  Female   47436             Denied

Please note that the exact same Applicant_ID and Incomemale clones are approved for loans. On the other hand, female clones with such moderate incomes are generally rejected. The mimesis function we used based on the profile helped us hold all other variables constant, thereby successfully isolating and revealing the model’s discriminatory decision-making.

# summary

Throughout this practical article, we have shown how to use Mimesis to generate balanced counterfactual data examples (without privacy or sensitive data constraints). This helps you audit the behavior of your model and identify if it is behaving in a biased manner. Next steps to take if your model is biased include:

  • Enrich your training data with a more balanced profile to correct historical skews and biases.
  • Depending on the model type, use a model reweighting strategy.
  • Utilize open source toolkits for fairness — e.g. AI Fairness 360 — This helps reduce bias in machine learning pipelines.

Ivan Palomares Carrascosa I am a leader, writer, speaker, and advisor in AI, machine learning, deep learning, and LLM. He trains and coaches others to leverage AI in the real world.



Source link