# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.8.8 64-bit (''tesi'': conda)'
#     language: python
#     name: python3
# ---

# # Testing DEMV on _Wine Quality_ Dataset

# - Label: `quality`
# - Positive label: `quality=6`
# - Unprivileged group: `alcohol=0 (<=10) & type=1 (white)`
#
# Source: [https://archive.ics.uci.edu/ml/datasets/wine+quality](https://archive.ics.uci.edu/ml/datasets/wine+quality)

# +
import pandas as pd

from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
#from sklearnex import patch_sklearn

from fairlearn.reductions import ExponentiatedGradient, BoundedGroupLoss, ZeroOneLoss

import matplotlib.pyplot as plt
import seaborn as sns

from demv import DEMV
from utils import *

import warnings
warnings.filterwarnings('ignore')

#patch_sklearn()
sns.set_style('whitegrid')


# -

def load_data():
  red = pd.read_csv('data2/winequality-red.csv', sep=';')
  red['type'] = 0
  white = pd.read_csv('data2/winequality-white.csv', sep=';')
  white['type'] = 1
  data = red.append(white)
  data.drop(data[(data['quality']==3)|(data['quality']==9)].index, inplace=True)
  data.loc[data['alcohol'] <= 10, 'alcohol'] = 0
  data.loc[(data['alcohol'] > 10) & (data['alcohol'] != 0), 'alcohol'] = 1
  return data


data = load_data()

data['quality'].hist()

data = data.loc[ data.quality != 8 ]

label = 'quality'
sensitive_variables = ['alcohol', 'type']
protected_group = {'alcohol': 0, 'type': 1}
positive_label = 6

pipeline = Pipeline([
  ('scaler', StandardScaler()),
  ('classifier', LogisticRegression(n_jobs=-1))
])

# ## Biased dataset

model, bias_metrics = cross_val(pipeline, data, label, protected_group, sensitive_variables,positive_label=positive_label)

print_metrics(bias_metrics)

# ## DEMV dataset

demv = DEMV(round_level=1)
demv_data = data.copy()

model, demv_metrics = cross_val(pipeline, demv_data, label, protected_group, sensitive_variables, positive_label=positive_label, debiaser=demv)

print_metrics(demv_metrics)

# ## DEMV Evaluation

demv.get_iters()

metrics = eval_demv(76, demv.get_iters(), data.copy(), pipeline, label,
                    protected_group, sensitive_variables, positive_label=positive_label)


# ##  Blackbox Postprocessing 

data = data.sample(frac=1).reset_index(drop=True)

model, blackboxmetrics, pred = cross_val2(pipeline, data, label, protected_group, sensitive_variables, positive_label=positive_label)

# ##  Plot

df = prepareplots(metrics,'wine')

# +
points = preparepoints(blackboxmetrics, 305)

plot_metrics_curves(df, points, 'Wine Multiclass Dataset')

# -

unprivpergentage(data,protected_group, demv.get_iters())

save_metrics('blackbox', 'wine', blackboxmetrics)

StarcoderdataJupyterScriptsDedupFiltered