Skip to main content

Ethical risk evaluation for machine learning models.

Project description

beyond_accuracy

beyond_accuracy is a production-ready Python package for ethical risk evaluation of machine learning models. It accepts a trained model and dataset, computes ethical risk metrics, generates an aggregate ethical risk score, exports JSON and PDF reports, and provides a Streamlit dashboard plus CLI workflow.

Installation

pip install beyond_accuracy

For local development:

pip install -r requirements.txt
pip install -e .

Python Usage

import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler

from beyond_accuracy import evaluate

df = pd.read_csv("loan_data.csv")

X = df.drop(columns=["loan_status"])
y = df["loan_status"]

numeric_columns = X.select_dtypes(include=["number"]).columns.tolist()
categorical_columns = [col for col in X.columns if col not in numeric_columns]

model = Pipeline(
    steps=[
        (
            "preprocessor",
            ColumnTransformer(
                transformers=[
                    (
                        "num",
                        Pipeline(
                            steps=[
                                ("imputer", SimpleImputer(strategy="median")),
                                ("scaler", StandardScaler()),
                            ]
                        ),
                        numeric_columns,
                    ),
                    (
                        "cat",
                        Pipeline(
                            steps=[
                                ("imputer", SimpleImputer(strategy="most_frequent")),
                                ("encoder", OneHotEncoder(handle_unknown="ignore")),
                            ]
                        ),
                        categorical_columns,
                    ),
                ]
            ),
        ),
        ("classifier", LogisticRegression(max_iter=1000)),
    ]
)

model.fit(X, y)

report = evaluate(
    model=model,
    data=df,
    target="loan_status",
    sensitive=["gender", "age"],
    launch_ui=False,
)

print(report.risk_score)
print(report.decision)
print(report.module_scores)
print(report.to_json())
report.save_json("ethical_report.json")
report.save_pdf("ethical_report.pdf")

To run evaluation and launch the Streamlit dashboard directly from Python:

report = evaluate(
    model=model,
    data=df,
    target="loan_status",
    sensitive=["gender", "age"],
    launch_ui=True,
    json_path="ethical_report.json",
    pdf_path="ethical_report.pdf",
)

CLI Usage

Run evaluation and open the dashboard:

beyond-audit --data data.csv --target loan_status --sensitive gender age --ui

Run evaluation without opening the dashboard:

beyond-audit --data data.csv --target loan_status --sensitive gender age --no-ui

Behavior:

  • Loads the CSV dataset
  • Uses a supplied pickled model when --model model.pkl is provided
  • Otherwise trains a baseline logistic regression pipeline
  • Runs the ethical evaluation
  • Saves ethical_report.json and ethical_report.pdf
  • Launches the Streamlit dashboard with the saved JSON report when UI is enabled

Streamlit Dashboard

The dashboard supports:

  • CSV upload
  • Optional report JSON upload
  • Optional pickled model upload
  • Target and sensitive attribute selection
  • Ethical Risk Score and decision display
  • Gauge, bar, pie, and bias comparison charts
  • JSON and PDF report downloads

Run directly:

streamlit run beyond_accuracy/ui/dashboard.py

Load a saved report directly into the dashboard:

streamlit run beyond_accuracy/ui/dashboard.py -- --report-json ethical_report.json

Project Structure

beyond_accuracy/
??? beyond_accuracy/
?   ??? __init__.py
?   ??? cli.py
?   ??? evaluator.py
?   ??? metrics/
?   ?   ??? bias.py
?   ?   ??? dataset.py
?   ?   ??? explainability.py
?   ?   ??? privacy.py
?   ?   ??? robustness.py
?   ??? report/
?   ?   ??? pdf_export.py
?   ?   ??? report_generator.py
?   ??? ui/
?       ??? dashboard.py
??? examples/
?   ??? example_usage.py
??? tests/
?   ??? test_evaluator.py
??? pyproject.toml
??? README.md
??? requirements.txt
??? setup.py

EthicalReport API

The evaluate(...) API returns an EthicalReport object with:

  • risk_score
  • decision
  • module_scores
  • to_dict()
  • to_json()
  • save_json()
  • save_pdf()

Publishing To PyPI

python -m pip install --upgrade build twine
python -m build
python -m twine check dist/*
python -m twine upload dist/*

Before publishing:

  • Update the version in beyond_accuracy/__init__.py, setup.py, and pyproject.toml
  • Verify package metadata
  • Run the test suite
  • Build and inspect the generated wheel and source distribution

Screenshots

Add dashboard screenshots here:

  • docs/images/dashboard-overview.png
  • docs/images/report-downloads.png

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

beyondaccuracy_ml-0.2.1.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

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

beyondaccuracy_ml-0.2.1-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

Details for the file beyondaccuracy_ml-0.2.1.tar.gz.

File metadata

  • Download URL: beyondaccuracy_ml-0.2.1.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.8

File hashes

Hashes for beyondaccuracy_ml-0.2.1.tar.gz
Algorithm Hash digest
SHA256 13290e93629d2e691ee88ea670a3551c389c691c34494949d839cfcba644075e
MD5 3c5d6982bf7824de210f8ea7c9d4b16b
BLAKE2b-256 b4eb58621161d2a3a417a7f0602e09b0d15a80a8aca0c94eaf6f7b64b5124b1f

See more details on using hashes here.

File details

Details for the file beyondaccuracy_ml-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for beyondaccuracy_ml-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2aa0ef22183d815a356c7663400eba3c687a66e18594dd88ee8a77395e107505
MD5 c4682d9e3edddd03f6e9f9eb80d75db0
BLAKE2b-256 56be807ccd1f5a8919700344b177b1ce2908520a7bc247702e9c33706c667bbd

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