From 5124b8a7a1e1b760074a86b3b74d4c38dbc31118 Mon Sep 17 00:00:00 2001 From: Fiona Victoria <37321147+fionavictoria@users.noreply.github.com> Date: Fri, 16 Sep 2022 17:54:10 -0700 Subject: [PATCH] Fix - Make notebooks in examples run out of the box using Google colab (4/5) #388 Signed-off-by: fionavictoria --- examples/demo_short_gerryfair_test.ipynb | 607 ++++++++++++++++------- 1 file changed, 432 insertions(+), 175 deletions(-) diff --git a/examples/demo_short_gerryfair_test.ipynb b/examples/demo_short_gerryfair_test.ipynb index 281dee28..13baba27 100644 --- a/examples/demo_short_gerryfair_test.ipynb +++ b/examples/demo_short_gerryfair_test.ipynb @@ -1,183 +1,440 @@ { - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "pycharm": { - "is_executing": false - } - }, - "outputs": [ + "cells": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "iteration: 1, error: 0.229, fairness violation: 0.05428400000000001, violated group size: 0.249\n", - "iteration: 2, error: 0.3645, fairness violation: 0.027142000000000006, violated group size: 0.249\n", - "iteration: 3, error: 0.4096666666666666, fairness violation: 0.01809466666666667, violated group size: 0.251\n", - "iteration: 4, error: 0.43225, fairness violation: 0.013571000000000003, violated group size: 0.249\n", - "iteration: 5, error: 0.44580000000000014, fairness violation: 0.0108568, violated group size: 0.251\n", - "iteration: 6, error: 0.4548333333333334, fairness violation: 0.009047333333333338, violated group size: 0.251\n", - "iteration: 7, error: 0.46128571428571435, fairness violation: 0.007754857142857144, violated group size: 0.251\n", - "iteration: 8, error: 0.466125, fairness violation: 0.006785500000000003, violated group size: 0.251\n", - "iteration: 9, error: 0.469888888888889, fairness violation: 0.006031555555555558, violated group size: 0.249\n" - ] - } - ], - "source": [ - "%matplotlib inline\n", - "import warnings\n", - "warnings.filterwarnings(\"ignore\")\n", - "import sys\n", - "sys.path.append(\"../\")\n", - "from aif360.algorithms.inprocessing import GerryFairClassifier\n", - "from aif360.algorithms.inprocessing.gerryfair.clean import array_to_tuple\n", - "from aif360.algorithms.preprocessing.optim_preproc_helpers.data_preproc_functions import load_preproc_data_adult\n", - "from sklearn import svm\n", - "from sklearn import tree\n", - "from sklearn.kernel_ridge import KernelRidge\n", - "from sklearn import linear_model\n", - "from aif360.metrics import BinaryLabelDatasetMetric\n", - "from IPython.display import Image\n", - "import pickle\n", - "import matplotlib.pyplot as plt\n", - "\n", - "# load data set\n", - "data_set = load_preproc_data_adult(sub_samp=1000, balance=True)\n", - "max_iterations = 10\n", - "C = 100\n", - "print_flag = True\n", - "gamma = .005\n", - "\n", - "fair_model = GerryFairClassifier(C=C, printflag=print_flag, gamma=gamma, fairness_def='FP',\n", - " max_iters=max_iterations, heatmapflag=False)\n", - "# fit method\n", - "fair_model.fit(data_set, early_termination=True)\n", - "\n", - "# predict method. If threshold in (0, 1) produces binary predictions\n", - "dataset_yhat = fair_model.predict(data_set, threshold=False)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "pycharm": { - "is_executing": false - } - }, - "outputs": [ + "cell_type": "code", + "source": [ + "!pip install 'aif360[all]'" + ], + "metadata": { + "id": "tTHft8J07XX-", + "outputId": "3aee3611-2c5f-4138-c6c5-d2cc2f364ed2", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", + "Requirement already satisfied: aif360[all] in /usr/local/lib/python3.7/dist-packages (0.5.0)\n", + "Requirement already satisfied: scipy>=1.2.0 in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (1.7.3)\n", + "Requirement already satisfied: numpy>=1.16 in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (1.21.6)\n", + "Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (3.5.3)\n", + "Requirement already satisfied: pandas>=0.24.0 in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (1.3.5)\n", + "Requirement already satisfied: scikit-learn>=1.0 in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (1.0.2)\n", + "Requirement already satisfied: sphinx-rtd-theme in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (1.0.0)\n", + "Requirement already satisfied: pytest>=3.5 in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (3.6.4)\n", + "Requirement already satisfied: tensorflow>=1.13.1 in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (2.8.2+zzzcolab20220719082949)\n", + "Requirement already satisfied: jinja2<3.1.0 in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (2.11.3)\n", + "Requirement already satisfied: seaborn in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (0.11.2)\n", + "Requirement already satisfied: rpy2 in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (3.4.5)\n", + "Requirement already satisfied: tempeh in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (0.1.12)\n", + "Requirement already satisfied: igraph[plotting] in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (0.10.1)\n", + "Requirement already satisfied: adversarial-robustness-toolbox>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (1.11.1)\n", + "Requirement already satisfied: lime in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (0.2.0.1)\n", + "Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (4.64.1)\n", + "Requirement already satisfied: jupyter in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (1.0.0)\n", + "Requirement already satisfied: lightgbm in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (2.2.3)\n", + "Requirement already satisfied: fairlearn~=0.7 in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (0.7.0)\n", + "Requirement already satisfied: cvxpy>=1.0 in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (1.2.1)\n", + "Requirement already satisfied: ipympl in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (0.9.2)\n", + "Requirement already satisfied: sphinx<2 in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (1.8.6)\n", + "Requirement already satisfied: torch in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (1.12.1+cu113)\n", + "Requirement already satisfied: BlackBoxAuditing in /usr/local/lib/python3.7/dist-packages (from aif360[all]) (0.1.54)\n", + "Requirement already satisfied: setuptools in /usr/local/lib/python3.7/dist-packages (from adversarial-robustness-toolbox>=1.0.0->aif360[all]) (57.4.0)\n", + "Requirement already satisfied: numba>=0.53.1 in /usr/local/lib/python3.7/dist-packages (from adversarial-robustness-toolbox>=1.0.0->aif360[all]) (0.56.2)\n", + "Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from adversarial-robustness-toolbox>=1.0.0->aif360[all]) (1.15.0)\n", + "Requirement already satisfied: scs>=1.1.6 in /usr/local/lib/python3.7/dist-packages (from cvxpy>=1.0->aif360[all]) (3.2.0)\n", + "Requirement already satisfied: ecos>=2 in /usr/local/lib/python3.7/dist-packages (from cvxpy>=1.0->aif360[all]) (2.0.10)\n", + "Requirement already satisfied: osqp>=0.4.1 in /usr/local/lib/python3.7/dist-packages (from cvxpy>=1.0->aif360[all]) (0.6.2.post0)\n", + "Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.7/dist-packages (from jinja2<3.1.0->aif360[all]) (2.0.1)\n", + "Requirement already satisfied: llvmlite<0.40,>=0.39.0dev0 in /usr/local/lib/python3.7/dist-packages (from numba>=0.53.1->adversarial-robustness-toolbox>=1.0.0->aif360[all]) (0.39.1)\n", + "Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.7/dist-packages (from numba>=0.53.1->adversarial-robustness-toolbox>=1.0.0->aif360[all]) (4.12.0)\n", + "Requirement already satisfied: qdldl in /usr/local/lib/python3.7/dist-packages (from osqp>=0.4.1->cvxpy>=1.0->aif360[all]) (0.1.5.post2)\n", + "Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.24.0->aif360[all]) (2.8.2)\n", + "Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.24.0->aif360[all]) (2022.2.1)\n", + "Requirement already satisfied: py>=1.5.0 in /usr/local/lib/python3.7/dist-packages (from pytest>=3.5->aif360[all]) (1.11.0)\n", + "Requirement already satisfied: more-itertools>=4.0.0 in /usr/local/lib/python3.7/dist-packages (from pytest>=3.5->aif360[all]) (8.14.0)\n", + "Requirement already satisfied: pluggy<0.8,>=0.5 in /usr/local/lib/python3.7/dist-packages (from pytest>=3.5->aif360[all]) (0.7.1)\n", + "Requirement already satisfied: atomicwrites>=1.0 in /usr/local/lib/python3.7/dist-packages (from pytest>=3.5->aif360[all]) (1.4.1)\n", + "Requirement already satisfied: attrs>=17.4.0 in /usr/local/lib/python3.7/dist-packages (from pytest>=3.5->aif360[all]) (22.1.0)\n", + "Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.7/dist-packages (from scikit-learn>=1.0->aif360[all]) (1.1.0)\n", + "Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from scikit-learn>=1.0->aif360[all]) (3.1.0)\n", + "Requirement already satisfied: docutils<0.18,>=0.11 in /usr/local/lib/python3.7/dist-packages (from sphinx<2->aif360[all]) (0.17.1)\n", + "Requirement already satisfied: alabaster<0.8,>=0.7 in /usr/local/lib/python3.7/dist-packages (from sphinx<2->aif360[all]) (0.7.12)\n", + "Requirement already satisfied: Pygments>=2.0 in /usr/local/lib/python3.7/dist-packages (from sphinx<2->aif360[all]) (2.6.1)\n", + "Requirement already satisfied: imagesize in /usr/local/lib/python3.7/dist-packages (from sphinx<2->aif360[all]) (1.4.1)\n", + "Requirement already satisfied: snowballstemmer>=1.1 in /usr/local/lib/python3.7/dist-packages (from sphinx<2->aif360[all]) (2.2.0)\n", + "Requirement already satisfied: requests>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from sphinx<2->aif360[all]) (2.23.0)\n", + "Requirement already satisfied: sphinxcontrib-websupport in /usr/local/lib/python3.7/dist-packages (from sphinx<2->aif360[all]) (1.2.4)\n", + "Requirement already satisfied: packaging in /usr/local/lib/python3.7/dist-packages (from sphinx<2->aif360[all]) (21.3)\n", + "Requirement already satisfied: babel!=2.0,>=1.3 in /usr/local/lib/python3.7/dist-packages (from sphinx<2->aif360[all]) (2.10.3)\n", + "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests>=2.0.0->sphinx<2->aif360[all]) (1.24.3)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests>=2.0.0->sphinx<2->aif360[all]) (2022.6.15)\n", + "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests>=2.0.0->sphinx<2->aif360[all]) (3.0.4)\n", + "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests>=2.0.0->sphinx<2->aif360[all]) (2.10)\n", + "Requirement already satisfied: gast>=0.2.1 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (0.5.3)\n", + "Requirement already satisfied: protobuf<3.20,>=3.9.2 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (3.17.3)\n", + "Requirement already satisfied: keras<2.9,>=2.8.0rc0 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (2.8.0)\n", + "Requirement already satisfied: tensorboard<2.9,>=2.8 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (2.8.0)\n", + "Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (3.3.0)\n", + "Requirement already satisfied: typing-extensions>=3.6.6 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (4.1.1)\n", + "Requirement already satisfied: google-pasta>=0.1.1 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (0.2.0)\n", + "Requirement already satisfied: astunparse>=1.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (1.6.3)\n", + "Requirement already satisfied: wrapt>=1.11.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (1.14.1)\n", + "Requirement already satisfied: libclang>=9.0.1 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (14.0.6)\n", + "Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (1.1.0)\n", + "Requirement already satisfied: flatbuffers>=1.12 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (2.0.7)\n", + "Requirement already satisfied: grpcio<2.0,>=1.24.3 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (1.48.1)\n", + "Requirement already satisfied: tensorflow-estimator<2.9,>=2.8 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (2.8.0)\n", + "Requirement already satisfied: h5py>=2.9.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (3.1.0)\n", + "Requirement already satisfied: keras-preprocessing>=1.1.1 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (1.1.2)\n", + "Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (0.26.0)\n", + "Requirement already satisfied: absl-py>=0.4.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=1.13.1->aif360[all]) (1.2.0)\n", + "Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/local/lib/python3.7/dist-packages (from astunparse>=1.6.0->tensorflow>=1.13.1->aif360[all]) (0.37.1)\n", + "Requirement already satisfied: cached-property in /usr/local/lib/python3.7/dist-packages (from h5py>=2.9.0->tensorflow>=1.13.1->aif360[all]) (1.5.2)\n", + "Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard<2.9,>=2.8->tensorflow>=1.13.1->aif360[all]) (1.8.1)\n", + "Requirement already satisfied: google-auth<3,>=1.6.3 in /usr/local/lib/python3.7/dist-packages (from tensorboard<2.9,>=2.8->tensorflow>=1.13.1->aif360[all]) (1.35.0)\n", + "Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.7/dist-packages (from tensorboard<2.9,>=2.8->tensorflow>=1.13.1->aif360[all]) (1.0.1)\n", + "Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.7/dist-packages (from tensorboard<2.9,>=2.8->tensorflow>=1.13.1->aif360[all]) (3.4.1)\n", + "Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard<2.9,>=2.8->tensorflow>=1.13.1->aif360[all]) (0.6.1)\n", + "Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.7/dist-packages (from tensorboard<2.9,>=2.8->tensorflow>=1.13.1->aif360[all]) (0.4.6)\n", + "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.7/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.9,>=2.8->tensorflow>=1.13.1->aif360[all]) (4.9)\n", + "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.7/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.9,>=2.8->tensorflow>=1.13.1->aif360[all]) (0.2.8)\n", + "Requirement already satisfied: cachetools<5.0,>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.9,>=2.8->tensorflow>=1.13.1->aif360[all]) (4.2.4)\n", + "Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.7/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.9,>=2.8->tensorflow>=1.13.1->aif360[all]) (1.3.1)\n", + "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages (from importlib-metadata->numba>=0.53.1->adversarial-robustness-toolbox>=1.0.0->aif360[all]) (3.8.1)\n", + "Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /usr/local/lib/python3.7/dist-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard<2.9,>=2.8->tensorflow>=1.13.1->aif360[all]) (0.4.8)\n", + "Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.7/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.9,>=2.8->tensorflow>=1.13.1->aif360[all]) (3.2.0)\n", + "Requirement already satisfied: networkx in /usr/local/lib/python3.7/dist-packages (from BlackBoxAuditing->aif360[all]) (2.6.3)\n", + "Requirement already satisfied: texttable>=1.6.2 in /usr/local/lib/python3.7/dist-packages (from igraph[plotting]->aif360[all]) (1.6.4)\n", + "Requirement already satisfied: cairocffi>=1.2.0 in /usr/local/lib/python3.7/dist-packages (from igraph[plotting]->aif360[all]) (1.3.0)\n", + "Requirement already satisfied: cffi>=1.1.0 in /usr/local/lib/python3.7/dist-packages (from cairocffi>=1.2.0->igraph[plotting]->aif360[all]) (1.15.1)\n", + "Requirement already satisfied: pycparser in /usr/local/lib/python3.7/dist-packages (from cffi>=1.1.0->cairocffi>=1.2.0->igraph[plotting]->aif360[all]) (2.21)\n", + "Requirement already satisfied: ipython-genutils in /usr/local/lib/python3.7/dist-packages (from ipympl->aif360[all]) (0.2.0)\n", + "Requirement already satisfied: pillow in /usr/local/lib/python3.7/dist-packages (from ipympl->aif360[all]) (7.1.2)\n", + "Requirement already satisfied: ipython<9 in /usr/local/lib/python3.7/dist-packages (from ipympl->aif360[all]) (7.9.0)\n", + "Requirement already satisfied: ipywidgets<9,>=7.6.0 in /usr/local/lib/python3.7/dist-packages (from ipympl->aif360[all]) (7.7.1)\n", + "Requirement already satisfied: traitlets<6 in /usr/local/lib/python3.7/dist-packages (from ipympl->aif360[all]) (5.1.1)\n", + "Requirement already satisfied: decorator in /usr/local/lib/python3.7/dist-packages (from ipython<9->ipympl->aif360[all]) (4.4.2)\n", + "Requirement already satisfied: pexpect in /usr/local/lib/python3.7/dist-packages (from ipython<9->ipympl->aif360[all]) (4.8.0)\n", + "Requirement already satisfied: pickleshare in /usr/local/lib/python3.7/dist-packages (from ipython<9->ipympl->aif360[all]) (0.7.5)\n", + "Requirement already satisfied: prompt-toolkit<2.1.0,>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from ipython<9->ipympl->aif360[all]) (2.0.10)\n", + "Requirement already satisfied: jedi>=0.10 in /usr/local/lib/python3.7/dist-packages (from ipython<9->ipympl->aif360[all]) (0.18.1)\n", + "Requirement already satisfied: backcall in /usr/local/lib/python3.7/dist-packages (from ipython<9->ipympl->aif360[all]) (0.2.0)\n", + "Requirement already satisfied: jupyterlab-widgets>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (3.0.3)\n", + "Requirement already satisfied: widgetsnbextension~=3.6.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (3.6.1)\n", + "Requirement already satisfied: ipykernel>=4.5.1 in /usr/local/lib/python3.7/dist-packages (from ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (5.3.4)\n", + "Requirement already satisfied: tornado>=4.2 in /usr/local/lib/python3.7/dist-packages (from ipykernel>=4.5.1->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (5.1.1)\n", + "Requirement already satisfied: jupyter-client in /usr/local/lib/python3.7/dist-packages (from ipykernel>=4.5.1->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (6.1.12)\n", + "Requirement already satisfied: parso<0.9.0,>=0.8.0 in /usr/local/lib/python3.7/dist-packages (from jedi>=0.10->ipython<9->ipympl->aif360[all]) (0.8.3)\n", + "Requirement already satisfied: pyparsing>=2.2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->aif360[all]) (3.0.9)\n", + "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->aif360[all]) (0.11.0)\n", + "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.7/dist-packages (from matplotlib->aif360[all]) (4.37.2)\n", + "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->aif360[all]) (1.4.4)\n", + "Requirement already satisfied: wcwidth in /usr/local/lib/python3.7/dist-packages (from prompt-toolkit<2.1.0,>=2.0.0->ipython<9->ipympl->aif360[all]) (0.2.5)\n", + "Requirement already satisfied: notebook>=4.4.1 in /usr/local/lib/python3.7/dist-packages (from widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (5.3.1)\n", + "Requirement already satisfied: nbconvert in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (5.6.1)\n", + "Requirement already satisfied: jupyter-core>=4.4.0 in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (4.11.1)\n", + "Requirement already satisfied: Send2Trash in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (1.8.0)\n", + "Requirement already satisfied: nbformat in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (5.4.0)\n", + "Requirement already satisfied: terminado>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (0.13.3)\n", + "Requirement already satisfied: pyzmq>=13 in /usr/local/lib/python3.7/dist-packages (from jupyter-client->ipykernel>=4.5.1->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (23.2.1)\n", + "Requirement already satisfied: ptyprocess in /usr/local/lib/python3.7/dist-packages (from terminado>=0.8.1->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (0.7.0)\n", + "Requirement already satisfied: qtconsole in /usr/local/lib/python3.7/dist-packages (from jupyter->aif360[all]) (5.3.2)\n", + "Requirement already satisfied: jupyter-console in /usr/local/lib/python3.7/dist-packages (from jupyter->aif360[all]) (6.1.0)\n", + "Requirement already satisfied: scikit-image>=0.12 in /usr/local/lib/python3.7/dist-packages (from lime->aif360[all]) (0.18.3)\n", + "Requirement already satisfied: imageio>=2.3.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image>=0.12->lime->aif360[all]) (2.9.0)\n", + "Requirement already satisfied: PyWavelets>=1.1.1 in /usr/local/lib/python3.7/dist-packages (from scikit-image>=0.12->lime->aif360[all]) (1.3.0)\n", + "Requirement already satisfied: tifffile>=2019.7.26 in /usr/local/lib/python3.7/dist-packages (from scikit-image>=0.12->lime->aif360[all]) (2021.11.2)\n", + "Requirement already satisfied: testpath in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (0.6.0)\n", + "Requirement already satisfied: bleach in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (5.0.1)\n", + "Requirement already satisfied: mistune<2,>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (0.8.4)\n", + "Requirement already satisfied: pandocfilters>=1.4.1 in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (1.5.0)\n", + "Requirement already satisfied: defusedxml in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (0.7.1)\n", + "Requirement already satisfied: entrypoints>=0.2.2 in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (0.4)\n", + "Requirement already satisfied: jsonschema>=2.6 in /usr/local/lib/python3.7/dist-packages (from nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (4.3.3)\n", + "Requirement already satisfied: fastjsonschema in /usr/local/lib/python3.7/dist-packages (from nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (2.16.1)\n", + "Requirement already satisfied: importlib-resources>=1.4.0 in /usr/local/lib/python3.7/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (5.9.0)\n", + "Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /usr/local/lib/python3.7/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (0.18.1)\n", + "Requirement already satisfied: webencodings in /usr/local/lib/python3.7/dist-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<9,>=7.6.0->ipympl->aif360[all]) (0.5.1)\n", + "Requirement already satisfied: qtpy>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from qtconsole->jupyter->aif360[all]) (2.2.0)\n", + "Requirement already satisfied: tzlocal in /usr/local/lib/python3.7/dist-packages (from rpy2->aif360[all]) (1.5.1)\n", + "Requirement already satisfied: sphinxcontrib-serializinghtml in /usr/local/lib/python3.7/dist-packages (from sphinxcontrib-websupport->sphinx<2->aif360[all]) (1.1.5)\n", + "Requirement already satisfied: shap in /usr/local/lib/python3.7/dist-packages (from tempeh->aif360[all]) (0.41.0)\n", + "Requirement already satisfied: memory-profiler in /usr/local/lib/python3.7/dist-packages (from tempeh->aif360[all]) (0.60.0)\n", + "Requirement already satisfied: psutil in /usr/local/lib/python3.7/dist-packages (from memory-profiler->tempeh->aif360[all]) (5.4.8)\n", + "Requirement already satisfied: cloudpickle in /usr/local/lib/python3.7/dist-packages (from shap->tempeh->aif360[all]) (1.5.0)\n", + "Requirement already satisfied: slicer==0.0.7 in /usr/local/lib/python3.7/dist-packages (from shap->tempeh->aif360[all]) (0.0.7)\n" + ] + } + ] + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "0.0060315555555555565\n" - ] - } - ], - "source": [ - "# auditing \n", - "\n", - "gerry_metric = BinaryLabelDatasetMetric(data_set)\n", - "gamma_disparity = gerry_metric.rich_subgroup(array_to_tuple(dataset_yhat.labels), 'FP')\n", - "print(gamma_disparity)\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "pycharm": { - "is_executing": false - } - }, - "outputs": [ + "cell_type": "code", + "execution_count": 2, + "metadata": { + "pycharm": { + "is_executing": false + }, + "id": "Hldl5d107RfS" + }, + "outputs": [], + "source": [ + "%matplotlib inline\n", + "import warnings\n", + "warnings.filterwarnings(\"ignore\")\n", + "import sys\n", + "sys.path.append(\"../\")\n", + "from aif360.algorithms.inprocessing import GerryFairClassifier\n", + "from aif360.algorithms.inprocessing.gerryfair.clean import array_to_tuple\n", + "from aif360.algorithms.preprocessing.optim_preproc_helpers.data_preproc_functions import load_preproc_data_adult\n", + "from sklearn import svm\n", + "from sklearn import tree\n", + "from sklearn.kernel_ridge import KernelRidge\n", + "from sklearn import linear_model\n", + "from aif360.metrics import BinaryLabelDatasetMetric\n", + "from IPython.display import Image\n", + "import pickle\n", + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "code", + "source": [ + "!wget https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data\n", + "!wget https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.test\n", + "!wget https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.names" + ], + "metadata": { + "id": "8Utify1t7tDD", + "outputId": "398bfce2-80aa-4df1-a143-082c96627dfe", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "--2022-09-17 00:53:24-- https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data\n", + "Resolving archive.ics.uci.edu (archive.ics.uci.edu)... 128.195.10.252\n", + "Connecting to archive.ics.uci.edu (archive.ics.uci.edu)|128.195.10.252|:443... connected.\n", + "HTTP request sent, awaiting response... 200 OK\n", + "Length: 3974305 (3.8M) [application/x-httpd-php]\n", + "Saving to: ‘adult.data’\n", + "\n", + "adult.data 100%[===================>] 3.79M 7.00MB/s in 0.5s \n", + "\n", + "2022-09-17 00:53:25 (7.00 MB/s) - ‘adult.data’ saved [3974305/3974305]\n", + "\n", + "--2022-09-17 00:53:25-- https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.test\n", + "Resolving archive.ics.uci.edu (archive.ics.uci.edu)... 128.195.10.252\n", + "Connecting to archive.ics.uci.edu (archive.ics.uci.edu)|128.195.10.252|:443... connected.\n", + "HTTP request sent, awaiting response... 200 OK\n", + "Length: 2003153 (1.9M) [application/x-httpd-php]\n", + "Saving to: ‘adult.test’\n", + "\n", + "adult.test 100%[===================>] 1.91M 4.02MB/s in 0.5s \n", + "\n", + "2022-09-17 00:53:26 (4.02 MB/s) - ‘adult.test’ saved [2003153/2003153]\n", + "\n", + "--2022-09-17 00:53:26-- https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.names\n", + "Resolving archive.ics.uci.edu (archive.ics.uci.edu)... 128.195.10.252\n", + "Connecting to archive.ics.uci.edu (archive.ics.uci.edu)|128.195.10.252|:443... connected.\n", + "HTTP request sent, awaiting response... 200 OK\n", + "Length: 5229 (5.1K) [application/x-httpd-php]\n", + "Saving to: ‘adult.names’\n", + "\n", + "adult.names 100%[===================>] 5.11K --.-KB/s in 0s \n", + "\n", + "2022-09-17 00:53:26 (98.7 MB/s) - ‘adult.names’ saved [5229/5229]\n", + "\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "!mv /content/adult* /usr/local/lib/python3.7/dist-packages/aif360/data/raw/adult" + ], + "metadata": { + "id": "jLRW6a_k7ygt" + }, + "execution_count": 4, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# load data set\n", + "data_set = load_preproc_data_adult(sub_samp=1000, balance=True)\n", + "max_iterations = 10\n", + "C = 100\n", + "print_flag = True\n", + "gamma = .005\n", + "\n", + "fair_model = GerryFairClassifier(C=C, printflag=print_flag, gamma=gamma, fairness_def='FP',\n", + " max_iters=max_iterations, heatmapflag=False)\n", + "# fit method\n", + "fair_model.fit(data_set, early_termination=True)\n", + "\n", + "# predict method. If threshold in (0, 1) produces binary predictions\n", + "dataset_yhat = fair_model.predict(data_set, threshold=False)" + ], + "metadata": { + "id": "dEOsl-q77rLq", + "outputId": "de68ccf4-4170-4407-de22-26b37d24e97f", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "iteration: 1, error: 0.265, fairness violation: 0.028192000000000002, violated group size: 0.236\n", + "iteration: 2, error: 0.3825, fairness violation: 0.014096000000000001, violated group size: 0.236\n", + "iteration: 3, error: 0.42166666666666663, fairness violation: 0.00939733333333333, violated group size: 0.236\n", + "iteration: 4, error: 0.44125, fairness violation: 0.0070480000000000004, violated group size: 0.236\n", + "iteration: 5, error: 0.4530000000000001, fairness violation: 0.0056384, violated group size: 0.264\n", + "iteration: 6, error: 0.4608333333333334, fairness violation: 0.004698666666666669, violated group size: 0.236\n", + "iteration: 7, error: 0.46642857142857147, fairness violation: 0.004027428571428572, violated group size: 0.264\n", + "iteration: 8, error: 0.470625, fairness violation: 0.0035240000000000015, violated group size: 0.236\n", + "iteration: 9, error: 0.47388888888888897, fairness violation: 0.0031324444444444455, violated group size: 0.236\n" + ] + } + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "pycharm": { + "is_executing": false + }, + "id": "WoPtOY9a7RfV", + "outputId": "82089736-659d-4646-db49-13e6b65125c8", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "0.003132444444444444\n" + ] + } + ], + "source": [ + "# auditing \n", + "\n", + "gerry_metric = BinaryLabelDatasetMetric(data_set)\n", + "gamma_disparity = gerry_metric.rich_subgroup(array_to_tuple(dataset_yhat.labels), 'FP')\n", + "print(gamma_disparity)\n", + "\n" + ] + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "Curr Predictor: Linear\n", - "Curr Predictor: SVR\n", - "Curr Predictor: Tree\n", - "Curr Predictor: Kernel\n" - ] + "cell_type": "code", + "execution_count": 7, + "metadata": { + "pycharm": { + "is_executing": false + }, + "id": "BRcI8h9O7RfV", + "outputId": "7cea63b7-7be3-44f1-8ba9-9cd09840cad4", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Curr Predictor: Linear\n", + "Curr Predictor: SVR\n", + "Curr Predictor: Tree\n", + "Curr Predictor: Kernel\n" + ] + } + ], + "source": [ + "# set to 10 iterations for fast running of notebook - set >= 1000 when running real experiments\n", + "# tests learning with different hypothesis classes\n", + "pareto_iters = 10\n", + "def multiple_classifiers_pareto(dataset, gamma_list=[0.002, 0.005, 0.01], save_results=False, iters=pareto_iters):\n", + "\n", + " ln_predictor = linear_model.LinearRegression()\n", + " svm_predictor = svm.LinearSVR()\n", + " tree_predictor = tree.DecisionTreeRegressor(max_depth=3)\n", + " kernel_predictor = KernelRidge(alpha=1.0, gamma=1.0, kernel='rbf')\n", + " predictor_dict = {'Linear': {'predictor': ln_predictor, 'iters': iters},\n", + " 'SVR': {'predictor': svm_predictor, 'iters': iters},\n", + " 'Tree': {'predictor': tree_predictor, 'iters': iters},\n", + " 'Kernel': {'predictor': kernel_predictor, 'iters': iters}}\n", + "\n", + " results_dict = {}\n", + "\n", + " for pred in predictor_dict:\n", + " print('Curr Predictor: {}'.format(pred))\n", + " predictor = predictor_dict[pred]['predictor']\n", + " max_iters = predictor_dict[pred]['iters']\n", + " fair_clf = GerryFairClassifier(C=100, printflag=True, gamma=1, predictor=predictor, max_iters=max_iters)\n", + " fair_clf.printflag = False\n", + " fair_clf.max_iters=max_iters\n", + " errors, fp_violations, fn_violations = fair_clf.pareto(dataset, gamma_list)\n", + " results_dict[pred] = {'errors': errors, 'fp_violations': fp_violations, 'fn_violations': fn_violations}\n", + " if save_results:\n", + " pickle.dump(results_dict, open('results_dict_' + str(gamma_list) + '_gammas' + str(gamma_list) + '.pkl', 'wb'))\n", + "\n", + "multiple_classifiers_pareto(data_set)\n", + "\n" + ] } - ], - "source": [ - "# set to 10 iterations for fast running of notebook - set >= 1000 when running real experiments\n", - "# tests learning with different hypothesis classes\n", - "pareto_iters = 10\n", - "def multiple_classifiers_pareto(dataset, gamma_list=[0.002, 0.005, 0.01], save_results=False, iters=pareto_iters):\n", - "\n", - " ln_predictor = linear_model.LinearRegression()\n", - " svm_predictor = svm.LinearSVR()\n", - " tree_predictor = tree.DecisionTreeRegressor(max_depth=3)\n", - " kernel_predictor = KernelRidge(alpha=1.0, gamma=1.0, kernel='rbf')\n", - " predictor_dict = {'Linear': {'predictor': ln_predictor, 'iters': iters},\n", - " 'SVR': {'predictor': svm_predictor, 'iters': iters},\n", - " 'Tree': {'predictor': tree_predictor, 'iters': iters},\n", - " 'Kernel': {'predictor': kernel_predictor, 'iters': iters}}\n", - "\n", - " results_dict = {}\n", - "\n", - " for pred in predictor_dict:\n", - " print('Curr Predictor: {}'.format(pred))\n", - " predictor = predictor_dict[pred]['predictor']\n", - " max_iters = predictor_dict[pred]['iters']\n", - " fair_clf = GerryFairClassifier(C=100, printflag=True, gamma=1, predictor=predictor, max_iters=max_iters)\n", - " fair_clf.printflag = False\n", - " fair_clf.max_iters=max_iters\n", - " errors, fp_violations, fn_violations = fair_clf.pareto(dataset, gamma_list)\n", - " results_dict[pred] = {'errors': errors, 'fp_violations': fp_violations, 'fn_violations': fn_violations}\n", - " if save_results:\n", - " pickle.dump(results_dict, open('results_dict_' + str(gamma_list) + '_gammas' + str(gamma_list) + '.pkl', 'wb'))\n", - "\n", - "multiple_classifiers_pareto(data_set)\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.9" + }, "pycharm": { - "is_executing": false + "stem_cell": { + "cell_type": "raw", + "metadata": { + "collapsed": false + }, + "source": [] + } + }, + "colab": { + "provenance": [] } - }, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.6.9" }, - "pycharm": { - "stem_cell": { - "cell_type": "raw", - "metadata": { - "collapsed": false - }, - "source": [] - } - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file