<jupyter_start><jupyter_text>Computes Fairness/Bias metricsUses the AIF360 AI Fairness 360 toolkit to compute Fairness/Bias metrics<jupyter_code>!pip3 install scikit-learn==0.24.1 aif360==0.3.0  tensorflow==2.4.0 nodejs==0.1.1 ipywidgets==7.6.3 lime==0.2.0.1 wget==3.2 #aix360==0.2.1 
import wget
wget.download(
    'https://raw.githubusercontent.com/' +
    'elyra-ai/component-library/master/claimed_utils.py'
)
import os
from claimed_utils import unzip
import pandas as pd
from aif360.datasets import BinaryLabelDataset
import pickle
from aif360.metrics import BinaryLabelDatasetMetric
# @dependency codait_utils.ipynb
# @dependency metadata
# @param target_column Column name containing the target/prediction value
# (the real measured value)
# @param protected_column Protected column (like sex, race, age, ...)
# Note: column arrays not supported at the moment
# @param prediction_column Column name containing the prediction of the model
# @param unpriviledged_group_key value containted in the protected_column
# indicating a unpriviledged group (e.g. female)
# @param priviledged_group_key value containted in the protected_column
# indicating a priviledged group (e.g. male)
# @param priviledged_group_key value containted in the protected_column
# indicating a priviledged group (e.g. male)
# @param metadata csv file name of the data
# @param metric_output file name of the pickeled metric object
# @returns pickeled metric object
target_column = os.environ.get('target_column', 'target')
protected_column = os.environ.get('protected_column')
prediction_column = os.environ.get('prediction_column', 'prediction')
unpriviledged_group_key = os.environ.get('unpriviledged_group_key')
priviledged_group_key = os.environ.get('priviledged_group_key')
metadata = os.environ.get('metadata', 'metadata.csv')
metric_output = os.environ.get('metric_output', 'metric.pickle')
# !jupyter labextension install @jupyter-widgets/jupyterlab-manager
unzip('.', 'model.zip')
unzip('.', 'data.zip')
df = pd.read_csv(metadata)
df["protected_column_index"] = df[protected_column].apply(
    lambda x:
    list(df[protected_column].unique()).index(x)
)

df["missclassified"] = df.apply(
    lambda d:
    1 if d[target_column] != d[prediction_column] else 0, axis=1
)
unprivileged_groups = [
    {'protected_column_index':
     df.loc[
         df[protected_column] == unpriviledged_group_key
     ].iloc[0]['protected_column_index']}
]
privileged_groups = [
    {'protected_column_index':
     df.loc[
         df[protected_column] == priviledged_group_key
     ].iloc[0]['protected_column_index']}
]

favorable_label = 0
unfavorable_label = 1  # missclassified == True

df_for_aif360 = df[["protected_column_index", "missclassified"]]

df_for_aif360
aif360_dataset = BinaryLabelDataset(
    favorable_label=favorable_label,
    unfavorable_label=unfavorable_label,
    df=df_for_aif360,
    label_names=['missclassified'],
    protected_attribute_names=['protected_column_index'],
    unprivileged_protected_attributes=unprivileged_groups)
metric_orig_train = BinaryLabelDatasetMetric(
    aif360_dataset,
    unprivileged_groups=unprivileged_groups,
    privileged_groups=privileged_groups)
metric_orig_train.statistical_parity_difference()
metric_orig_train.smoothed_empirical_differential_fairness()
pickle.dump(metric_orig_train, open(metric_output, "wb"))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1044069801833777, 'fraction_numerical': 0.019520851818988466, 'mean_word_length': 6.150105708245243, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '473710', 'license': 'Apache-2.0', 'n_tokens_mistral': 1151, 'n_tokens_neox': 1111, 'n_words': 233, 'path': 'metric/metric-aif360.ipynb', 'repo_name': 'Tales-Carvalho/component-library', 'size': '3381'}