Skip to main content

EazyML Responsible-AI: Counterfactual

Python PyPI package Code Style

EazyML

eazyml-counterfactual is a python package that helps users optimize predictive outcomes by generating counterfactual explanations. EazyML revolutionizes machine learning by introducing counterfactual inference, automating the process of identifying optimal changes to variables that shift outcomes from unfavorable to favorable. This approach overcomes the limitations of manual "what-if" analysis, enabling models to provide actionable, prescriptive insights alongside their predictions.

Designed for post-prediction analysis, this package answers questions like:

  • "What minimal changes to input features can reverse an unfavorable prediction?"
  • "How can I achieve a desired outcome by tweaking feature values?"

Features

  • Prescriptive Insights: Automatically recommend actionable steps to achieve desired outcomes by identifying optimal feature modifications.
  • Outcome Optimization: Calculates the likelihood of favorable outcomes and determines optimal adjustments to improve predictions while respecting user-defined constraints.
  • Flexible Feature Selection: Allows users to manually specify relevant features from the training dataset for counterfactual inference.
  • Custom Model Integration: Accepts pre-trained machine learning models and user-defined preprocessing steps for seamless inference.
  • Classification and Regression Support: Handles both classification and regression problems.

eazyml-counterfactual is ideal for scenarios like loan approvals, customer churn prevention, and healthcare treatment planning.`

Installation

User installation

The easiest way to install counterfactual package is using pip:

pip install -U eazyml-counterfactual

Dependencies

EazyML Counterfactual requires :

  • pandas
  • eazyml-automl
  • openpyxl
  • scikit-learn
  • scipy

Usage

Counterfactual inference can be applied by initializing the EazyML framework and obtaining actionable insights for new test data. It is important to note that counterfactual inference is a post-prediction analysis. This means it can only be applied after the model is built and predictions are made.

Imports

from eazyml_counterfactual import ez_init, ez_cf_inference

Initialize and Read Data

# Initialize the EazyML automl library.
_ = ez_init()

# Specify the training dataset (Replace with the correct data path).
train_data_path = "path_to_your_training_data.csv"

# Load the predicted test dataset (Replace with the correct file path).
# Ensure the `ez_predict` function is configured to generate predictions along with probability scores.
predicted_data_path = "path_to_your_predicted_test_data.csv"
pred_df = pd.read_csv(predicted_data_path)

Set Input Parameters

# Specify the outcome (target variable)
outcome = "target"  # Replace with your target variable name

# List the features used during model training
selected_features = ['feature1', 'feature2', 'feature3']

# Retrieve model information from the `build_model_response`.
model_info = build_model_response["model_info"]

# Define variants
variants = ['feature1', 'feature2']

Counterfactual Inference

# Select test record for inference
test_index_no = 0
test_data = pred_df.loc[[test_index_no]]

# Customize options for counterfactual inference
cf_options = {"variants": variants, "outcome_ordinality": "1", "train_data": train_data_path}

# Call the EazyML API for counterfactual inference
result, optimal_transition_df = ez_cf_inference(
    test_data=test_data,
    outcome=outcome,
    selected_features=selected_features,
    model_info=model_info,  
    options=cf_options
)

Sample Output Preview

# Summarizes whether an optimal transition was found and the improvement in outcome probability.
result = {
    "success": True,
    "message": "Optimal transition found",
    "summary": {
        "Actual Outcome": "0",
        "Optimal Outcome": "1",
        "Improvement in Probability": 0.416
    }
}

# Details the feature changes needed to achieve the optimal outcome.
optimal_transition_df = pd.DataFrame({
    "Feature": ["Feature1", "Feature1", "Feature3"],
    "Actual": [13.0, 32554.0, 2.29],
    "Optimal": [15.6, 29203.22, 2.29],
    "Percentage Change": [20.0, -10.3, 0.0],
    "Absolute Change": [2.6, -3350.78, 0.0]
})

You can find more information in the documentation.

  • Documentation

  • Homepage

  • If you have questions or would like to discuss a use case, please contact us here

  • Here are the other packages from EazyML suite:

    • eazyml-automl: eazyml-automl provides a suite of APIs for training, optimizing and validating machine learning models with built-in AutoML capabilities, hyperparameter tuning, and cross-validation.
    • eazyml-data-quality: eazyml-data-quality provides APIs for comprehensive data quality assessment, including bias detection, outlier identification, and drift analysis for both data and models.
    • eazyml-counterfactual: eazyml-counterfactual provides APIs for optimal prescriptive analytics, counterfactual explanations, and actionable insights to optimize predictive outcomes to align with your objectives.
    • eazyml-insight: eazyml-insight provides APIs to discover patterns, generate insights, and mine rules from your datasets.
    • eazyml-xai: eazyml-xai provides APIs for explainable AI (XAI), offering human-readable explanations, feature importance, and predictive reasoning.
    • eazyml-xai-image: eazyml-xai-image provides APIs for image explainable AI (XAI).

License

This project is licensed under the Proprietary License.


Maintained by EazyML
© 2025 EazyML. All rights reserved.

Release files for eazyml-counterfactual 1.0.5

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

Source distribution (sdist)

Source distribution for eazyml-counterfactual 1.0.5
File Size Uploaded
eazyml_counterfactual-1.0.5.tar.gz 18.6 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for eazyml-counterfactual 1.0.5
File Interpreter ABI Platform
eazyml_counterfactual-1.0.5-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 37.9 MB

Release files / eazyml_counterfactual-1.0.5.tar.gz

Download URL eazyml_counterfactual-1.0.5.tar.gz
Size 18.6 MB
Tags Source
SHA-256 checksum
How to use checksums
b4ee96dfb8997dc162f3d74e804cbdf09d33eb75e46d53ed48c08dd9571ce2a1
BLAKE2b-256 checksum
How to use checksums
6dea769ab85e9f96a22eb0aed63171dd294898d2aeee327128eaf37004640a11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / eazyml_counterfactual-1.0.5-py2.py3-none-any.whl

Download URL eazyml_counterfactual-1.0.5-py2.py3-none-any.whl
Size 19.3 MB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7505069fe4180ebfae142de2790b12d6571c1bfbf55edc7c32612a7f2eef3995
BLAKE2b-256 checksum
How to use checksums
9f368b3e0b7c587863ffc9f78ae09648af4f8cab669f4e06f4fafff1cfd9a9af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release history Release notifications | RSS feed

This release

1.0.5 This release

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

0.0.68

2 release files

0.0.67

2 release files

0.0.56

2 release files

0.0.54

2 release files

0.0.53

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