From 97809ca313400a9243121cb727df3b1e0907d1be Mon Sep 17 00:00:00 2001 From: Jayana Gunaweera <79576139+JayanaGunaweera01@users.noreply.github.com> Date: Fri, 26 Jan 2024 16:21:18 +0530 Subject: [PATCH] add: Added a sample notebook --- examples/ethaiaudithub.ipynb | 168 +++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 examples/ethaiaudithub.ipynb diff --git a/examples/ethaiaudithub.ipynb b/examples/ethaiaudithub.ipynb new file mode 100644 index 0000000..b514847 --- /dev/null +++ b/examples/ethaiaudithub.ipynb @@ -0,0 +1,168 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "43649816", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "system imported\n" + ] + } + ], + "source": [ + "# Load all necessary packages\n", + "import sys\n", + "sys.path.insert(1, \"../\") \n", + "print(\"system imported\")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "d818c516", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (Temp/ipykernel_3400/1054722161.py, line 5)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m File \u001b[1;32m\"C:\\Users\\User\\AppData\\Local\\Temp/ipykernel_3400/1054722161.py\"\u001b[1;36m, line \u001b[1;32m5\u001b[0m\n\u001b[1;33m pip install aif360;\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "print(\"numpy imported\")\n", + "np.random.seed(0)\n", + "\n", + "pip install aif360;\n", + "\n", + "print(\"360 imported\")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f3ace3f0", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "import time\n", + "\n", + "while True:\n", + " try:\n", + " from aif360.datasets import GermanDataset\n", + " print(\"GermanDataset imported\")\n", + " break # Exit the loop if the import is successful\n", + " except ImportError:\n", + " print(\"Waiting for GermanDataset import...\")\n", + " time.sleep(1) # Wait for 1 second before trying again\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "91ebeb2e", + "metadata": {}, + "outputs": [], + "source": [ + "from aif360.metrics import BinaryLabelDatasetMetric\n", + "print(\"BinaryLabelDatasetMetric imported\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f3100718", + "metadata": {}, + "outputs": [], + "source": [ + "from aif360.algorithms.preprocessing import Reweighing\n", + "print(\"Reweighing imported\")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "67a13955", + "metadata": {}, + "outputs": [], + "source": [ + "from IPython.display import Markdown, display\n", + "print(\"Markdown, display imported\")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2bbb37e8", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "dataset_orig = GermanDataset(\n", + " protected_attribute_names=['age'], # this dataset also contains protected\n", + " # attribute for \"sex\" which we do not\n", + " # consider in this evaluation\n", + " privileged_classes=[lambda x: x >= 25], # age >=25 is considered privileged\n", + " features_to_drop=['personal_status', 'sex'] # ignore sex-related attributes\n", + ")\n", + "\n", + "dataset_orig_train, dataset_orig_test = dataset_orig.split([0.7], shuffle=True)\n", + "\n", + "privileged_groups = [{'age': 1}]\n", + "unprivileged_groups = [{'age': 0}]\n", + "\n", + "\n", + "metric_orig_train = BinaryLabelDatasetMetric(dataset_orig_train, \n", + " unprivileged_groups=unprivileged_groups,\n", + " privileged_groups=privileged_groups)\n", + "display(Markdown(\"#### Original training dataset\"))\n", + "print(\"Difference in mean outcomes between unprivileged and privileged groups = %f\" % metric_orig_train.mean_difference())\n", + "\n", + "RW = Reweighing(unprivileged_groups=unprivileged_groups,\n", + " privileged_groups=privileged_groups)\n", + "dataset_transf_train = RW.fit_transform(dataset_orig_train)\n", + "\n", + "\n", + "metric_transf_train = BinaryLabelDatasetMetric(dataset_transf_train, \n", + " unprivileged_groups=unprivileged_groups,\n", + " privileged_groups=privileged_groups)\n", + "display(Markdown(\"#### Transformed training dataset\"))\n", + "print(\"Difference in mean outcomes between unprivileged and privileged groups = %f\" % metric_transf_train.mean_difference())" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}