SO i have this code and the first time it worked just fine, the second time i tried to run the code i got an error and i couldn’t fix the error, the next day i tried it and it literally worked just fine the first time I tried to run it again and now it’s showing the same error! i tired to restart the run time and creating a new notebook but no. this is the code
pip install aif360
!pip install 'aif360[Reductions]'
!pip install 'aif360[inFairness]'
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from aif360.datasets import BinaryLabelDataset
from aif360.algorithms.preprocessing import Reweighing
from aif360.metrics import ClassificationMetric
from aif360.algorithms.postprocessing import CalibratedEqOddsPostprocessing
from aif360.algorithms.inprocessing import PrejudiceRemover
np.random.seed(42)
def generate_controlled_applicants(n_samples=1000):
return pd.DataFrame({
'Gender': np.random.choice(['Male', 'Female'], n_samples),
'Race': np.random.choice(['White', 'Non-White'], n_samples),
'Age': np.full(n_samples, 35),
'Experience': np.full(n_samples, 10),
'Education': np.full(n_samples, 1)
})
# Generate controlled applicant data
controlled_applicants = generate_controlled_applicants()
# Encode categorical variables
controlled_applicants['Gender'] = controlled_applicants['Gender'].map({'Male': 1, 'Female': 0})
controlled_applicants['Race'] = controlled_applicants['Race'].map({'White': 1, 'Non-White': 0})
# Generate biased hiring decisions
n_samples = 1000
biased_hiring = (
(controlled_applicants['Gender'] == 1) * 0.3 +
(controlled_applicants['Race'] == 1) * 0.3 +
np.random.random(n_samples) * 0.4
) > 0.5
biased_hiring = biased_hiring.astype(int)
# Combine features and labels
data = pd.concat([controlled_applicants, pd.Series(biased_hiring, name='Hired')], axis=1)
# Split the data
train_data, test_data = train_test_split(data, test_size=0.3, random_state=42)
# Create BinaryLabelDataset objects
def create_bld(df):
return BinaryLabelDataset(favorable_label=1, unfavorable_label=0,
df=df,
label_names=['Hired'],
protected_attribute_names=['Gender', 'Race'])
bld_train = create_bld(train_data)
bld_test = create_bld(test_data)
# Original biased model
biased_model = RandomForestClassifier(random_state=42)
biased_model.fit(bld_train.features, bld_train.labels.ravel())
y_pred_biased = biased_model.predict(bld_test.features)
# Reweighing
rw = Reweighing(unprivileged_groups=unprivileged_groups,
privileged_groups=privileged_groups)
bld_reweighed = rw.fit_transform(bld_train)
reweighed_model = RandomForestClassifier(random_state=42)
reweighed_model.fit(bld_reweighed.features, bld_reweighed.labels.ravel(),
sample_weight=bld_reweighed.instance_weights)
y_pred_reweighed = reweighed_model.predict(bld_test.features)
# Prejudice Remover
pr = PrejudiceRemover(eta=0.1, sensitive_attr='Gender')
pr.fit(bld_train)
y_pred_pr = pr.predict(bld_test).labels
# Calibrated Equalized Odds
cpp = CalibratedEqOddsPostprocessing(unprivileged_groups=unprivileged_groups,
privileged_groups=privileged_groups)
y_pred_biased_prob = biased_model.predict_proba(bld_test.features)[:, 1]
bld_pred_prob = bld_test.copy()
bld_pred_prob.scores = y_pred_biased_prob.reshape(-1, 1)
cpp = cpp.fit(bld_test, bld_pred_prob)
y_pred_cpp = cpp.predict(bld_pred_prob).labels
# Reweighing + Calibrated Equalized Odds
y_pred_reweighed_prob = reweighed_model.predict_proba(bld_test.features)[:, 1]
bld_pred_prob_rw = bld_test.copy()
bld_pred_prob_rw.scores = y_pred_reweighed_prob.reshape(-1, 1)
cpp_rw = CalibratedEqOddsPostprocessing(unprivileged_groups=unprivileged_groups,
privileged_groups=privileged_groups)
cpp_rw = cpp_rw.fit(bld_test, bld_pred_prob_rw)
y_pred_combined = cpp_rw.predict(bld_pred_prob_rw).labels
# Ensemble of fairness-aware models
y_pred_ensemble = np.zeros_like(y_pred_biased)
for pred in [y_pred_reweighed, y_pred_pr, y_pred_cpp]:
y_pred_ensemble += pred.reshape(-1)
y_pred_ensemble = (y_pred_ensemble > 1.5).astype(int)
# Additional technique: Threshold Optimizer
from aif360.algorithms.postprocessing import RejectOptionClassification
roc = RejectOptionClassification(unprivileged_groups=unprivileged_groups,
privileged_groups=privileged_groups,
low_class_thresh=0.01, high_class_thresh=0.99,
num_class_thresh=100, num_ROC_margin=50,
metric_name="Statistical parity difference")
roc = roc.fit(bld_test, bld_pred_prob)
y_pred_roc = roc.predict(bld_pred_prob).labels
def evaluate_fairness(dataset, predictions, model_name):
dataset_pred = dataset.copy()
dataset_pred.labels = predictions.reshape(-1, 1)
metric = ClassificationMetric(dataset, dataset_pred,
unprivileged_groups=unprivileged_groups,
privileged_groups=privileged_groups)
print(f"n{model_name} Fairness Metrics:")
print(f"Disparate Impact: {metric.disparate_impact():.4f}")
print(f"Statistical Parity Difference: {metric.statistical_parity_difference():.4f}")
print(f"Average Odds Difference: {metric.average_odds_difference():.4f}")
print(f"Equal Opportunity Difference: {metric.equal_opportunity_difference():.4f}")
print("nClassification Report:")
print(classification_report(dataset.labels, predictions))
# Evaluate all models
evaluate_fairness(bld_test, y_pred_biased, "Biased Model")
evaluate_fairness(bld_test, y_pred_reweighed, "Reweighed Model")
evaluate_fairness(bld_test, y_pred_pr, "Prejudice Remover")
evaluate_fairness(bld_test, y_pred_cpp, "Calibrated Equalized Odds")
evaluate_fairness(bld_test, y_pred_combined, "Reweighing + Calibrated Equalized Odds")
evaluate_fairness(bld_test, y_pred_ensemble, "Ensemble of Fairness-Aware Models")
evaluate_fairness(bld_test, y_pred_roc, "Reject Option Classification")
and this is the error that i sometimes get
NameError Traceback (most recent call last)
<ipython-input-2-725a0a5f5db9> in <cell line: 61>()
59
60 # Reweighing
---> 61 rw = Reweighing(unprivileged_groups=unprivileged_groups,
62 privileged_groups=privileged_groups)
63 bld_reweighed = rw.fit_transform(bld_train)
NameError: name 'unprivileged_groups' is not defined
Like i siad i have tried to restart runtime and create a new notebook and it didn’t work. please help:(
2