I'm trying to use the aif360 library of ibm for debiasing. I'm working on a linear regression model and want to try out a metric to calculate the difference between the priviliged and unpriviliged groups. However when this code is run I get the following error:
TypeError: difference() missing 1 required positional argument: 'metric_fun'
I've looked into the class for this function but they are referring to a metric_fun, also read the docs but didn't get any further. The function is missing an argument, but I don't know which argument it expects.
A short snippit of the code is:
train_pp_bld = StructuredDataset(df=pd.concat((x_train, y_train),
axis=1),
label_names=['decile_score'],
protected_attribute_names=['sex_Male'],
privileged_protected_attributes=1,
unprivileged_protected_attributes=0)
privileged_groups = [{'sex_Male': 1}]
unprivileged_groups = [{'sex_Male': 0}]
# Create the metric object
metric_train_bld = DatasetMetric(train_pp_bld,
unprivileged_groups=unprivileged_groups,
privileged_groups=privileged_groups)
# Metric for the original dataset
metric_orig_train = DatasetMetric(train_pp_bld,
unprivileged_groups=unprivileged_groups,
privileged_groups=privileged_groups)
display(Markdown("#### Original training dataset"))
print("Difference in mean outcomes between unprivileged and privileged groups = %f" % metric_orig_train.difference())
The stack trace that was given is:
Traceback (most recent call last):
File "/Users/sef/Desktop/Thesis/Python Projects/Stats/COMPAS_Debias_AIF360_Continuous_Variable.py", line 116, in <module>
print("Difference in mean outcomes between unprivileged and privileged groups = %f" % metric_orig_train.difference())
File "/Users/sef/opt/anaconda3/envs/AI/lib/python3.8/site-packages/aif360/metrics/metric.py", line 37, in wrapper
result = func(*args, **kwargs)
TypeError: difference() missing 1 required positional argument: 'metric_fun'
After creating a function:
def privileged_value(self, privileged=False):
if privileged:
return unprivileged_groups['sex_Male']
else:
return privileged_groups['sex_Male']
display(Markdown("#### Original training dataset"))
print("Difference in mean outcomes between unprivileged and privileged groups = %f" % metric_orig_train.difference(privileged_value))
still get a similar error traceback:
Traceback (most recent call last):
File "/Users/sef/Desktop/Thesis/Python Projects/Stats/COMPAS_Debias_AIF360_Continuous_Variable.py", line 123, in <module>
print("Difference in mean outcomes between unprivileged and privileged groups = %f" % metric_orig_train.difference(privileged_value))
File "/Users/sef/opt/anaconda3/envs/AI/lib/python3.8/site-packages/aif360/metrics/metric.py", line 37, in wrapper
result = func(*args, **kwargs)
File "/Users/sef/opt/anaconda3/envs/AI/lib/python3.8/site-packages/aif360/metrics/dataset_metric.py", line 77, in difference
return metric_fun(privileged=False) - metric_fun(privileged=True)
File "/Users/youssefennali/Desktop/Thesis/Python Projects/Stats/COMPAS_Debias_AIF360_Continuous_Variable.py", line 120, in privileged_value
return privileged_groups['sex_Male']
TypeError: list indices must be integers or slices, not str
Could someone please point me in the right direction? There are no examples available of similar code online.
Regards,
Sef
Looking at the source code for the library on GitHub a reference to a function needs to be passed into
difference(self, metric_fun). All difference does is subtract the output of your function withprivileged=Falseas the input with the output of your function withprivileged=Trueas the input.Create a function like this and pass it into difference.