# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # This is the demo to apply our AutoML system to AdultDataset

# ## dataset_metric you can select and their ranges
# 'mean_difference': [-1,1] close to 0 <br>
# 'statistical_parity_difference': [-1,1] close to 0 <br>
# 'disparate_impact': > 0  The larger the better<br>
# 'consistency': [0, 1]: The larger the better

# ## classifier_metric (optimal range)
# "Statistical parity difference" : [-0.1, 0.1]<br>
# "Mean difference": [-0.1, 0.1] <br>
# "Disparate impact": [0.8,1.2] <br>
# "Average odds difference" [-0.1, 0.1] <br>
# "Equal opportunity difference" [-0.1, 0.1] <br>
# "Theil index": [0, 0.2]

# +
import warnings
warnings.filterwarnings('ignore')

import AutoML
import pandas as pd
import numpy as np
from importlib import reload
pd.set_option('display.max_columns', None)
import matplotlib.pyplot as plt
# %matplotlib inline
df = pd.read_csv('mylsn_raw.csv')
df = df.drop(['Unnamed: 0'], axis=1)
train = df.sample(frac=0.5, random_state=200)
test = df.drop(train.index)

# set up the parameters for StandardDataset class
input_columns = ['sex', 'gpa', 'lsat', 'lsat1']#,'school']  # school will generate too many columns
label_name = 'status'
favorable_classes = ['Ac']
protected_attribute_names = ['sex']
privileged_classes = [['Male','male']]
privileged_groups = [{'sex':1}]
unprivileged_groups = [{'sex': 0}]
categorical_features = []#['school']
features_to_keep = ['sex', 'gpa', 'lsat']#,'school']
features_to_drop = []
is_valid = False

reload(AutoML)
model = AutoML.FairAutoML(input_columns, label_name, favorable_classes, 
                 protected_attribute_names, privileged_classes,
                 privileged_groups, unprivileged_groups,
                 categorical_features, features_to_keep, 
                 features_to_drop, is_valid)
model.fit(train, dataset_metric="disparate_impact", dataset_metric_threshold=0.9,
            classifier_metric="Disparate impact", optim_options = None,
            time_left_for_this_task=200, per_run_time_limit=20, train_split_size=0.8,
            verbose=True)
# -

fig = model.plot()
fig

print(model.preproc_name)
print('Best classification threshold: {}'.format(model.best_ultimate_thres))
print('Best balanced accuracy: {}'.format(model.best_acc))

metrics = model.evaluate(test)

x = np.arange(2)
y = [0.09, 0.08]
fig, ax = plt.subplots()
for i, v in enumerate(y):
    ax.text(i-0.1, 
              y[i], 
              y[i], 
              fontsize=11, 
              color='b')
plt.bar(x,y, color='lightblue')
plt.xlabel('Preprocessing Method for Law School Data')
plt.ylabel('ABS(Mean Diff)')
plt.axhline(y=0.1, color='red', linestyle='-')
plt.annotate('0.10', (0.44,0.105),color = 'red')
plt.xticks(x, ('DIR', 'LFR'))
plt.ylim((0, 0.115))
plt.show()

x = np.arange(2)
y = [0.93, 0.99]
fig, ax = plt.subplots()
for i, v in enumerate(y):
    ax.text(i-0.1, 
              y[i], 
              y[i], 
              fontsize=11, 
              color='b')
plt.bar(x,y, color='lightblue')
plt.xlabel('Preprocessing Method for Law School Data')
plt.ylabel('Disparate impact')
plt.axhline(y=0.88, color='red', linestyle='-')
plt.annotate('0.88', (0.44,0.9),color = 'red')
plt.xticks(x, ('DIR', 'LFR'))
plt.ylim((0, 1.1))
plt.show()

StarcoderdataJupyterScriptsDedupFiltered