Skip to main content

Counter factual exploration utility

Project description

whatifact

A Counter Factual Exploration Tool

Counter Factual explanations are a methdology to investigate model's predictions. whatifact allows data scientists to play with their population features, and find out how predictions are changed.

Specifically, it allows to ask "what-if" questions:

  • What would the model predict if a record belonged to a man, instead of a woman?
  • What would the model predict if my blood glucose levels were slightly higher, or lower?

It should be noted that this tool can help assess the causal questions of the model's prediction - but not the causal questions of the real world! Answer causal questions of the real worlds require unique design, rather than some UI tool...

Example

In the most basic setting, whatifact only requries the data and a classifier. Everything will be selected automatically:

  • Whether a feature is categorical or continuous
  • Should missing values be allowed
  • How to set-up the sliders for continuous features

You will notice that some sliders have a little checkbox on their left. Selecting this checkbox will disable the slider, and set the value for this feature as null.

from sklearn.datasets import fetch_openml
import lightgbm as lgb

from whatifact import whatifact

# Load Titanic dataset
titanic = fetch_openml("titanic", version=1, as_frame=True)

# Convert to DataFrame
df = titanic.data[['pclass', 'sex', 'age', 'sibsp', 'parch', 'fare', 'embarked']]
labels = titanic.target.astype(int)

# Train a LGBMClassifier
clf = lgb.LGBMClassifier(verbose=0).fit(df, labels)

# Running whatifact
app = whatifact(df=df, clf=clf)

# # Output: (Clicking on the http link will open whatifact in the browser)
# INFO:     Started server process [42841]
# INFO:     Waiting for application startup.
# INFO:     Application startup complete.
# INFO:     Uvicorn running on http://<LOCAL_IP>:8000 (Press CTRL+C to quit)

However, when running the code above, you will notice a strange behavior. The sliders for both age and fare start at negative values, which is of cource non-sensical, the parch variable has no missing-value checkbox next to it, and the sibsp was considered as a continuous feature, but we'd rather handle it as categorical.

To change this behavior, we can send the feature_settings parameter, such as:

feature_settings = {
    'parch': {'null': True},
    'age': {'min': 0},
    'fare': {'min': 0},
    'sibsp': {'type': 'categorical'}
}

app = whatifact(df=df, clf=clf, feature_settings=feature_settings)

This should solve the above behavior. feature_settings is a dictionary, with column names as keys and dicionaries as values. All features may contain null or type keys.

  • null: a boolearn (True/False) to state whether the feature be null (i.e., null-checkbox for continuous features, an empty selection for categorical features).
  • type: 'continuous' or 'categorical' - manually defining the feature type.

Continuous features may also contain the min, max, step, and decimals keys. All other keys will be ignored. The decimals parameters is an integer defining the number of decimal digits for rounding purposes (default: 1).

The last two parameters in whatifact are sample_id and run_application.

  • sample_id is the name of a column in df, that will be used in the sample selector at the top of the app. If it remains None, the index column will be used as sample_id.
  • run_application is a boolean (True/False, defaults to True) that run the web service to run the shiny app. If changed to False, an App object will be returned, but not run, and running the app will require shiny run my_file.py

Limitations

whatifact currently works with binary prediction models only, and should support LogisticRegression, XGBoost, and LGBMClassifier.

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

whatifact-0.1.1.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

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

whatifact-0.1.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file whatifact-0.1.1.tar.gz.

File metadata

  • Download URL: whatifact-0.1.1.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for whatifact-0.1.1.tar.gz
Algorithm Hash digest
SHA256 77e3ebbad60c602f41b5507935b743bdac5df98af81b2e05949b18c1619de760
MD5 ba78ca1623df64c72e2cd28806b10b40
BLAKE2b-256 76a02edde44d95f64899f15282f86c99aedac39c4f94d6cb334afce9a3b93b49

See more details on using hashes here.

File details

Details for the file whatifact-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: whatifact-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for whatifact-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2757041b63215272ef3a41d6aa2b0b8f6757c5359409050ab01a8422bdf44312
MD5 95663e66c08a46a24c7509923caff924
BLAKE2b-256 41e22cbc4b965e0b77d464f305888532eef98ce4493e13295a3cef8f23193af4

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