callmefair.mitigation.fair_bm
Bias Mitigation (BM) Manager Module
This module provides a comprehensive framework for applying various bias mitigation techniques in machine learning models. It supports preprocessing, in-processing, and postprocessing approaches to reduce algorithmic bias and promote fairness in AI systems.
The module implements multiple bias mitigation algorithms from the AIF360 library: - Preprocessing: Reweighing, Disparate Impact Remover (DIR), Learning Fair Representations (LFR) - In-processing: Adversarial Debiasing, MetaFair Classifier - Postprocessing: Calibrated Equalized Odds, Equalized Odds, Reject Option Classification
- Classes:
BMType: Enumeration of available bias mitigation techniques BMManager: Main class for applying bias mitigation methods to datasets
Example
>>> from callmefair.mitigation.fair_bm import BMManager, BMType
>>> from callmefair.util.fair_util import BMInterface
>>>
>>> # Initialize with your data
>>> bm_manager = BMManager(bm_interface, privileged_groups, unprivileged_groups)
>>>
>>> # Apply preprocessing bias mitigation
>>> bm_manager.pre_Reweighing()
>>>
>>> # Apply in-processing bias mitigation
>>> ad_model = bm_manager.in_AD(debias=True)
>>>
>>> # Apply postprocessing bias mitigation
>>> mitigated_predictions = bm_manager.pos_CEO(valid_pred, test_pred)
Attributes
Classes
Bias Mitigation Manager for applying various fairness techniques. |
|
Enumeration of bias mitigation techniques. |
Functions
|
Generate a random string of specified length. |
Module Contents
- class callmefair.mitigation.fair_bm.BMManager(bmI, privileged_group, unprivileged_group)[source]
Bias Mitigation Manager for applying various fairness techniques.
This class provides a unified interface for applying different bias mitigation techniques to machine learning datasets. It supports preprocessing, in-processing, and postprocessing approaches to reduce algorithmic bias.
The manager works with BinaryLabelDataset objects from the AIF360 library and provides methods for each type of bias mitigation technique.
- Variables:
bmI (BMInterface) – Interface for managing binary label datasets
privileged_group (list[dict]) – List of dictionaries defining privileged groups
unprivileged_group (list[dict]) – List of dictionaries defining unprivileged groups
- Parameters:
Example
>>> from callmefair.util.fair_util import BMInterface >>> from callmefair.mitigation.fair_bm import BMManager >>> >>> # Initialize with your data >>> bm_interface = BMInterface(train_df, val_df, test_df, 'label', ['protected_attr']) >>> privileged_groups = [{'protected_attr': 1}] >>> unprivileged_groups = [{'protected_attr': 0}] >>> >>> bm_manager = BMManager(bm_interface, privileged_groups, unprivileged_groups) >>> >>> # Apply preprocessing bias mitigation >>> bm_manager.pre_Reweighing()
Initialize the Bias Mitigation Manager.
- Parameters:
bmI (BMInterface) – Interface for managing binary label datasets
privileged_group (list[dict]) – List of dictionaries defining privileged groups. Each dict should contain protected attribute names and their privileged values.
unprivileged_group (list[dict]) – List of dictionaries defining unprivileged groups. Each dict should contain protected attribute names and their unprivileged values.
Example
>>> privileged_groups = [{'gender': 1, 'race': 1}] >>> unprivileged_groups = [{'gender': 0, 'race': 0}] >>> bm_manager = BMManager(bm_interface, privileged_groups, unprivileged_groups)
- in_AD(debias=False)[source]
Create an Adversarial Debiasing in-processing model.
Adversarial Debiasing is an in-processing technique that trains a classifier while simultaneously training an adversary that tries to predict the sensitive attribute from the classifier’s predictions. This encourages the classifier to make predictions that are independent of the sensitive attribute.
- Parameters:
debias (bool) – Whether to apply debiasing. If True, the model will be trained to be fair. If False, it will be a standard classifier. Defaults to False.
- Returns:
Configured adversarial debiasing model.
- Return type:
AdversarialDebiasing
- Raises:
ImportError – If TensorFlow is not available.
Example
>>> ad_model = bm_manager.in_AD(debias=True) >>> # Train the model with your data >>> ad_model.fit(train_data, train_labels)
- in_Meta(sensitive_attribute, tau=0)[source]
Create a MetaFair Classifier in-processing model.
MetaFair is an in-processing technique that uses a meta-learning approach to learn fair classifiers. It optimizes for both accuracy and fairness using a regularization term that penalizes unfairness.
- Parameters:
- Returns:
Configured MetaFair classifier.
- Return type:
MetaFairClassifier
Example
>>> meta_model = bm_manager.in_Meta('gender', tau=0.1) >>> # Train the model with your data >>> meta_model.fit(train_data, train_labels)
- pos_CEO(valid_BLD_pred, test_BLD_pred)[source]
Apply Calibrated Equalized Odds (CEO) postprocessing bias mitigation.
CEO is a postprocessing technique that adjusts model predictions to achieve equalized odds while maintaining calibration. It ensures that the true positive and false positive rates are equal across different groups.
- Parameters:
valid_BLD_pred (BinaryLabelDataset) – Validation dataset with model predictions
test_BLD_pred (BinaryLabelDataset) – Test dataset with model predictions
- Returns:
Postprocessed test predictions with equalized odds
- Return type:
BinaryLabelDataset
Example
>>> mitigated_predictions = bm_manager.pos_CEO(valid_pred, test_pred) >>> # The predictions now satisfy equalized odds
- pos_EO(valid_BLD_pred, test_BLD_pred)[source]
Apply Equalized Odds (EO) postprocessing bias mitigation.
EO is a postprocessing technique that adjusts model predictions to achieve equalized odds. It ensures that the true positive and false positive rates are equal across different groups, without considering calibration.
- Parameters:
valid_BLD_pred (BinaryLabelDataset) – Validation dataset with model predictions
test_BLD_pred (BinaryLabelDataset) – Test dataset with model predictions
- Returns:
Postprocessed test predictions with equalized odds
- Return type:
BinaryLabelDataset
Example
>>> mitigated_predictions = bm_manager.pos_EO(valid_pred, test_pred) >>> # The predictions now satisfy equalized odds
- pos_ROC(valid_BLD_pred, test_BLD_pred)[source]
Apply Reject Option Classification (ROC) postprocessing bias mitigation.
ROC is a postprocessing technique that rejects predictions for instances where the model is uncertain, particularly when this uncertainty is correlated with the sensitive attribute. This helps reduce bias by abstaining from making predictions on potentially unfair cases.
- Parameters:
valid_BLD_pred (BinaryLabelDataset) – Validation dataset with model predictions
test_BLD_pred (BinaryLabelDataset) – Test dataset with model predictions
- Returns:
Postprocessed test predictions with reject option
- Return type:
BinaryLabelDataset
Example
>>> mitigated_predictions = bm_manager.pos_ROC(valid_pred, test_pred) >>> # Some predictions may be rejected to improve fairness
- pre_DR(sensitive_attribute)[source]
Apply Disparate Impact Remover (DIR) preprocessing bias mitigation.
DIR is a preprocessing technique that repairs the training data to remove disparate impact while preserving the utility of the data. It works by learning a repair transformation that minimizes the difference in conditional distributions between groups.
- Parameters:
sensitive_attribute (str) – Name of the sensitive attribute to repair. This should be one of the protected attributes in your dataset.
- Return type:
None
Example
>>> bm_manager.pre_DR('gender') >>> # The training dataset is now repaired for the 'gender' attribute
- pre_LFR()[source]
Apply Learning Fair Representations (LFR) preprocessing bias mitigation.
LFR is a preprocessing technique that learns a fair representation of the data that removes information about the sensitive attributes while preserving the utility for the prediction task. It uses an adversarial approach to learn representations that are fair across different groups.
Example
>>> bm_manager.pre_LFR() >>> # The training dataset now has fair representations
- Return type:
None
- pre_Reweighing()[source]
Apply Reweighing preprocessing bias mitigation.
Reweighing is a preprocessing technique that assigns different weights to instances based on their group membership to achieve statistical parity. This method modifies the training dataset by reweighting instances.
The method fits the reweighing algorithm on the training data and transforms it to create a fairer training dataset.
Example
>>> bm_manager.pre_Reweighing() >>> # The training dataset is now reweighted for fairness
- Return type:
None
- class callmefair.mitigation.fair_bm.BMType(*args, **kwds)[source]
Bases:
enum.EnumEnumeration of bias mitigation techniques.
This enum categorizes bias mitigation methods into three main groups: - Preprocessing: Applied to training data before model training - In-processing: Applied during model training - Postprocessing: Applied to model predictions after training
- Variables:
preReweighing – Reweighing preprocessing technique
preDisparate – Disparate Impact Remover preprocessing technique
preLFR – Learning Fair Representations preprocessing technique
inAdversarial – Adversarial Debiasing in-processing technique
inMeta – MetaFair Classifier in-processing technique
posCalibrated – Calibrated Equalized Odds postprocessing technique
posEqqOds – Equalized Odds postprocessing technique
posROC – Reject Option Classification postprocessing technique
- property is_in: bool[source]
Check if the bias mitigation technique is an in-processing method.
- Returns:
True if the technique is in-processing, False otherwise.
- Return type:
- property is_pos: bool[source]
Check if the bias mitigation technique is a postprocessing method.
- Returns:
True if the technique is postprocessing, False otherwise.
- Return type:
- callmefair.mitigation.fair_bm.get_random_string(N=7)[source]
Generate a random string of specified length.
- Parameters:
N (int) – Length of the random string to generate. Defaults to 7.
- Returns:
Random string containing uppercase and lowercase letters.
- Return type:
Example
>>> get_random_string(5) 'AxK9m'