<jupyter_start><jupyter_text>Testing DEMV on Drug Dataset - Sensitive group: white women (`race: 1, gender: 0`)- Positive label: never (0)<jupyter_code>import pandas as pd
import numpy as np

from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

from fairlearn.reductions import ExponentiatedGradient, BoundedGroupLoss, ZeroOneLoss, GridSearch

import matplotlib.pyplot as plt
import seaborn as sns

from utils import *
from demv import DEMV

import warnings
warnings.filterwarnings('ignore')

sns.set_style('whitegrid')
def prepare_data():
  data = pd.read_csv('data2/drugs.csv')
  data.drop(['yhat','a'], axis=1, inplace=True)
  data.loc[data['gender']==0.48246,'gender']=1
  data.loc[data['gender']==-0.48246,'gender']=0
  data['y'].replace({
    'never': 0,
    'not last year': 1,
    'last year': 2}, inplace=True)
  data['race'].replace({
    'non-white': 0,
    'white': 1}, inplace=True)
  string_cols = data.dtypes[data.dtypes == 'object'].index.values
  data.drop(string_cols, axis=1, inplace=True)
  return data

data = prepare_data()
data
label = 'y'
protected_group = {'race': 1, 'gender': 0}
positive_label = 0
sensitive_features = ['race', 'gender']
pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('classifier', LogisticRegression(n_jobs=-1))
])<jupyter_output><empty_output><jupyter_text>Biased classifier<jupyter_code>model, lr_bias = cross_val(pipeline, data, label, protected_group, sensitive_features, positive_label)
print_metrics(lr_bias)<jupyter_output>Equalized odds:  0.319  +-  0.143
Disparate impact:  0.142  +-  0.081
Zero one loss:  0.335  +-  0.098
Accuracy score:  0.68  +-  0.025<jupyter_text>DEMV application<jupyter_code>demv = DEMV(round_level=1)
demv_data = data.copy()
model, lr_demv = cross_val(pipeline, demv_data, label, protected_group, sensitive_features, debiaser=demv, positive_label=positive_label)
print_metrics(lr_demv)<jupyter_output>Equalized odds:  0.171  +-  0.079
Disparate impact:  0.497  +-  0.143
Zero one loss:  0.334  +-  0.115
Accuracy score:  0.676  +-  0.027<jupyter_text>DEMV Evaluation<jupyter_code>demv.get_iters()
metrics = eval_demv(17, 89, data.copy(), pipeline, label, protected_group, sensitive_features, positive_label=positive_label)<jupyter_output><empty_output><jupyter_text>Blackbox Postprocessing<jupyter_code>model, blackboxmetrics, pred = cross_val2(pipeline, data, label, protected_group, sensitive_features, positive_label=positive_label)<jupyter_output>Pre-adjustment group rates are 

0
      fpr     tpr
0  0.2113  0.6098
1  0.1829  0.3333
2  0.1549  0.8780

1
      fpr     tpr
0  0.0484  0.4000
1  0.1194  0.5000
2  0.2400  0.9423


And loss is 0.3069


Pre-adjustment group rates are 

0
      fpr     tpr
0  0.2154  0.5714
1  0.1757  0.3462
2  0.1311  0.9231

1
      fpr     tpr
0  0.0508  0.3636
1  0.1148  0.5556
2  0.2000  0.9400


And loss is 0.2882

[0.57142857 0.39107721 0.03749421]
[0.5        0.45284435 0.04715564]
[0.02564103 0.77878164 0.19557734]
[0.57142857 0.39074215 0.03782927]
[0.50091987 0.45284435 0.04623578]
[0.16398827 0.64043439 0.19557734]

Pre-adjustment group rates are 

0
      fpr     tpr
0  0.1940  0.6786
1  0.1304  0.3462
2  0.1667  0.8780

1
      fpr     tpr
0  0.0484  0.3846
1  0.1077  0.5000
2  0.2609  0.9423


And loss is 0.2765

[0.47514222 0.47562279 0.049235  ]
[0.32317365 0.62334198 0.05348437]
[0.01707828 0.79768561 0.18523611]
[0.47514222 0.47949383 0.04536395]
[0.31768488 0.62334198 0.05897314]
[...]<jupyter_text>Plot<jupyter_code>df = prepareplots(metrics, 'drugs')
# df = pd.read_csv('ris/drugs_eval.csv')
points = preparepoints(blackboxmetrics, 85)
plot_metrics_curves(df, points, 'Drug')
unprivpergentage(data, protected_group, 89)
save_metrics('blackbox', 'drug', blackboxmetrics)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12357217030114226, 'fraction_numerical': 0.15809968847352024, 'mean_word_length': 4.480796586059744, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '505074', 'license': 'Apache-2.0', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1555, 'n_words': 337, 'path': 'drug.ipynb', 'repo_name': 'giordanoDaloisio/fairness', 'size': '3852'}