{"id":23249174,"url":"https://github.com/divyat09/cf-feasibility","last_synced_at":"2025-08-20T07:31:29.710Z","repository":{"id":39739112,"uuid":"242301032","full_name":"divyat09/cf-feasibility","owner":"divyat09","description":"Code accompanying the paper \"Preserving Causal Constraints in Counterfactual Explanations for Machine Learning Classifiers\"","archived":false,"fork":false,"pushed_at":"2023-03-24T22:24:50.000Z","size":40132,"stargazers_count":29,"open_issues_count":1,"forks_count":10,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-02T11:13:06.056Z","etag":null,"topics":["causality","counterfactual-explanations","explainable-ml","machine-learning","neurips-2019"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/divyat09.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-22T07:42:52.000Z","updated_at":"2024-03-19T06:26:11.000Z","dependencies_parsed_at":"2022-08-21T01:10:57.705Z","dependency_job_id":null,"html_url":"https://github.com/divyat09/cf-feasibility","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divyat09%2Fcf-feasibility","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divyat09%2Fcf-feasibility/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divyat09%2Fcf-feasibility/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divyat09%2Fcf-feasibility/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/divyat09","download_url":"https://codeload.github.com/divyat09/cf-feasibility/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230400578,"owners_count":18219832,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["causality","counterfactual-explanations","explainable-ml","machine-learning","neurips-2019"],"created_at":"2024-12-19T08:17:41.373Z","updated_at":"2024-12-19T08:17:41.975Z","avatar_url":"https://github.com/divyat09.png","language":"Python","funding_links":[],"categories":["🚀 GitHub Repositories","Github Repos"],"sub_categories":["🎓 Educational \u0026 Tutorial Resources"],"readme":"# Feasible Counterfactual Explanations\nCode accompanying the paper [Preserving Causal Constraints in Counterfactual Explanations for Machine Learning Classifiers](https://arxiv.org/abs/1912.03277), selected for Oral Spotlight at the [NeurIPS 2019 Workshop](http://tripods.cis.cornell.edu/neurips19_causalml/) on Machine learning and Causal Inference for improved decision making\n\n# DiCE\n\nThis work is also being integerated with [DiCE](https://github.com/interpretml/DiCE), an open source library for explaining ML models. Please check the this [tutorial](https://github.com/interpretml/DiCE/blob/master/docs/notebooks/DiCE_getting_started_feasible.ipynb) and follow DiCE for updates regarding the same. \n\n# Cite\n```bibtex\n@article{mahajan2019preserving,\n  title={Preserving Causal Constraints in Counterfactual Explanations for Machine Learning Classifiers},\n  author={Mahajan, Divyat and Tan, Chenhao and Sharma, Amit},\n  journal={arXiv preprint arXiv:1912.03277},\n  year={2019}\n}\n```\n\n# Code Structure \n\n### generativecf \n\nContains the code for experiments on Simple-BN, Sangiovese, Adult dataset\n\n### generativecf-mnist\n\nContaints the code for experiments on MNIST \n\n## generativecf\n\n* models/\n\n  - Contains pre trained models for the different methods across datasets\n\n* data/\n\n  - Contains the processed data files for all the datasets; download the data files from this [link](https://drive.google.com/drive/folders/1wI_PdEC9bTc80Lj0Th8LZ63dQGmtaDij?usp=sharing)\n\n- master_evalute.py \n\n  - Utilizes the pre trained models (models/) and datasets (data/) to reproduce the results mentioned in the paper. The results are stored in the directory /results\n\n  - It also generates a file 'plot_dict.json' in the directory r_plots/; where you may convert it to plotdf.csv file and then execute 'plot_figures.R' script to get better graphs stored in the directory /results\n\n- base-generative-cf.py\n\n  - Implementation of BaseGenCF for all datasets\n\n  - Usage: python3 base-generative-cf.py --htune 0 --batch_size 64 --epoch 50 --dataset_name bn1 --margin 0.1  --validity_reg 10 \n\n- ae-base-generative-cf.py\n\n  - Implementation of AEGenCF for all datasets\n\n  - Usage: python3 ae-base-generative-cf.py --htune 0 --batch_size 64 --epoch 50 --dataset_name bn1 --ae_path bn1-64-50-target-class--1-auto-encoder.pth --margin 0.1  --validity_reg 10 --ae_reg 10\n\n- oracle-generative-cf.py\n\n  - Implementation of OracleGenCF for all datasets\n\n  - Usage: python3 oracle-generative-cf.py --htune 0 --batch_size 64 --epoch 50 --dataset_name bn1 --cf_path bn1-margin-0.014-validity_reg-54.0-epoch-50-base-gen.pth --oracle_data bn1-fine-tune-size-100-upper-lim-10-good-cf-set.json --margin 0.1 --validity_reg 10 --oracle_reg 10\n\n- model-approx-generative-cf.py\n\n  - Implementation of ModelApproxGenCF for Simple-BN dataset\n\n  - Usage: python3 model-approx-generative-cf.py --htune 0  --batch_size 64 --epoch 50 --dataset_name bn1 --ae_reg 0 --ae_path bn1-64-50-target-class--1-auto-encoder.pth  --margin 0.1 --validity_reg 10 --constraint_reg 10\n\n- model-approx-generative-cf-bnlearn.py\n\n  - Implementation of ModelApproxGenCF for Sangiovese dataset\n\n  - Usage: python3 model-approx-generative-cf-bnlearn.py --htune 0 --batch_size 512 --epoch 50 --dataset_name sangiovese --ae_reg 0 --ae_path sangiovese-512-50-target-class--1-auto-encoder.pth  --margin 0.1 --validity_reg 10 --constraint_reg 10 --constrained_nodes 'BunchN'\n\n- unary-const-generative-cf.py\n\n  - Implementation of ModelApproxGenCF for Adult dataset C1 constraint ( Non Decreasing Age )\n\n  - Usage: python3 unary-const-generative-cf.py --htune 0 --batch_size 2048 --epoch 50 --dataset_name adult --margin 0.1 --validity_reg 10 --constraint_reg 10\n\n- unary-ed-const-generative-cf.py\n\n  - Implementation of ModelApproxGenCF for Adult dataset C2 constraint ( Age-Ed Causal Constraint )\n\n  - Usage: python3 unary-ed-const-generative-cf.py --htune 0 --batch_size 2048 --epoch 50 --dataset_name adult --margin 0.1 --validity_reg 10 --constraint_reg 10\n\n- scm-generative-cf.py\n\n  - Implementation of SCMGenCF for Simple-BN dataset \n\n  - Usage: python3 scm-generative-cf.py --htune 0 --batch_size 64 --epoch 50 --dataset_name bn1 --margin 0.1 --validity_reg 10 --scm_reg 10\n\n- scm-generative-cf-bnlearn.py\n\n  - Implementation of SCMGenCF for Sangiovese dataset \n\n  - Usage: python3 scm-generative-cf-bnlearn.py --htune 0 --batch_size 512 --epoch 50 --dataset_name sangiovese --validity_reg 10 --scm_reg 10 --constraint_node 'BunchN'\n\n- contrastive_explanations.py\n\n  - Implementation of CEM for all datasets\n\n  - Usage: python3 contrastive_explanations.py --dataset_name bn1 --htune 0 --train_case_pred 0 --train_case_ae 0 --explain_case 1 --sample_size 3 --timeit 0 --c_init 10 --max_iterations 1000 --beta 0.1 --kappa 0.1 --gamma 1 --c_steps 2\n\n- timeit-base-generative-cf.py\n\n  - Computing the training and evaluaiton time of BaseGenCF\n\n  - Usage: python3 timeit-base-generative-cf.py --htune 0 --batch_size 64 --epoch 50 --dataset_name bn1 --margin 0.1  --validity_reg 10 --cf_path bn1-margin-0.014-validity_reg-54.0-epoch-50-base-gen.pth\n\n- timeit-oracle-generative-cf.py\n\n  - Computing the training and evaluaiton time of Example-based CF\n\n  - Usage: python3 timeit-oracle-generative-cf.py --htune 0 --batch_size 64 --epoch 50 --dataset_name bn1 --cf_path bn1-margin-0.014-validity_reg-54.0-epoch-50-base-gen.pth --oracle_data bn1-fine-tune-size-100-upper-lim-10-good-cf-set.json --margin 0.1 --validity_reg 10 --oracle_reg 10 \n\n## generativecf/scripts/\n\n- blackboxmodel.py\n\n  - Contains the architecture of the ML model to be explained across datasets\n\n- vae_model.py\n\n  - Contains the architecutre of the BaseGenCF and AutoEncoder model\n\n- blackbox-model-train.py\n\n  - Trains the ML model to be explained across datasets\n\n  - Usage: python3 blackbox-model-train.py bn1\n\n- auto-encoder-train.py\n\n  - Trains the Auto Encoder model used in AEGenCF and computing IM Metric\n\n  - Usage: python3 auto-encoder-train.py --dataset_name bn1 --batch_size 64 --epoch 50 --target_class -1\n\n- good-cf-set-gen.py\n\n  - Contains the code for generating labelled queries for OracleGenCF for Simple-BN, Adult dataset\n\n  - Usage: python3 good-cf-set-gen.py --dataset_name bn1 --fine_tune_size 100 --upper_limit 10 --cf_path bn1-margin-0.014-validity_reg-54.0-epoch-50-base-gen.pth\n\n- good-cf-set-gen-bnlearn.py\n\n  - Contains the code for generating labelled queries for OracleGenCF for Simple-BN, Adult dataset\n\n  - Usage: python3 good-cf-set-gen-bnlearn.py --dataset_name sangiovese --fine_tune_size 100 --upper_limit 10 --cf_path sangiovese-margin-0.161-validity_reg-94.0-epoch-50-base-gen.pth --constraint_node BunchN\n\n- datagen.py\n\n  - Creates train, val, test splits with other important processed data for all datasets\n\n  - Usage: python3 datagen.py bn1\n\n- evaluation_functions.py\n\n  - Contains evaluations metrics like Target-Class Validity, Constraint Feasibility Score, etc. for all datasets\n\n- bnlearn_parser.py\n\n  - Reads the sangiovese-scm.txt and creates the SCM \n\n- helpers.py\n\n  - Contains code for generating the  Adult dataset\n\n- sangiovese-data-gen.py\n\n  - Contains code for processing the Sangiovese dataset\n\n- simple-bn-gen.py\n\n  - Contains the code for generating the Simple-BN dataset\n\n\n## generativecf-mnist \n\nSimilar description as stated above for generativecf files; with the only difference that evaluation happens for MNIST dataset.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdivyat09%2Fcf-feasibility","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdivyat09%2Fcf-feasibility","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdivyat09%2Fcf-feasibility/lists"}