Skip to main content

Python package for counterfactuals generation and presentation

Project description

Tests flake8,mypy Documentation Status

CFEC

This is a specialized programming library which contains three different counterfactual generation methods for tabular data, supporting various constraints, and to construct a tool for comparing their results.

Requirements

Tha package has been tested under python 3.7 up to 3.9, on both Windows and Ubuntu platforms. Its main dependency is tensorflow, which all the methods use, and typical scientific stack (numpy, scipy, pandas). Requirements include:

  • tensorflow~=2.7.0
  • pandas==1.3.4
  • numpy==1.21.4
  • scikit-learn==1.0.1

Installation

This package can be installed using pip

pip install cfec

Implemented algorithms

Our package includes implementation of algorithms, such as:

Example usage

from cfec.explainers import Fimap
from cfec.constraints import ValueMonotonicity, ValueNominal
from data import AdultData
from sklearn.ensemble import RandomForestClassifier

adult_data = AdultData('data/datasets/adult.csv')

rf = RandomForestClassifier()
rf.fit(adult_data.X_train, adult_data.y_train)

predictions = rf.predict(adult_data.X_train)

constraints = [
    OneHot('workclass', 2, 8),
    OneHot('martial.status', 9, 15),
    OneHot('occupation', 16, 29),
    OneHot('race', 30, 34),
    OneHot('sex', 35, 36),
]

fimap = Fimap(constraints=constraints)

fimap.fit(adult_data.X_train, predictions)

x = adult_data.X_train.iloc[0]
cf = fimap.generate(x)
from cfec.explainers import Cadex
from cfec.constraints import ValueMonotonicity, ValueNominal
from data import GermanData
from tensorflow import keras

german_data = GermanData('data/datasets/input_german.csv', 'data/datasets/labels_german.csv')

# simple model consisting of one dense layer with 2 units and a softmax activation
german_model = keras.models.load_model('models/model_german')

predictions = german_model.predict(german_data.X_train)

constraints = [
    OneHot("account_status", 7, 10), 
    OneHot("credit_history", 11, 15),
    OneHot("purpose", 16, 25), 
    OneHot("savings", 26, 30), 
    OneHot("sex_status", 31, 34),
    OneHot("debtors", 35, 37), 
    OneHot("property", 38, 41),
    OneHot("other_installment_plans", 42, 44), 
    OneHot("housing", 45, 47), 
    OneHot("job", 48, 51),
    OneHot("phone", 52, 53), 
    OneHot("foreign", 54, 55), 
    OneHot("employment", 56, 60)
]

cadex = Cadex(german_model, constraints=constraints)

x = german_data.X_train.iloc[0]
cf = cadex.generate(x) # cadex method does not need to fit before generate

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

CFEC-0.0.5.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

CFEC-0.0.5-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file CFEC-0.0.5.tar.gz.

File metadata

  • Download URL: CFEC-0.0.5.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for CFEC-0.0.5.tar.gz
Algorithm Hash digest
SHA256 343d7e2d7f0ccb3b47545087ddc80388fd599702dfb36199942e1b1094b631f6
MD5 23daf92de055cd87d7bf77227b5a8e1b
BLAKE2b-256 dd0ed5e934ab74d0f3f504a360d885d798aa119519eec403a5eba2986dcfd5a1

See more details on using hashes here.

File details

Details for the file CFEC-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: CFEC-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for CFEC-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 2d0897bbf364c111838aa34e965bc82c391abf6919efd0255c53572cf4021e53
MD5 046cdca9a33b2b07a9179855972256df
BLAKE2b-256 fcde759e33b651621235af59cdd7b4e238482c87614274be7e0f5d220abd9729

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page