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

# # Retraining Classifier
# In the previous examples, the classifier was only trained in the first generation and a artificial decision function is used. Here, one example is given with a true decision function retrained in every time step.

# +
import sys
import numpy as np
sys.path.append("../")

from fairensics.methods import AccurateDisparateImpact, DisparateMistreatment

from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC, SVC

from ltf.ltf_plot import LongTermFairnessPlot
from ltf.ltf_data.individual_data_generator import IndividualDataGenerator
from ltf.ltf_metric.aif360_metric import AifLongTermMetric
from ltf.ltf_clf.aif360_clf import AifLongTermCLF
# -

import warnings
warnings.filterwarnings("ignore")

# The generator.
generator = IndividualDataGenerator(mean_pos=[0, 1],
                                    mean_neg=[12, 11],
                                    num_positive_label=100,
                                    num_negative_label=100,
                                    discrimination_factor=.9)

# The long term metric with default options (disparate impact and accuracy).
metric = AifLongTermMetric()

# ## 2. Constrained Example 

clf = AifLongTermCLF(AccurateDisparateImpact())
l = LongTermFairnessPlot(generator, clf, metric.metric, x_lim=[-3, 13], y_lim=[-1, 13], update_clf=True)

l.init_data()
l.plot_step()

for i in range(20):
    l.run_step()
    l.run_baseline_step()
    if i % 5 == 0:
        l.plot_step()

l.plot_ltf(metric._metrics)

l.plot_step()

StarcoderdataJupyterScriptsDedupFiltered