Skip to main content

LUNA-XAI

Local Understanding through Neighborhood Analysis for Explainable AI

LUNA is a Python library for model-agnostic explainability on tabular machine learning models.
It focuses on local explanations built from neighborhood analysis (HNSW), sensitivity analysis (DSA-style), and a Calinski–Harabasz-based importance metric with robust normalization.

The current version accepts data managed by pandas and sklearn models and pipelines.

LUNA is designed to be:

  • Model-agnostic — works with any sklearn-like estimator (regression or classification).
  • Tabular-first — optimized for tabular problems with mixed numerical & categorical features.
  • Neighborhood-based — uses HNSW neighbors + synthetic sampling (LHS, CTGAN) for local exploration.
  • Metric-driven — feature importances derived from CH-style separability scores, with optional arctan normalization.
  • Extensible — pluggable methods, metrics, renderers, and domains.

Main Features

  • Hybrid preprocessing for neighbors

    • HybridPreprocessor with:
      • Optional CANE IDF encoding for categorical features.
      • Global scaling via StandardScaler (configurable).
      • Fully numeric representation to build HNSW indices.
  • Efficient neighborhood search

    • HNSW index via hnswlib:
      • HNSWBuilder + HNSWIndexManager.
      • Save/load index and metadata separately from training data.
  • Local sensitivity analysis (DSA-style)

    • DSAMethod:
      • Finds neighbors in hybrid space.
      • Generates synthetic samples via:
        • Empirical LHS (LatinHypercube from scipy.stats.qmc).
        • CTGAN (optional, if installed).
      • Perturbs features over discretized levels for sensitivity analysis.
  • CH-based metric with sign & normalization

    • CHMetric + utilities:
      • calculate_ch_index() — per-feature CH-like separability across levels.
      • adjust_signal() — flips sign based on monotonic trend direction.
      • arctan_normalize() — squashes importances to [-1, 1] for stability.
  • Explainers

    • TabularRegressionExplainer
      • Works on predictions y_hat.
      • 2-stage design:
        1. generate_explanation_data() → sensitivity data.
        2. explain_from_data() → importances + rendering.
    • TabularClassificationExplainer
      • Uses predict_proba for the predicted class (argmax).
      • Passes target_class down to the method for class-conditional analysis.
  • Plotting & rendering

    • PlotRenderer + MatplotlibPngExporter:

      • Local feature importances (horizontal bars).

      LUNA Feature Importance

      • ICE-like line plots (feature trends).

      LUNA ICE-style Plot

      • Per-feature vec boxplots for sensitivity distributions.

      LUNA VEC Boxplot

    • Low-level plotting helpers in luna_xai.utils.plotting.

  • Logging utilities

    • logging_utils:
      • get_luna_logger() — scoped loggers under luna_xai.
      • configure_luna_logging() — console/file configuration.
      • log_execution_segment() — context manager for timing segments.

Documentation

Complete documentation is available at:

LUNA XAI Documentation

Complete tutorials, and examples are available at:

LUNA XAI Classification Demo

LUNA XAI Regression Demo

📦 Installation

Prerequisites: Python >=3.11, <3.13 and a C++ toolchain (for hnswlib).

Windows Installation Notes

On Windows, the hnswlib dependency may require a C++ compiler to be installed, as precompiled binaries may not be available for all Python versions.

If installation fails with an error such as:

error: Microsoft Visual C++ 14.0 or greater is required.

Install the Microsoft C++ Build Tools and select the Desktop development with C++ workload, including the MSVC compiler and Windows SDK.

After installation, restart your terminal and retry:

pip install luna-xai

This requirement comes from the hnswlib dependency and does not require any changes to the LUNA source code.

Disclaimer and Limitations

This package is provided under the MIT License and is intended to support the interpretation and analysis of machine learning models.

As with other explainable AI methods, the generated explanations are subject to methodological assumptions and may vary depending on the underlying model, data, and configuration. They should be interpreted within the context of the specific application and are not guaranteed to accurately or completely represent the model's behavior in every scenario.

Users are encouraged to assess the suitability of the explanations for their intended use and exercise appropriate judgment when incorporating them into decision-making processes.

The software is provided "AS IS", without warranties of any kind. To the maximum extent permitted by applicable law, the authors and contributors shall not be liable for any claims, damages, losses, or other liabilities arising from the use of, inability to use, or reliance upon the software or its outputs, including inaccurate, incomplete, or misleading explanations.

For further details, please refer to the MIT License.

Metadata

Release files for luna-xai 0.1.3

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

Source distribution (sdist)

Source distribution for luna-xai 0.1.3
File Size Uploaded Provenance Actions
luna_xai-0.1.3.tar.gz 2.6 MB Without provenance Details for luna_xai-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for luna-xai 0.1.3
File Interpreter ABI Platform Actions
luna_xai-0.1.3-py3-none-any.whl Python 3 none any Details for luna_xai-0.1.3-py3-none-any.whl

Total release size: 5.3 MB

Release files / luna_xai-0.1.3.tar.gz

Download URL luna_xai-0.1.3.tar.gz
Size 2.6 MB
Tags Source
SHA-256 checksum
How to use checksums
a254a075cc92392ded482677f2c7c4e4d084479cf93b6940a3e2c785429d73cd
BLAKE2b-256 checksum
How to use checksums
5c08ed798ccde645cff69a44b1fdf83580a19e76e6c308b2fdb6e42174fa5dd0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.7

Release files / luna_xai-0.1.3-py3-none-any.whl

Download URL luna_xai-0.1.3-py3-none-any.whl
Size 2.7 MB
Tags Python 3
SHA-256 checksum
How to use checksums
6f3a256347ec65019ca90efaa4ee47fa6d03407beb34e1f3745dd462975f58af
BLAKE2b-256 checksum
How to use checksums
be88a262617f0bc359013ee8cb383a852d9cc9fab1daa3ff0131938ff61cd41d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.7

Security for luna-xai 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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