Skip to main content

supervised-discretization

This repository contains the code for the paper Supervised Feature Compression based on Counterfactual Analysis

Installation

  • The MILP problem for computing the Counterfactual Explanation for a point is implemented in Gurobi. An active Gurobi Licence is needed to run the code.

  • The package can be installed with the command:

pip install SupervisedDiscretization

Hyperparameters

The implementation of the FCCA procedure can be found in the file discretize.py that contains the Python class FCCA which takes the following parameters:

  • estimator: an unfitted binary classifier from the sklearn package. It can be one of the following: RandomForestClassifier, GradientBoosting, LinearSVC, SVC(kernel='linear'). It is also possible to take in input GridSearchCV to choose in cross validation the parameters of the estimator;
  • p0, p1: lower and upper bound for the classification probability of points for which computing the Counterfactual Explanation;
  • lambda0, lambda1, lambda2: hyperparameters for the Counterfactual Explanation problem that represents respectively the weights for the l0-, l1- and l2- norm;
  • compress: boolean that is set to True to merge thresholds whose absolute difference is smaller than 0.01;
  • timelimit: time limit in seconds for solving the Counterfactual Explanations problem;
  • verbose: boolean that is set to True to print some informations about the process of fitting the FCCA procedure.

The FCCA class offers the following methods:

  • fit: method for fitting the FCCA procedure;
  • transform: method for discretizing a dataset by using the set of thresholds previously computed via the fit method;
  • fit_transform: method for applying in sequence the fit and transform methods;
  • selectThresholds: method for setting a different value of Q after the fit has been called; this method allows to subsample the set of thresholds in a fast way without recomputing the FCCA procedure.

Execution

We report an example on how to use the FCCA procedure on new data. The example can also be found in the file example.py

import pandas as pd
from sklearn.ensemble import GradientBoostingClassifier
from SupervisedDiscretization.discretizer import FCCA

if __name__ == '__main__':
    # Reading the dataset
    data = pd.read_csv('datasets/boston.csv')
    label_column = data.columns[-1]
    feature_columns = data.columns[:-1]

    # Train - test split
    data_ts = data.sample(n=int(0.3*len(data)))
    data_tr = data.drop(index=data_ts.index)

    x_tr, y_tr = data_tr[feature_columns], data_tr[label_column]
    x_ts, y_ts = data_ts[feature_columns], data_ts[label_column]

    # Target model
    target = GradientBoostingClassifier(max_depth=2, n_estimators=100,learning_rate=0.1)

    # Hyperparameters for the discretization - default values
    discretizer = FCCA(target, p0=0.5, p1=1, lambda0=0.1, lambda1=1, lambda2=0)

    # Discretization
    x_tr_discr, y_tr_discr = discretizer.fit_transform(x_tr, y_tr)
    x_ts_discr, y_ts_discr = discretizer.transform(x_ts, y_ts)

    # Compression - inconsistency rate
    print(f'Compression rate: {discretizer.compression_rate(x_ts, y_ts)}')
    print(f'Inconsistency rate: {discretizer.inconsistency_rate(x_ts, y_ts)}')

    print('Setting Q to 0.7')
    # Increasing the value of Q
    tao_q = discretizer.selectThresholds(0.7)

    # Discretization
    x_tr_discr, y_tr_discr = discretizer.transform(x_tr, y_tr, tao_q)
    x_ts_discr, y_ts_discr = discretizer.transform(x_ts, y_ts, tao_q)

    # Compression - inconsistency rate
    print(f'Compression rate: {discretizer.compression_rate(x_ts, y_ts, tao_q)}')
    print(f'Inconsistency rate: {discretizer.inconsistency_rate(x_ts, y_ts, tao_q)}')

Metadata

Release files for SupervisedDiscretization 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for SupervisedDiscretization 0.0.7
File Size Uploaded
SupervisedDiscretization-0.0.7.tar.gz 10.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for SupervisedDiscretization 0.0.7
File Interpreter ABI Platform
SupervisedDiscretization-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 21.7 kB

Release files / SupervisedDiscretization-0.0.7.tar.gz

Download URL SupervisedDiscretization-0.0.7.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
37eccd98d242f7e4a794ecc45d7fe69cc06fc0a3895723b37eae6a9fb359b4b0
BLAKE2b-256 checksum
How to use checksums
4a5bc10b99351700fd9de05aaa0cd025fcc1c3ae9c25f82a0c3de51cd2a3a239
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Release files / SupervisedDiscretization-0.0.7-py3-none-any.whl

Download URL SupervisedDiscretization-0.0.7-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f5295480a2914f6f480fc0b3e52c42e686d74abc62e5ef5a2a4743c8d7486e57
BLAKE2b-256 checksum
How to use checksums
57c624b86663b81c0d847fc802001cfd3cd8fbce219496479cc050ef79dd50a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page